My main problem right now with this code is trying to get the carry function to work. Also, I know it isn't pretty code. Could someone help me in figuring out why this isn't working the way it is suppose to. The carry function is suppose to add the carry over digit but I can't seem to pass a variable correctly. I've tried everything I could think of but can' seem to do it.

#include<iostream>
#include<cmath>

using namespace std;
const int ARRAY_SIZE = 20;
void fillZero(int conInput1[], int conInput2[], int output[]);
void mod1(int conInput1[], int conInput2[], int output[], int& trans);
void remainder(int conInput1[], int conInput2[], int output[]);
void carry(int conInput1[], int conInput2[], int output[], int& trans);
void addition(int conInput1[], int conInput2[], int output[], int& trans);
void printResults(int conInput1[], int conInput2[], int output[]);


int main()
{
     int conInput1[ARRAY_SIZE];
     int conInput2[ARRAY_SIZE];
     int output[ARRAY_SIZE];
     int trans;
    
     fillZero(conInput1, conInput2, output);
     addition(conInput1, conInput2, output, trans);
     printResults(conInput1, conInput2, output);
return 0;
}
void fillZero(int conInput1[], int conInput2[], int output[])
{
     for ( int i = 0; i < ARRAY_SIZE; i++ )
     {
          conInput1[i] = 9;
          conInput2[i] = 9;
          output[i] = 0;
          /*cout << conInput1[i];
          cout << conInput2[i];
          cout << output[i];*/
     }
}
void mod1(int conInput1[], int conInput2[], int output[], int& trans)
{
     for ( int i = 0; i < ARRAY_SIZE; i++ )
     {
          double val2;
          double val1; 
          double sum = output[i];
          val1 = sum / 10;
          modf(val1, &val2);
          //cout << endl << sum;
          //cout << endl << val1; 
          //cout << endl << &val2 << "..."; 
          //cout << endl << modf(val1, &val2); 
          //cout << x;
     int trans = 0;
     trans = int (&val2);
     //cout << trans << " trans";
     }

}

void remainder(int conInput1[], int conInput2[], int output[])
{
     for ( int i = 0; i < ARRAY_SIZE; i++ )
     {
           int sum1 = 0;
           sum1 = output[i] % 10;
           output[i] = sum1;
           //cout << endl << sum1; 
     }
}

void carry(int conInput1[], int conInput2[], int output[], int& trans)
{
     //cout << trans;
     
     
     for ( int i = 0; i < ARRAY_SIZE; i++ )
     {
          int sum2 = output[i] + trans;
          //cout << sum2;
     }     
}

void addition(int conInput1[], int conInput2[], int output[], int& trans)
{    
     for ( int i = 0; i < ARRAY_SIZE; i++ )
     {
          output[i] = conInput1[i] + conInput2[i];
          //cout << output[i]; 
     
          if (  output[i] > 9 )
          {
               mod1(conInput1, conInput2, output, trans);
               remainder(conInput1, conInput2, output);
               carry(conInput1, conInput2, output, trans);
          }
     }
}

void printResults(int conInput1[], int conInput2[], int output[])
{
     cout << endl << endl;
     for ( int i = 0; i < ARRAY_SIZE; i++  )
     {
          cout << conInput1[i];
     }
     cout << endl;
     for ( int j = 0; j < ARRAY_SIZE; j++  )
     {
          cout << conInput2[j];
     }
     cout << endl << "--------------------" << endl;
     for ( int i = 0; i < ARRAY_SIZE; i++  )
     {
          cout << output[i];
     }
     cout << endl << endl;
}

Edited 6 Years Ago by jmcorpse: n/a

What is trans in your argument? When you do carrying, you need to check whether the number will carry to the next digit or not. You should use while-loop instead of for-loop because you do not know whether the carry will cause the adding through out all digits. For example, 35899 + 4, would cause the carry up to digit '8' and changes it to become '9' (35903). You should see that you aren't supposed to use for-loop because it may or may not carry through the whole number. Also, I'm not sure how you store digit in your int array. Does consInt1[0] is the first or last digit in a number?

Edited 6 Years Ago by Taywin: n/a

This article has been dead for over six months. Start a new discussion instead.