Advertisement
tvarbanov

CSharp2-03-08

Jan 4th, 2014
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.22 KB | None | 0 0
  1. using System;
  2.  
  3. class ToArrayOfDigits
  4. {
  5.     /*Write a method that adds two positive integer numbers represented as arrays of digits
  6.      * (each array element arr[i] contains a digit; the last digit is kept in arr[0]).
  7.      * Each of the numbers that will be added could have up to 10 000 digits.*/
  8.     static int[] ToArray(string num)
  9.     {
  10.         int[] array = new int[num.Length]; //new array for reversed storing
  11.         for (int i = 0; i < num.Length; i++)
  12.         {
  13.             array[i] = num[num.Length - 1 - i]-48;
  14.         }
  15.         return array;
  16.     }
  17.  
  18.     static int[] SumOfArrays(int[] firstArr, int[] secArr)
  19.     {
  20.         //Get the length of the possible outcome of the sum
  21.         int maxLength = Math.Max(firstArr.Length, secArr.Length) + 1;
  22.         //Create two new arrays with the new max length
  23.         int[] arrOne = new int[maxLength];
  24.         int[] arrTwo = new int[maxLength];
  25.         //Transfer the old array to the new ones
  26.         for (int i = 0; i < firstArr.Length; i++)
  27.         {
  28.             arrOne[i] = firstArr[i];
  29.         }
  30.         for (int i = 0; i < secArr.Length; i++)
  31.         {
  32.             arrTwo[i] = secArr[i];
  33.         }
  34.         int tempSum = 0;
  35.         int remainder = 0;
  36.         //New array for the sum
  37.         int[] sumOfArrs = new int[maxLength];
  38.         //Actual sum of the arrays
  39.         for (int i = 0; i < maxLength; i++)
  40.         {
  41.             tempSum = arrOne[i] + arrTwo[i]; //Calculate temp result
  42.             if (tempSum + remainder > 10) //If temp result + remainder is bigger than 10 gets the remainder of mod with 10 and adds the remainder if there is
  43.             {
  44.                 sumOfArrs[i] = (arrOne[i] + arrTwo[i]) % 10 + remainder;
  45.                 remainder = 1;
  46.             }
  47.             else if (tempSum + remainder == 10) // if the result is exactly 10 add 0 and remainder 1 for the next sum of elements
  48.             {
  49.                 sumOfArrs[i] = 0;
  50.                 remainder = 1;
  51.             }
  52.             else //if its <10 just adds the sum and resets the remainder
  53.             {
  54.                 sumOfArrs[i] = tempSum + remainder;
  55.                 remainder = 0;
  56.             }
  57.         }
  58.         //Reverse the array
  59.         Array.Reverse(sumOfArrs);
  60.         //Checks if the first digit is 0(for example 100+100 remaind with only 3 digits) and if it is - removes it
  61.         if (sumOfArrs[0] == 0)
  62.         {
  63.             int[] newSumArr = new int[sumOfArrs.Length - 1];
  64.             for (int i = 0; i < newSumArr.Length; i++)
  65.             {
  66.                 newSumArr[i] = sumOfArrs[i + 1];
  67.             }
  68.             return newSumArr;
  69.         }
  70.         else
  71.         {
  72.             return sumOfArrs;
  73.         }
  74.  
  75.     }
  76.  
  77.     static void Main()
  78.     {
  79.         Console.Write("Enter the first number: ");
  80.         string firstNum = Console.ReadLine();
  81.         int[] firstNumArr = ToArray(firstNum); //assign the reversed array to int array
  82.         Console.Write("Enter the second number: ");
  83.         string secondNum = Console.ReadLine();
  84.         int[] secondNumArr = ToArray(secondNum); //assign the reversed array to int array
  85.         //Print the reversed arrays
  86.         Console.WriteLine(string.Join("",SumOfArrays(firstNumArr,secondNumArr)));
  87.  
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement