In this program that I have written so far I am attempting to add two very large integers together using arrays:
Everything works right untill the numbers add up to be bigger then 10 and then I am trying to figure out how to carry the one to the next number. Any suggestions?Code:#include <iostream> using namespace std; const int Size = 20; int main () { char FirstNumber [Size]; char SecondNumber [Size]; int Length; int Length2; int Num1 [Size]; int Num2 [Size]; int Answer [Size]; //getting numbers into character array cout<<"Enter the First Number : "; cin>>FirstNumber; cout<<"Enter the Second Number : "; cin>>SecondNumber; //finding length of the arrays Length = strlen (FirstNumber); Length2 = strlen (SecondNumber); //bringing numbers in reverse so that they can be added for (int a = Length - 1; a >= 0; a--) { Num1 [a] = FirstNumber [a] - int ('0');//changing char to int for (int a = Length2 -1; a >= 0; a--) { Num2 [a] = SecondNumber [a] - int ('0'); } Answer [a] = Num1 [a] + Num2 [a];//getting sum of two numbers in the arrays if (Answer [a] > 10)//if answer of the two numbers is greter than ten, minus 10 and add ten to the next number { Answer [a] = Answer [a] - 10; Answer [a--] = Answer [a--] + 1; } cout<< Answer [a]; } return 0; }



LinkBack URL
About LinkBacks


