Advertisement
stak441

Methods - 8 - SumArrays

Jan 18th, 2013
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _8.SumDigitArrays
  7. {
  8.     class SumArrays
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             int[] firstArray = { 4, 1, 8, 5, 2, 1 };
  13.             int[] secondArray = { 8, 6, 1, 2, 7, 6, 4, 8 };
  14.  
  15.             int[] sumArray = SumOfArrays(firstArray, secondArray);
  16.             for (int i = sumArray.Length - 1; i >= 0; i--)
  17.             {
  18.                 if (i == sumArray.Length - 1 && sumArray[i] == 0)        //If the last element was 0, it will not be printed
  19.                 {
  20.                     continue;
  21.                 }
  22.                 Console.Write(sumArray[i]);
  23.             }
  24.             Console.WriteLine();
  25.         }
  26.  
  27.         static int[] SumOfArrays(int[] firstArray, int[] secondArray)
  28.         {
  29.             bool firstArrayShorter = (firstArray.Length < secondArray.Length);
  30.             int minLength = 0;
  31.             int maxLength = 0;
  32.             if (firstArrayShorter)
  33.             {
  34.                  minLength = firstArray.Length;
  35.                  maxLength = secondArray.Length;
  36.             }
  37.             else
  38.             {
  39.                  minLength = secondArray.Length;
  40.                  maxLength = firstArray.Length;
  41.             }
  42.  
  43.             int[] sumArray = new int[maxLength + 1];         //size is 1 more, because if the 2 sizes are equal, the last digits may sum to more than 10                              
  44.             bool largerThanTen = false;
  45.  
  46.             for (int i = 0; i < sumArray.Length; i++)
  47.             {
  48.                 int tempSum = 0;
  49.                 if (i < minLength)                      // First part - sums both arrays
  50.                 {
  51.                     tempSum = firstArray[i] + secondArray[i];
  52.                 }
  53.                 else if (i >= minLength && i < sumArray.Length - 1)         //Second part - sums only the longer array
  54.                 {
  55.                     if (firstArrayShorter)
  56.                     {
  57.                         tempSum += secondArray[i];
  58.                     }
  59.                     else
  60.                     {
  61.                         tempSum += firstArray[i];
  62.                     }
  63.                 }
  64.                
  65.                 if (largerThanTen)                           //Adds 1 if the previous sum was > 10
  66.                 {
  67.                     tempSum += 1;
  68.                 }
  69.  
  70.                 if (tempSum > 9)
  71.                 {
  72.                     largerThanTen = true;
  73.                     sumArray[i] = tempSum % 10;
  74.                 }
  75.                 else
  76.                 {
  77.                     largerThanTen = false;
  78.                     sumArray[i] = tempSum;
  79.                 }
  80.             }
  81.  
  82.             return sumArray;
  83.            
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement