can someone help me im getting some errors void addBig(int a[], int b[], int c[]) { int i, temp, ones, carry; for(i=0;i<=MAX_DIGITS-1;i++)//sets the int c[] to zero c[i]=0; for(i=0;i<=MAX_DIGITS-1;i++) { c[i]=a[i]+b[i];//adds the arrays togther } for(i=0;i<=MAX_DIGITS-1;i++) { ones=c[i]%10; carry=c[i]/10; c=carry[i]; c=ones + carry c[i+1] } }
the last two lines are giving me errors c=carry{i} and c=ones+carry c{i+1}
where us max_digits?
carry[] doesn't exist from whatr I see
c by itself also doesn't exist... You cannot get rid of an array, or add them randomly.. c != c[] carry != carry[]
well this is the whole code //harjasdeep Samra //This program will test three functions capable of reading, adding, and printing 100-digit numbers. #include <iostream> #include<string> using namespace std; // 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; } //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. 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-1;i++) { x[i]=number.at(l)-'0'; l--; } } //PrintBig uses a while loop to skip leading zeros and then uses a for loop to print the number. void printBig(int a[]) { int count, i=0; bool x=true; count=MAX_DIGITS-1; while(x) { if(a[count]!=0) x=false; //else if(count==MAX_DIGITS-1) //x=false; else //i++; count--; } for(i=count;i>=0;i--) cout<<a[i]; } //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. void addBig(int a[], int b[], int c[]) { int i, temp, ones, carry; for(i=0;i<=MAX_DIGITS-1;i++)//sets the int c[] to zero c[i]=0; for(i=0;i<=MAX_DIGITS-1;i++) { c[i]=a[i]+b[i];//adds the arrays togther } for(i=0;i<=MAX_DIGITS-1;i++) { ones=c[i]%10; carry=c[i]/10; c=carry[i]; c=ones + carry c[i+1] } }
same error as stated above.
yes
"KonradZuse c by itself also doesn't exist... You cannot get rid of an array, or add them randomly.. c != c[] carry != carry[] 8 minutes ago"
how could i fix this problem
Not sure exactly what each variable is doing, but if you need a carry array then make it an array, and if you need a single variable 'c' then make it a dummy variable since you use it once... You also seem to only use carry once, so I'm not sure why there is an array of it.
Join our real-time social learning platform and learn together with your friends!