Working with large int numbers. Broken up as char for each digit, total of 3 arrays, n1, n2, and sum. I have everything completed, the math works fine, the arrays work fine, the shuffling to the right side of the array works fine. I just have two issues, that both are in the print department:

1. I know that I can use a count variable to right justify these numbers like the professor wants, i just cannot for the life of me figure out how to implement it or how to do it a different way.

2. On the 3rd set and the 6th set of numbers, they stop printing for some reason. Again, I know its something I am doing wrong in the printing method, just not sure what.

my code:

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

ifstream dataIn;
ofstream dataOut;


void printarry(int arg[]);
int zeroArray(int arg[]);
int getNum(int arg[]);
void spacer();
int adder(int arg1[], int arg2[], int arg3[]);


int adder(int arg1[], int arg2[], int arg3[]){
    
    for(int x = 26; arg1[x-1] != 0 && arg2[x-1] != 0; x--){
         int c;
         c = arg1[x-1]+arg2[x-1]+arg3[x-1];
         arg3[x-1] = c%10;
         arg3[x-2] = c/10;
         
    }
   
}

//basic array printer using an array and a lenth as params
void printarray (int arg[]) {
     int x = 0;
     for (int n=0; arg[n] == 0; n++){
         x++;
         }
     for(int n = x; n < 26; n++){
         
         cout << arg[n] << " ";
         }
         cout << "\n";
     
}

int zeroArray (int arg[]) {
    for (int n=0; n<26; n++){
        arg[n] = 0;
    }
}

int getNum(int arg[]){
    char x;
    int i=0;
    
    
    
    dataIn.get(x);
    
    
    while(x != '\n'){
         arg[i] = x-'0';
         i++;
         dataIn.get(x);
         //arg[i] = x-'0';
    }
    
    
}

int shuffle(int arg[]){
    int count = 0;
    int back = 26;
    int digits = 0;
    //get total digits
    for(int i = 26; arg[i-1] == 0; i--){
            count=count+1;
            digits = back - count;
            }
    
    
  
    for(int i = digits; i > 0; i--){
            
            arg[back-1] = arg[i-1];
            arg[i-1] = 0;
            back--;
            }

}

void spacer(){
     cout << "-------------------------------------" << endl;
     }

int main(int argc, char *argv[])
{
    int n1[26], n2[26], sum[26];
    dataIn.open("BigNumbersV2.txt");
    
    for(int x = 6; x > 0; x--){
         zeroArray(n1);
         zeroArray(n2);
         zeroArray(sum);
         getNum(n1);
         getNum(n2);
         shuffle(n1);
         shuffle(n2);
         printarray(n1);
         printarray(n2);
         adder(n1,n2,sum);
         spacer();
         printarray(sum);
    
    cout << endl;
}
    

    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

what my output is looking like currently:

5
7
-------------------------------------
1 2

3 6
8 5
-------------------------------------
1 2 1

3 8 7 8 5 6 3
6 4 2 6
-------------------------------------
1 4 9 8 9

2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7
-------------------------------------
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9

9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
-------------------------------------
1 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 8

4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
-------------------------------------
1 4 4 4 4 4 4 4 4 4 4 4 4 4 4 3

Press any key to continue . . .

Write one more function to return the number of digits in one of the arrays. Have it return the index of the leftmost digit. Call this function on each array and remember the lowest value (the leftmost digit of the 'largest' value).

Pass this value into the print function. You can take it from there.

Consider using a named constant to replace the 'magic number' 26 whic appears at several places in your code.What would you do if you need to modify your program to deal with 100-digit integers tomorrow?

enum { NDIGITS = 26, LAST_DIGIT = NDIGITS-1 } ;

I know that I can use a count variable to right justify these numbers like the professor wants, i just cannot for the life of me figure out how to implement it or how to do it a different way.

Just modify your print function to print a space when leading zeros are encountered.

void printarray( int arg[] )
{
    int i = 0 ;
    for( ; i<LAST_DIGIT && arg[i]==0 ; ++i ) std::cout << ' ' ;
    for( ; i<LAST_DIGIT ; ++i ) std::cout << arg[i] ;
    std::cout << arg[LAST_DIGIT] << '\n' ; // print the last digit even if it is zero
}

If you can determine the number of characters in the largest number, then you can just use std::setw() and std::right to do this:

void printarray( int *arg, const unsigned maxWidth )
{
    for(unsigned i = 0; i < maxWidth; ++i)
        std::cout << std::setw(maxWidth) << std::right << arg[i];
    std::cout << std::endl;
}

std::setw() is in <iomanip> EDIT: OK, just re-read your original post. Since your array is padded with zeros, this method won't work directly, you would have to make a new array that doesn't have these padding zeros. By the time you've done that, you might as well use one of the techniques already posted :)

Just modify your print function to print a space when leading zeros are encountered.

void printarray( int arg[] )
{
    int i = 0 ;
    for( ; i<LAST_DIGIT && arg[i]==0 ; ++i ) std::cout << ' ' ;
    for( ; i<LAST_DIGIT ; ++i ) std::cout << arg[i] ;
    std::cout << arg[LAST_DIGIT] << '\n' ; // print the last digit even if it is zero
}

You should combine this with the solution suggested by WaltP, what if all of your numbers only have 4 or 5 digits? You will have a huge number of spaces before each number. As WaltP says, determine how big the padding needs to be by looking for the minimum amount of padding that will do the job.

You guys are great. I can't thank you enough. This is the second time I have had this professor for an online course and in short, he is awful when it comes to answering questions in a timely fashion. If he assigns a project on monday and makes it due friday night, its very typical of him to not answer his emails until thursday night or friday morning. This project he assigned Saturday night and didn't send an email out for notification. He made it do this last monday. So I'm not even getting credit for it, I just need to figure out the mechanics so I can move on to the multiplication version of it, which is due saturday now. Probably won't make the due date for that one either, but I'm not giving up.

I just noticed a bug in your program that you might like to know about. Take a look at the third example calculation that you gave, your result is:

3 8 7 8 5 6 3
6 4 2 6
-------------------------------------
1 4 9 8 9

This is incorrect, since the answer should be 3884989, not 14989. Actually, 14989 = 6426 + 8563. Your code currently only works for numbers with equal number of digits. Should be quite simple to fix though.

Yes, I posted that there was an issue with that and for the 6th set too. instead of having it try to stop once it reaches the zeros, im just going to have it process all 26 spots, and just worry about taking care of NOT print the leading 0's.

Yes, I posted that there was an issue with that and for the 6th set too. instead of having it try to stop once it reaches the zeros, im just going to have it process all 26 spots, and just worry about taking care of NOT print the leading 0's.

Good choice. You pretty much have to do that anyway, in case you have to add something like: 100001 + 100001, for example.

Once again. Thanks for all the help.
The next project is Multiplying. We are using the same method, only 3 arrays, so all my code can be reused, except for the add needs to be changed to the product.

all of the code that i wrote from the ADD works up until the one thing I change, the adder into a multiplier.

It works fine for the first two sets of numbers. After that, funkyness. I think its because I am not decrementing the K (product array)
in the I (number set 2) loop. When i do try to decrement it, it crashes.

int product(int arg1[], int arg2[], int arg3[]){ \\brings in my 3 arrays

\\i just set up the all the arrays to handle up to 51 digits (52).


      for(int j = 52; j > 1 ; j--){ \\ sets up my second set of numbers as my master loop
           int k = j; \\ sets k to j
           for(int i = 52; i > 1; i--){ \\sets up my first set of numbers as the inner loop
                int p;
               p = (arg2[j-1]*arg1[i-1])+arg3[k-1]; \\takes location j and i, multi and adds to location k
               if(p < 10){
                    arg3[k-1] = p;
                    }
                    else { //adds the numbers, splitting the digits
                              arg3[k-1] = p%10;
                              arg3[k-2] = p/10;
                              }
//then the way this works in my head is: decrement k here, so that when 'i' loops around again, multiplying with the same 'j', it gets placed in the correct k, which is one to the right of the last loop around. When i try to decrement here, it errors out.
               }
      }
}
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.