okay.. i'm really stuck on this program assignment
i have to add two arrays (20 digits long) than sum the two.

what i have done so far is (or at least attempted) is putting the digits into a temporary array than moving them into one of the arrays i want to sum, than doing that with the second array i want to sum (i had to put the digits in from last to first). now i'm trying to sum and print the array i'm not exactly sure what my problem is yet, i figured i would get it online than try to debug it while i also get some help

anyway any input is appreciated.

here is what i have so far.

#include <iostream>

using namespace std;

int main()
{
char temp[21];
int i=0, j, k, z, q;
char augend[19];
char addend[19];
char count;
char sum;
char total;
char carry;

cout << "This programs adds two positive integers up to 20 digits long." << endl << endl;
cout << "Enter augend digits";
{
cin >> temp[0];
/*if(temp > 0)
break;*/

while(temp[i]!= '\n');
{
i=i+1;
cin >> temp[i];
}

for (j=i-1, k=19; j>=0; j--, k--)
{
augend[k] = temp[j];
}
cout << "Enter addend digits";
cin >> temp[0];
i=0;
while(temp[i]!='\n');
{
i=i+1;
cin >> temp[i];
}

for (z=i-1, q=19; q>=0; j--, q--)
{
addend[k] = temp[z];
}
while(temp[count]!='\n')

for(i=count-1; j=19; i>=0; i--; j--)
augend[j] = temp[i];

carry = 0;
total = augend[i] + addend[i] + carry;
if total > 10;
{
sum[i] = total - 10;
}
else
carry = 0;
sum[i]=total;


i = 0;
while(addend[i] != '\n');
{
cout << augend << " + " << addend << " = " << i << endl;
++i;
}
}
cout << "- is not a digit" << endl;

return 0;
}

ALso

i tried just printing my temp array, when i run it cin the digits and just stops there.. i dont see whats the problem.

#include <iostream>

using namespace std;

int main()
{
char temp[21];
int i=0, j, k, z, q;
char augend[19];
char addend[19];
char count;
char sum;
char total;
char carry;

cout << "This programs adds two positive integers up to 20 digits long." << endl << endl;
cout << "Enter augend digits: ";
cin >> temp[0];
while(temp[i]!= '\n');
{
i=i+1;
cin >> temp[i];
}

cout << temp;

return 0;
}

In your first program be careful with your variable names. Making them more descriptive might help. Also adding comments to indicate to yourself what each loop is doing should help you find some errors.

In your second program, temp is a char array, and C style strings are char arrays, but not every char array is a C style string. C style strings can be displayed using the >> operator, but char arrays that aren't C style strings cannot. Be sure that temp is a C style char array if you want to display it with >>.

In your first program be careful with your variable names. Making them more descriptive might help. Also adding comments to indicate to yourself what each loop is doing should help you find some errors.

In your second program, temp is a char array, and C style strings are char arrays, but not every char array is a C style string. C style strings can be displayed using the >> operator, but char arrays that aren't C style strings cannot. Be sure that temp is a C style char array if you want to display it with >>.

okay i added some comments and got the arrays to work, i'm still having trouble adding the two arrays together. i know one reason is because my int and char types ummm i'm flustered.. still going to debug, can anyone look at my code?

#include <iostream>

using namespace std;

int main()
{
        char temp[21];
        int i=0;
        int j, k, z, q;
        int augend[19] = {0} ;
        int addend[19] = {0};
        char count=0;
        char sum[5000];
        char total;
        char carry=0;

        cout << "This programs adds two positive integers up to 20 digits";
        cout << " long." << endl << endl;
        cout << "Enter augend digits: ";
        cin >> noskipws >> temp[0];
        i = 0 ;
        while ( temp[i] != '\n')
           {
           i=i+1;
           cin >> temp[i];
           }
        for (j=i-1, k=19; j>=0; j--, k-- )
            augend[k] = temp[j] - 48 ;            // augend
// Testing
cout << endl ;
for ( int f = 0; f <= 19; f+=1 )
     cout << augend[f] << ' ' ; // augendcout
cout << endl ;
// End Testing
        cout << "Enter addend digits: ";
        cin >> noskipws >> temp[0];
        i = 0;
        while( temp[i] != '\n')
           {
           i=i+1;
           cin >> temp[i];
           }
        for (z=i-1, q=19; z>=0; z--, q-- )
            addend[q] = temp[z] - 48 ;  //   addend
// Testing
cout << endl ;
for ( int r = 0; r <= 19; r+=1 )
     cout << addend[r] << ' ' ; //   addendcout
cout << endl ;
// End Testing

									   // Sum the two numbers.
for(i=count-1, j=19; i>=0; i--, j--)
        {
                augend[j] = temp[i];
                total = augend[i] + addend[i] + count;
                        if (total>10)
                        {
                                sum[i]=total-10;
                        }
                        else
                        {
                                count = 0;
                                sum[i]=total;
                        }

        }
                        cout << augend << " + " << addend << " = "
                             << sum[i] << endl;


        return 0;
}

you have entered both numbers and reversed the significant digits so the least significant of each has index 0. Now you need to convert each char representing a digit to an int and add the ints, starting at index 0 and advancing to index = 19, or whatever. If the sum of the two digits with the same index is above 10 then you need to determine the placeholder and the carry. Obviously, the carry with index == zero is zero, but it may be either zero or 1 for every other set of indexes.

If you don't know how to get an int from a char take a look at this:

char ch = '6';
int a = ch - '0';

also what happens if either the augend or the addend have fewer than 20 digits. what if one has 9 and the other 17? It's not too hard to figure out what you can do to accomodate this scenario. So give it a try and try to work in there once you get the adding and carry bit worked out.

Yeah thats definately one of the problems you got..geting int from char..Im doin the same thing, but in C...Jus starting, geting that we also use (Atoi) in 'C' jus search how its used...think al also need help will b back :) Gud Luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.