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.

// This constant should be 100 when the program is finished.constint 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);

//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.

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.