Sign in to follow this  

argh

This topic is 2854 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So i just wrote this program right now and i quite figure out whats wrong with it =/ Any suggestions would be helpful. Thank you p.s sorry, im still new to this site, not sure how to copy the code for easy viewing. #include <iostream> 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]; 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"; return 0; } void ReadBig(int num[MAX_DIGITS]) { int j, length, temp, f; string s; cin>>s; length=s.length(); f=(length-1)/2; for( j=0;j<=MAX_DIGITS-1;j++) { num[j]=0; } for(j=0;j<=length-1;j++) { num[j]=s.at(j)-'0'; } for(j=0;j<=f;j++) { temp=num[s.length()-1-1]; num[s.length()-1-1]=num[j]; num[j]-temp; } } //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 addBig(int num1[], int num2[], int sum[]) { int j, ones=0, carry=0; for(j=0;j<=MAX_DIGITS-1;j++) { sum[j]=num1[j]+num2[j]; } for(j=0;j<=MAX_DIGITS-2;j++) if(sum[j]>=10) { ones-sum[j]%10; carry-sum[j]/10; sum[j]=ones; sum[j+1]=sum[j+1]+carry; } } //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 printBig(int num[MAX_DIGITS]) { int j=0; int skip=MAX_DIGITS-1; while(num[skip]==0 && skip>0) skip--; for(j=skip;j>=0;j--) cout<<num[j]; } //PrintBig uses a while loop to skip leading zeros and then uses a for loop to print the number.

Share this post


Link to post
Share on other sites

//There should be no changes made to the main program when you turn it in.



This is homework so we won't answer it for you. Why don't you describe what you mean by "can't figure out what's wrong"? Is it crashing? not compiling? is the assignment to figure out what's wrong?

The typical way to figure out what's wrong is to run the program through your debugger and step through the code making sure it's doing what you think it should be doing.

-me

Share this post


Link to post
Share on other sites
okay fixed one problem =], as for the program, yes its homework, i've tried debugging it and i cant quite understand whats wrong with it. I have commented the lines that error when debugged.


#include <iostream>
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];
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";
return 0;
}

void ReadBig(int num[MAX_DIGITS])
{
int j, length, temp, f;
string s;
cin>>s; //error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
length=s.length();
f=(length-1)/2;
for(
j=0;j<=MAX_DIGITS-1;j++)
{
num[j]=0;
}
for(j=0;j<=length-1;j++)
{
num[j]=s.at(j)-'0';
}
for(j=0;j<=f;j++)
{
temp=num[s.length()-1-1];
num[s.length()-1-1]=num[j];
num[j]-temp; //C4552: '-' : operator has no effect; expected operator with side-effect
}
}


//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 addBig(int num1[], int num2[], int sum[])
{
int j, ones=0, carry=0;
for(j=0;j<=MAX_DIGITS-1;j++)
{
sum[j]=num1[j]+num2[j];
}
for(j=0;j<=MAX_DIGITS-2;j++)
if(sum[j]>=10)
{
ones-sum[j]%10; //C4552: '-' : operator has no effect; expected operator with side-effect
carry-sum[j]/10; //C4552: '-' : operator has no effect; expected operator with side-effect
sum[j]=ones;
sum[j+1]=sum[j+1]+carry;
}
}

//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 printBig(int num[MAX_DIGITS])
{
int j=0;
int skip=MAX_DIGITS-1;
while(num[skip]==0 && skip>0)
skip--;
for(j=skip;j>=0;j--)
cout<<num[j];
}
//PrintBig uses a while loop to skip leading zeros and then uses a for loop to print the number.


Share this post


Link to post
Share on other sites

This topic is 2854 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this