Adding two numbers given in the form of digitized array

Given two array, where each item in the array is a digit of a number i.e. {1,0,1} is number 101 and {1,3,5} = 135, create a sum function that takes two arrays and outputs the sum of the array.
A1 = {1,2,3}
A2 = {2,2,2}
SumArrays(A1, A2) = {3,4,5} = 345

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] array1 = { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 }; //9999999999
            int[] array2 = { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 }; //9999999999
            int[] sum = SumArrays(array1, array2);
            Console.Write("Num1 = ");
            for (int i = 0; i < array1.Length; i++)
            {
                Console.Write(array1[i]);
            }
            Console.WriteLine();
            Console.Write("Num2 = ");
            for (int i = 0; i < array2.Length; i++)
            {
                Console.Write(array2[i]);
            }
            Console.WriteLine("\n----------------------");
            Console.Write("Sum  = ");
            for (int i = 0; i < sum.Length; i++)
            {
                Console.Write(sum.GetValue(i));
            }
            Console.WriteLine();
            Console.WriteLine("----------------------");
        }

        public static int[] SumArrays(int[] array1, int[] array2)
        {
            if (array1 != null && array2 != null)
            {
                int index1 = array1.Length - 1;
                int index2 = array2.Length - 1;
                ArrayList sumArray = new ArrayList();
                int tempSum, carry = 0;
                while (index1 >= 0 || index2 >= 0)
                {
                    if (index1 >= 0 && index2 >= 0)
                    {
                        tempSum = array1[index1] + array2[index2] + carry;
                        index1--;
                        index2--;
                        if (tempSum > 9)
                        {
                            carry = 1;
                            tempSum = tempSum % 10;
                        }
                        else
                        {
                            carry = 0;
                        }
                        sumArray.Insert(0, tempSum);
                    }

                    else if (index1 >= 0)
                    {
                        tempSum = array1[index1] + carry;
                        index1--;
                        if (tempSum > 9)
                        {
                            carry = 1;
                            tempSum = tempSum % 10;
                        }
                        else
                        {
                            carry = 0;
                        }
                        sumArray.Insert(0, tempSum);
                    }

                    else if (index2 >= 0)
                    {
                        tempSum = array2[index2] + carry;
                        index2--;
                        if (tempSum > 9)
                        {
                            carry = 1;
                            tempSum = tempSum % 10;
                        }
                        else
                        {
                            carry = 0;
                        }
                        sumArray.Insert(0, tempSum);
                    }
                }
                
                if (carry > 0)
                {
                    sumArray.Insert(0, carry);
                }
                Array sum = Array.CreateInstance(typeof(int), sumArray.Count);
                sumArray.CopyTo(sum);
                return (int[])sum;
            }
            return null;
        }
    }
}

Output
=======
Num1 = 9999999999
Num2 = 9999999999
----------------------
Sum  = 19999999998
----------------------
Press any key to continue . . .
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s