Here's the question you clicked on:
Jas11
need help finishing a code
#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 (!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 << "\n+\n"; printBig(num2); cout << "\n=\n"; printBig(sum); cout << "\n"; cout <<"test again?"; cin>>response; cin.ignore(900,'\n'); done = toupper(response)=='N'; } return 0; } void readBig(int x[]) { int length, i, oneLess, l; string number; getline(cin,number); length=number.length(); l=length-1; oneLess=MAX_DIGITS-1; for(i=0;i<MAX_DIGITS;i++) { x[i]=0; } for(i=0;i<length;i++) { x[oneLess-l]=number[i]-'0'; l--; } } void printBig(int a[]) { int d, count, i, b; bool x=true; count=99; for(d=0; x && d<100 ;d++) { if(a[d]=0) { count--; } else { x=false; for(i=count;i<100;i++) { cout<<a[d]; } //d=100; } for(b=count;b<100;b++) { cout<<a[d]; } } } void addBig(int a[], int b[], int c[]) { } //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.
need help finishing the addBig and printBig functions
If I understand your addBig() function, you are taking the number from the same index out of each array, adding them, then storing it in a third array. So you will have a for loop where you have array3[i] = array2[i] + array1[i]; and presumably you will do something with it there since you are neither passing by reference or returning a value.
@eSpeX would i start it out like inti; for(i=0;i<MAX_DIGITS;i++)
Assuming MAX_DIGITS is the size of your array, yes. You want your index to step through each element of your array.
well this is what the code is supposed to do Write functions for doing arithmetic on 100-digit integers. Include functions for input, output, and addition. The input function should not print anything. The addition function should do no reading or printing. No function should make any assumptions about how the other functions or main program work. The number of digits should be in a global constant. The function prototypes should be as follows: void readBig(int[]) void addBig(int[],int[],int[]) void printBig(int[])
I see where you are getting an integer from the user, what are you doing if that number isn't as big as 100 digits?
well my teacher wrote us the main function and told us not to change it, he wants us to add the three add, print, and read functions
Did your teacher also write the readBig() method or is that something you created?
i wrote the readBig function
The same loop you use for setting the values of the array is the same method you will use to step through the addition. You just have two arrays and a temp, so you will need to create a temp[] and then loop through saying that temp[i] = x[i] + y[i]; you will need to add some error handling in the event the two numbers go over 10 so you can carry it on to the next iteration.