Advertisement
soxa

AddsTwoPositiveNumbers

Dec 31st, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.16 KB | None | 0 0
  1. using System;
  2. //Write a method that adds two positive integer numbers represented as arrays of digits (each array element arr[i] contains a digit; the last digit is kept in arr[0]). Each of the numbers that will be added could have up to 10 000 digits.
  3.  
  4. class AddsTwoPositiveNumbers
  5. {
  6.     // Convert string to array
  7.     static string[] InputElemOfStrToArray(string numStr)
  8.     {
  9.         string[] array = new string[numStr.Length];
  10.         for (int i = 0; i < numStr.Length; i++)
  11.         {
  12.             array[i] = numStr[i].ToString();
  13.         }
  14.         return array;
  15.     }
  16.     // Collection of the two arrays by index
  17.     private static string[] SumArrays(string[] firstArray, string[] secArray)
  18.     {
  19.         int currentSum = 0;
  20.         string sumStr = " ";
  21.         bool check = true;
  22.         int remainder = 0;
  23.         int maxLength = (firstArray.Length - 1);
  24.         int minLength = (secArray.Length - 1);
  25.         // Summation of the array in sumStr
  26.         for (int i = maxLength, j = minLength; i >= 0; i--)
  27.         {
  28.             currentSum = (int.Parse(secArray[j]) + int.Parse(firstArray[i]));
  29.             if (currentSum < 10)
  30.             {
  31.                 sumStr += Convert.ToString(currentSum) + " ";
  32.             }
  33.             else
  34.             {
  35.                 remainder = currentSum % 10;
  36.                 sumStr += Convert.ToString(remainder) + " ";
  37.  
  38.                 if (i > 0)
  39.                 {   // If we have remainder increase nex elem with one
  40.                     int increase = (int.Parse(firstArray[i - 1]) + 1);  
  41.                     firstArray[i - 1] = increase.ToString();
  42.                 }
  43.                 else
  44.                 {
  45.                     sumStr += 1;
  46.                 }
  47.             }
  48.             // Modeling 'j'
  49.             if ((j >= 0) && check)
  50.             {
  51.                 if (j == 0)
  52.                 {
  53.                     secArray[j] = "0";
  54.                     check = false;
  55.                     j++; // Stop out of range exept.
  56.                 }
  57.                 j--;
  58.             }
  59.         }
  60.         string[] unnecessaryElements = new string[] {" "}; // Remove empty spaces in array
  61.         string[] SplitSumStrArr = sumStr.Split( unnecessaryElements,StringSplitOptions.RemoveEmptyEntries );
  62.         Array.Reverse(SplitSumStrArr); // Reverse array SplitSumArr  
  63.         return SplitSumStrArr;
  64.     }
  65.  
  66.     static void Main(string[] args)
  67.     {
  68.         string[] firstArray;
  69.         string[] secArray;
  70.         Console.WriteLine("Enter the first integer");
  71.         string firstNum = Console.ReadLine();
  72.        
  73.         Console.WriteLine("Enter the second integer");
  74.         string secNum = Console.ReadLine();
  75.         // Most larger number adds to firstArray
  76.         if (firstNum.Length > secNum.Length)
  77.         {
  78.             firstArray = InputElemOfStrToArray(firstNum);
  79.             secArray = InputElemOfStrToArray(secNum);
  80.         }
  81.         else
  82.         {
  83.             firstArray = InputElemOfStrToArray(secNum);
  84.             secArray = InputElemOfStrToArray(firstNum);
  85.         }
  86.  
  87.         string[] arraySum = SumArrays(firstArray, secArray);
  88.         Console.WriteLine(string.Join("", arraySum));
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement