Hello i need help writing an add big function I cant get it
Hello, i need help writing an add big function. I cant get it to work properly so I will leave it at the last place where it compiles.
#include <iostream>
#include <string>
using namespace std;
//This program will test three functions capable of reading, adding,
//and printing 100-digit numbers.
// Do not change these function prototypes:
void readBig(int[]);
void printBig(int[]);
void addBig(int[], int[], int[]);
// This constant should be 100 when the program is finished.
const int MAX_DIGITS = 100;
//There should be no changes made to the main program when you turn it in.
int main()
{
// Declare the three numbers, the first, second and the sum:
int num1[MAX_DIGITS], num2[MAX_DIGITS], sum[MAX_DIGITS];
bool done = false;
char response;
while (not done)
{
cout << \"Please enter a number up to \"<<MAX_DIGITS<< \" digits: \";
readBig(num1);
cout << \"Please enter a number up to \"<<MAX_DIGITS<< \" digits: \";
readBig(num2);
//addBig(num1, num2, sum);
printBig(num1);
cout << \"\ +\ \";
printBig(num2);
cout << \"\ =\ \";
//printBig(sum);
cout << \"\ \";
cout <<\"test again?\";
cin>>response;
cin.ignore(900,\'\ \');
done = toupper(response)!=\'Y\';
}
return 0;
}
//ReadBig will read a number as a string,
//It then converts each element of the string to an integer and stores it in an integer array.
//Finally, it reverses the elements of the array so that the ones digit is in element zero,
//the tens digit is in element 1, the hundreds digit is in element 2, etc.
//AddBig adds the corresponding digits of the first two arrays and stores the answer in the third.
//In a second loop, it performs the carry operation.
//PrintBig uses a while loop to skip leading zeros and then uses a for loop to print the number.
#include <string>
//
//
void readBig( int *num )
{
for ( int i = 0; i < MAX_DIGITS; i++ ) num[i] = 0; // initialise to zero
string s;
getline( cin, s );
int last = s.size() - 1; // STILL TO ADD: CHECK last < MAX_DIGITS
for ( int i = 0; i <= last; i++ )
{
int digit = s[last-i] - \'0\'; // STILL TO ADD: check 0 <= digit <= 9
num[i] = digit;
}
}
void printBig( int *num )
{
int last = MAX_DIGITS - 1;
while ( num[last] == 0 && last > 0 ) last--; // strip trailing blanks
for ( int i = 0; i <= last; i++ ) cout << num[last-i]; // write back in original order
cout << endl;
}
Solution
The Complet program
#include <iostream>
#include <string>
using namespace std;
//This program will test three functions capable of reading, adding,
//and printing 100-digit numbers.
// Do not change these function prototypes:
void readBig(int[]);
void printBig(int[]);
void addBig(int[], int[], int[]);
// This constant should be 100 when the program is finished.
const int MAX_DIGITS = 100;
//There should be no changes made to the main program when you turn it in.
int main()
{
// Declare the three numbers, the first, second and the sum:
int num1[MAX_DIGITS], num2[MAX_DIGITS], sum[MAX_DIGITS];
bool done = false;
char response;
while (not done)
{
cout << \"Please enter a number up to \"<<MAX_DIGITS<< \" digits: \";
readBig(num1);
cout << \"Please enter a number up to \"<<MAX_DIGITS<< \" digits: \";
readBig(num2);
addBig(num1, num2, sum);
printBig(num1);
cout << \"\ +\ \";
printBig(num2);
cout << \"\ =\ \";
printBig(sum);
cout << \"\ \";
cout <<\"test again?\";
cin>>response;
cin.ignore(900,\'\ \');
done = toupper(response)!=\'Y\';
}
return 0;
}
//ReadBig will read a number as a string,
//It then converts each element of the string to an integer and stores it in an integer array.
//Finally, it reverses the elements of the array so that the ones digit is in element zero,
//the tens digit is in element 1, the hundreds digit is in element 2, etc.
//AddBig adds the corresponding digits of the first two arrays and stores the answer in the third.
//In a second loop, it performs the carry operation.
//PrintBig uses a while loop to skip leading zeros and then uses a for loop to print the number.
#include <string>
//
//
void readBig( int *num )
{
for ( int i = 0; i < MAX_DIGITS; i++ ) num[i] = 0; // initialise to zero
string s;
getline( cin, s );
int last = s.size() - 1; // STILL TO ADD: CHECK last < MAX_DIGITS
for ( int i = 0; i <= last; i++ )
{
int digit = s[last-i] - \'0\'; // STILL TO ADD: check 0 <= digit <= 9
num[i] = digit;
}
}
void printBig( int *num )
{
int last = MAX_DIGITS - 1;
while ( num[last] == 0 && last > 0 ) last--; // strip trailing blanks
for ( int i = 0; i <= last; i++ ) cout << num[last-i]; // write back in original order
cout << endl;
}
void addBig(int *num1, int *num2, int *sum)
{
int num1_last = MAX_DIGITS - 1;
int num2_last = MAX_DIGITS - 1;
int big_Num_dig,small_Num_dig,C=0,S;
for ( int i = 0; i < MAX_DIGITS; i++ ) sum[i] = 0; // initialise to zero
for(int i = 0; i< MAX_DIGITS; i++) // loop for sum individual elements
{
S = num1[i]+num2[i]; // sum of individual numbers
sum[i] = S%10+C; // extracting 10th and 1st part
C = S/10; // carry
}
}
OUTPUT
Please enter a number up to 100 digits: 123123123
Please enter a number up to 100 digits: 123123123
123123123
+
123123123
=
246246246
test again?y
Please enter a number up to 100 digits: 999999999999
Please enter a number up to 100 digits: 999999999999
999999999999
+
999999999999
=
1999999999998
test again?n



