#include<iostream>
#include<cstddef>

using namespace std;
typedef int* IntArrayPtr;
void getSize(int& size1, int& size2);//Function declaration for getSize
void createArrays(int& size1, int& size2);//Function declaration for createArrays
void fillArrays(int& size1, int& size2);//Function declaration for fillArrays


int main()
{

     int size1;//Variable that will be passed from one function to another
     int size2;//Variable that will be passed from one function to another
     getSize(size1, size2);//Funcion call for getSize
     createArrays(size1, size2);//Function call for createArrays
     fillArrays(size1, size2);//Function call for fillArrays

  return 0;
}

void getSize(int& size1, int& size2)//Function definition for getSize
{
     cout << "\nEnter the size of the first number to be added:" << endl;//Asks user to input the size of the number( number of digits) to use to create a dynamic array
     cin  >> size1;//Input from user
     //cout << size1 << endl;//Test to show input is correct
     
     cout << "\nEnter the size of the second number to be added:" << endl;//Asks user to input the size of the number( number of digits) to use to create a dynamic array
     cin  >> size2;//Input from user
     //cout << size2 << endl;//Test to show input is correct
}

void createArrays(int& size1, int& size2)//Function definition for createArray
{
     int* a = NULL;//Sets pointer a to NULL 

     a = new int[size1];//Sets pointer a equal to new int[size1] where the size is that of the number entered by the user
     for ( int i = 0; i < size1; i++ )
     {
          a[i] = 0;//Fills all elements to zero.
          cout << a[i];//Test to outpt a[i] to make sure array is filled with 0's
     }
     cout << endl;


     int* b = NULL;//Sets pointer b to NULL 

     b = new int[size2];//Sets pointer b equal to new int[size2] where the size is that of the number entered by the user
     for ( int i = 0; i < size2; i++ ) 
     {
          b[i] = 0;// Fills all elements to zero.
	      cout << b[i];//Test to outpt b[i] to make sure array is filled with 0's
     }
     cout << endl;


}

void fillArrays(int& size1, int& size2)//Function definition for fillArrays
{
     int input1, input2;//Local variables that may be moved to a global position
     
     cout << "\nPlease enter your first non-negative " << size1 << " digit number to add: " << endl;//Prompt asking the user for their non-negative number of the size they previously entered
     cin >> input1;//Input from user
     //cout << input2 << endl;//Test to make sure data entered is correct

     cout << "\nPlease enter your second non-negative " << size2 << " digit number to add: " << endl;
     cin  >> input2;//Input from user
     //cout << input2 << endl;//Test to make sure data entered is correct
}

The problem works as such: The user enters the size of the array that will be created. I have this part worked out and passed the variable to the function that will create the array. Then the array pointer should be passed to the function that will ask the user for his two number that he wants to add together. This is where I am having the difficulties, I am trying to pass the pointer to the dynamic array and everything I have bee trying is not working. I thought I would ask for some help to see if I can be able to get this to work the way it is suppose to.

Within main, call getSize() and after that use the 2 values to establish the dynamic arrays in main(). Pass the arrays and the sizes into createArrays, and initialize them to zero there. Then pass the arrays and sizes into fillArrays.

Ok, I moved the code to establish the dynamic arrays after getSize(). For some reason my brain is becoming stuck in slow motion I can't figure out the warning that the compiler is giving me. I'm pretty sure it is something simple and easy to fix but is not clear to me at the moment.

Here are the errors:

In function `int main()':
cannot convert `int**' to `int*' for argument `1' to `void createArrays(int*, int&, int*, int&)'
cannot convert `int**' to `int*' for argument `1' to `void fillArrays(int*, int&, int*, int&)'

int size1;//Variable that will be passed from one function to another
     int size2;//Variable that will be passed from one function to another
     getSize(size1, size2);//Funcion call for getSize
     IntArrayPtr a[size1];
     for ( int i = 0; i < size1; i++ )
     {
          a[i] = new int[size1];//Sets pointer a equal to new int[size1] where the size is that of the number entered by the user
     }
     IntArrayPtr b[size2];
     for ( int j = 0; j < size2; j++ )
     {
          b[j] = new int[size2];//Sets pointer b equal to new int[size2] where the size is that of the number entered by the user
     }
     createArrays(a, b, size1, size2);//Function call for createArrays
     fillArrays(a, b, size1, size2);//Function call for fillArrays

Edited 6 Years Ago by jmcorpse: n/a

On line 8 and 13, you are creating #size2 sub arrays each of size2. When you are passing it into the function, the compiler is interpreting it as a int ** (i.e., a 2D array). Don't do the loop, just initialize a and b as new int[size2] .

Like this?

int *a = new int[size2];;//Sets pointer a equal to new int[size1] where the size is that of the number entered by the user
     int *b = new int[size2];//Sets pointer b equal to new int[size2] where the size is that of the number entered by the user

It compiles and then when I run in it. The console freezes and shuts down.

Ok, I figured out the problem with it crashing. It was coming from how I was intializing a and b to zero, so I removed it and it worked properly. Would I set them to zero like an array or how you set them to zero using the int* a = Null?

#include<iostream>
#include<cstddef>

using namespace std;
typedef int* IntArrayPtr;
void getSize(int& size1, int& size2);//Function declaration for getSize
void createArrays(int a[], int b[], int& size1, int& size2);//Function declaration for createArrays
void fillArrays(int a[], int b[], int& size1, int& size2);//Function declaration for fillArrays


int main()
{

     int size1;//Variable that will be passed from one function to another
     int size2;//Variable that will be passed from one function to another

     getSize(size1, size2);//Funcion call for getSize

     int *a = new int[size1];;//Sets pointer a equal to new int[size1] where the size is that of the number entered by the user
     int *b = new int[size2];//Sets pointer b equal to new int[size2] where the size is that of the number entered by the user

     createArrays(a, b, size1, size2);//Function call for createArrays
     fillArrays(a, b, size1, size2);//Function call for fillArrays

  return 0;
}

void getSize(int& size1, int& size2)//Function definition for getSize
{
     cout << "\nEnter the size of the first number to be added:" << endl;//Asks user to input the size of the number( number of digits) to use to create a dynamic array
     cin  >> size1;//Input from user
     //cout << size1 << endl;//Test to show input is correct
     
     cout << "\nEnter the size of the second number to be added:" << endl;//Asks user to input the size of the number( number of digits) to use to create a dynamic array
     cin  >> size2;//Input from user
     //cout << size2 << endl;//Test to show input is correct
}

void createArrays(int a[], int b[], int& size1, int& size2)//Function definition for createArray
{

     for ( int i = 0; i < size1; i++ )
     {
          a[i] = 0;//Fills all elements to zero.
          cout << a[i];//Test to outpt a[i] to make sure array is filled with 0's
     }
     cout << endl;

     for ( int i = 0; i < size2; i++ ) 
     {
          b[i] = 0;// Fills all elements to zero.
	      cout << b[i];//Test to outpt b[i] to make sure array is filled with 0's
     }
     cout << endl;

}

void fillArrays(int a[], int b[], int& size1, int& size2)//Function definition for fillArrays
{
     int input1, input2;//Local variables that may be moved to a global position

     cout << "\nPlease enter your first non-negative " << size1 << " digit number to add: " << endl;//Prompt asking the user for their non-negative number of the size they previously entered
     cin >> input1;//Input from user
     //cout << input2 << endl;//Test to make sure data entered is correct

     cout << "\nPlease enter your second non-negative " << size2 << " digit number to add: " << endl;
     cin  >> input2;//Input from user
     //cout << input2 << endl;//Test to make sure data entered is correct
          
}

Don't worry about initializing them to NULL if you're just going to turn around and initialize them with new one step later, just do it all in one step like you did.

To set them to zero, do what you did within your function. If they were static you could do it all at once, like int a[5] = {0}; but they are not.

I fixed all the problems from earlier now I just have some minor issues that I can't seem to iron out. The math portion of my program works, what I am having trouble with is carrying over the number and creating the next column. For example 9 + 1 = 10 that would mean 0 in the ones column and 1 in the tens column. I can't get the program to do this when it reaches the first number in the number if it is greater than ten. And also if the first number that was entered by the user is larger than the second I think I need to pad the second number to the right so it will add properly and I do not know how to do make that happen. I could sure use some advice.

#include<iostream>
#include<iomanip>
#include<cstddef>
#include<cmath>


using namespace std;
typedef int* IntArrayPtr;
const int size3 = 20;
const int TEN = 10;//Constant for the number10
const int ASCII = 48;//Constant for the ASCII character 0
const int ASCII9 = 57;//Constant for the ASCII character 9
void getSize(int& size1, int& size2);//Function declaration for getSize
void fillZero(int total[], int sum[], int& size1, int& size2);
void createArrays(int a[], int b[], int& size1, int& size2);//Function declaration for createArrays
void fillArray1(int a[], int& size1, int conInput1[]);//Function declaration for fillArray1
void fillArray2(int b[], int& size2, int conInput2[]);//Function declaration for fillArray2
void addition(int conInput1[], int conInput2[], int total[], int sum[], int& size1);//Function declaration for addition
void carry(int total[], int sum[], int& size1);//Function declaration for the carry function
void modInput(int total[], int& size1);//Function declaration for the modInput function
void printResults(int conInput1[], int conInput2[], int total[], int sum[], int& size1, int& size2);//Function declaration for printResults
char repeat();//Function declaration to repeat program



int main()
{

     int size1;//Variable that will be passed from one function to another
     int size2;//Variable that will be passed from one function to another
     getSize(size1, size2);//Funcion call for getSize
     int conInput1[size1];
     int conInput2[size2];
     int total[size1];
     int sum[size1];
     char ans;
     int *a = new int[size1];//Sets pointer a equal to new int[size1] where the size is that of the number entered by the user
     int *b = new int[size2];//Sets pointer b equal to new int[size2] where the size is that of the number entered by the user
     int *c = new int[size3];//Sets pointer c equal to new int[size3] where the size is the total of 

     createArrays(a, b, size1, size2);//Function call for createArrays
     fillZero(total, sum, size1, size2);
     fillArray1(a, size1, conInput1);//Function call for fillArray1
     fillArray2(b, size2, conInput2);//Function call for fillArray2
     addition(conInput1, conInput2, total, sum, size1);//Function call for addition
     printResults(conInput1, conInput2, total, sum, size1, size2);//Function call for printResults
     ans = repeat();//Calls the repeat function
     
     return 0;
}

void getSize(int& size1, int& size2)//Function definition for getSize
{
     cout << "\nEnter the size of the first number to be added:" << endl;//Asks user to input the size of the number( number of digits) to use to create a dynamic array
     cin  >> size1;//Input from user
     //cout << size1 << endl;//Test to show input is correct
     
     cout << "\nEnter the size of the second number to be added:" << endl;//Asks user to input the size of the number( number of digits) to use to create a dynamic array
     cin  >> size2;//Input from user
     //cout << size2 << endl;//Test to show input is correct
}

void createArrays(int a[], int b[], int& size1, int& size2)//Function definition for createArray
{

     for ( int i = 0; i < size1; i++ )
     {
          a[i] = 0;//Fills all elements to zero.
          //cout << a[i];//Test to outpt a[i] to make sure array is filled with 0's
     }
     cout << endl;

     for ( int i = 0; i < size2; i++ ) 
     {
          b[i] = 0;// Fills all elements to zero.
	      //cout << b[i];//Test to outpt b[i] to make sure array is filled with 0's
     }
     cout << endl;

}

void fillZero(int total[], int sum[], int& size1, int& size2)
{

     for ( int i = 0; i < size1; i++ )//For loop used to count up the numbers to the maximum number
     {
         //input1[size1] = 0;
         total[size1] = 0;
         sum[size1] = 0;
     }
     
}

void fillArray1(int a[], int& size1, int conInput1[])//Function definition for fillArray1
{

     int count = 0;//Sets variable count to 0

     char c;
     //Output used to instruct user to input data
     cout << "\nPlease enter the first non-negative " << size1 << " or less digit integer." << endl;
          
          
     for( int i = 0; i < size1; i++ )//For loop used to count up the numbers to the maximum number
     a[i] = '\0';//Sets a[i] to an empty string

     while (c != '.' && count < size1)//Check used to make sure no decimals are entered and the # of numbers is less than 20
     {
          cin >> c;//Reads the data into the array from the user
          if(c >= ASCII && c <= ASCII9)//Check to make sure the numbers entered are correct
          {
               a[count] = c;//Fills the input1 array
               count++;//Iterates the numbers up to the end of the array
          
          }
     //a[count]='\0';//Used to terminate the string
     }
     for ( int i = 0; i < size1; i++ )
     {
          //cout << "a[i]= " << input1[i];
          conInput1[i] = (a[i] - ASCII);//Converts the char(input1[i]) array into int conInput1[i] array by
          //cout << "conInput1[i]= " << conInput1[i];
     } 
     //cout << endl;
     //cout << "conInput1[1]= " << conInput1[0] << endl;
     //cout << "conInput1[2]= " << conInput1[1] << endl;
     //cout << "conInput1[3]= " << conInput1[2] << endl;
     //cout << "conInput1[4]= " << conInput1[3] << endl;
     //cout << "conInput1[5]= " << conInput1[4] << endl;  
}

void fillArray2(int b[], int& size2, int conInput2[])//Function definition for fillArray2
{
     int count = 0;//Sets variable count to 0
     char d;
     //Output used to instruct user to input data

     cout << "\nPlease enter the first non-negative " << size2 << " or less digit integer. "<< endl;
          
     for( int j = 0; j < size2; j++ )//For loop used to count up the numbers to the maximum number
     b[j] = '\0';//Sets b[j] to an empty string

     while (d != '.' && count < size2)//Check used to make sure no decimals are entered and the # of numbers is less than 20
     {
          cin >> d;//Reads the data into the array from the user
          if(d >= ASCII && d <= ASCII9)//Check to make sure the numbers entered are correct
          {
               b[count] = d;//Fills the input1 array
               count++;//Iterates the numbers up to the end of the array

          }
     //input2[count]='\0';//Used to terminate the string
     }
     for ( int j = 0; j < size2; j++ )
     {
          //cout << "b[j]= " << input2[j];
          conInput2[j] = (b[j] - ASCII);//Converts the char(input1[i]) array into int conInput2[j] array by
          //cout << "conInput2[j] = " << conInput2[j];
     } 
     //cout << endl;
     //cout << "conInput2[1]= " << conInput2[0] << endl;
     //cout << "conInput2[2]= " << conInput2[1] << endl;
     //cout << "conInput2[3]= " << conInput2[2] << endl;
     //cout << "conInput2[4]= " << conInput2[3] << endl;
     //cout << "conInput2[5]= " << conInput2[4] << endl;    
     
}

void addition(int conInput1[], int conInput2[], int total[], int sum[], int& size1)
{

    for(int i = 0; i < size1; i++)//For loop used to count up the numbers to the maximum number
    {
	     //cout << "conInput1= " << conInput1[i] << "conInput2= " << conInput2[i];
         total[i] = conInput1[i] + conInput2[i];//Adds conInput1 and conInput2 together to give the total to total[i]
         //cout << "total= " << total[i] << endl;//test for total to make sure total has the values
         if( total[i] >= TEN )//If loop to process the math into the carry function and modInput function if total[i] is above 10
         {
              carry(total, sum, size1);//Carry function call
              modInput(total, size1);//modInput function call
         }
         //cout << "total = " << total[i];
    }
         
}

void carry(int total[], int sum[], int& size1)//Carry function definition
{
     
     for(int i = 0; i < size1; i++)//For loop used to count up the numbers to the maximum number
     {
          sum[i] = total[i] / TEN;//Sum is equaled to total[i] divided by 10
          //cout << "divtotal=" << sum[i] << endl;//test for sum[i]
          total[i - 1] = total[i - 1] + sum[i];//Total[i-1] is equal to total[i-1] added to sum[i]. This is used to pass the number on to the previous number.
     }  
          //cout << "sum[0]= " << sum[0] << endl;     
}

void modInput( int total[], int& size1)//modInput function definition
{
     for(int i = 0; i < size1; i++)//For loop used to count up the numbers to the maximum number
     {
          total[i] = total[i] % TEN;//total[i] is equal to total[i] modulus divided by ten. This is used to show only the remainder in the total
          //cout << "modtotal= " << total[i] << endl;//test for total[i] to prove if the information it produces is correct
     }
}

void printResults(int conInput1[], int conInput2[], int total[], int sum[], int& size1, int& size2)//Prints out the results from the calculations
{
     
     cout << endl;
     for ( int i = 0; i < size1; i++ )//For loop used to count up the numbers to the maximum number
     {
          cout << conInput1[i];//Prints out the input entered by the user
     }
     cout << endl;
     for ( int j = 0; j < size2; j++ )//For loop used to count up the numbers to the maximum number
     {
               cout << conInput2[j];//Prints out the input entered by the user
     }
     cout << endl << "----------------------" << endl;//Draws a line used as a seperator bar
     //cout << "sum= " << sum[0] << endl;
     //cout << "sum 1 = " << sum[1] << endl;   
     if ( sum[0] != 1 )
     {
          for ( int i = 0; i < size1; i++ )//For loop used to count up the numbers to the maximum number
          {
               cout << total[i];//Prints out the total produced from the addition of conInput1 and conInput2
          }
     }
     else if ( sum[0] == 1 )
     {
          cout << "\nInteger Overflow Error!" << endl;
     }
}

char repeat()//Repeat function declaration
{
    char c;//local variable
    do
    {
        cout << endl << endl;
        cout << "Do you want to continue? (y for yes, n for no): ";//Asks for the users response whether to continue or not
        cin  >> c;//Input from user
        if (c != 'y' && c != 'n' )//check to make sure input is correct
             cout << "Invalid input, enter y or n" << endl;//informs user if input is not correct
    }while(c != 'y' && c != 'n');

    return c; 
}

I think what you want to be doing, if you are going to use that approach with shifting, is making all of your arrays the same size as the largest one. Say your largest array is 20 and someone enters 4 digits, you need to shift index 3 down to index 19, 2 down to index 18, 1 down to index 17, 0 down to index 16. I'm sure you see the pattern.

If your sum going into the leftmost number is greater than 10, you have overflowed your capacity. So, for the sum make the array 1 longer than the longest item.

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