Big numbers in Arrays

i have to create three arrays. my first array is going to be a 20 digit number if not less. this also goes for my secnd array.


Please enter first number --> 8764
Please enter second number --> 98
00000000000000008764
00000000000000000098


my third array has to be the sum of the first two arrays.


00000000000000008764
00000000000000000098
------------------------------
00000000000000008862


i am unsure on how to get them to add together. should i just add them both and then make an array out of the sum equation?

here is my code so far. my arrays are not showing up when i run it also..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>

const int Size = 20;
int CreateArrays (int Digit, int DigitArray[Size]);
void DisplayArrayEquation ( int Array1[Size], int Array2[Size], int ArraySum[Size]);

using namespace std;

int main()

{

	int Digit1, Digit2, DigitSum;
	char Continue;
	int Array1[Size], Array2[Size], ArraySum[Size];

	do
	{
		cout << "\n\nPlease enter first number --> ";
		Digit1 = cin.get();
		CreateArrays (Digit1, Array1);

		cout << "\n\nPlease enter second number --> ";
		Digit2 = cin.get();
		CreateArrays (Digit2, Array2);

		DigitSum = Digit1 + Digit2;
		DigitSum = cin.get();
		CreateArrays (DigitSum, ArraySum);

		DisplayArrayEquation ( Array1, Array2, ArraySum);

		cout << "\n\nContinue? Y or N    ";
		cin >> Continue;
	}
		while ( Continue != 'n' && Continue != 'N');
}
int CreateArrays (int Digit, int DigitArray[Size])
{
    
	int T[20];
	int A[20];
	int i = 0, k;

        for (i = 0; i < Size; i++)
    {
		A[i]=0;
		T[i]=0;
    }
        
		i = 0;
        
		while (Digit != '\n' && i < Size)
		{
			T[i] = Digit - '0';
			++i;
			Digit = cin.get();
		}
		for ( k = Size - 1, --i; i >= 0 ; --i, --k)
		{
			A[k] = T[i];
		}

		DigitArray[Size] = A[k];

		return DigitArray[Size];
}
void DisplayArrayEquation (int A1[Size], int A2[Size], int ASum[Size])
{
	int i = 0;

		cout << endl;
		for (i = 0; i < Size; i++)
		{
            cout << A1[i];
		}

		cout << endl;
		for (i = 0; i < Size; i++)
		{
            cout << A2[i];
		}

		cout << "\n----------------------";

		cout << endl;
		for (i = 0; i < Size; i++)
		{
            cout << ASum[i];
		}


}
Last edited on
In your CreateArrays function you are not creating anything at all.

There was the same question before:
http://www.cplusplus.com/forum/general/128732/
MiiNiPaa i am still fairly new to programming and do not understand strings or bools.

in my code i have an error
'DisplayArrayEquation': identifier not found

on line 31

this is my output...



Please enter first number --> 123


Please enter second number --> 12321


Continue? Y or N    N


Please enter first number -->

Please enter second number -->



my loop is not even working correctly.. i am trying to get the arrays to show first though
while ( Continue != 'n' || Continue != 'N'); Will continue to iterate while your character is not equals to 'n' or not equals to 'N'. As it cannot be equal to both, at least one part will evaluate to true therefore all expression will be true. You probably want && here.

'DisplayArrayEquation': identifier not found That is because ypou do not have DisplayArrayEquation function declared before. But you have DisplayArrays which is decalred but not defined.

do not understand strings or bools.
But you use them extensivly in your code :)
im trying to have my outpur like this..


Please enter first number --> 8764
Please enter second number --> 98
00000000000000001234
00000000000000000012
------------------------------
00000000000000001246
Continue Y or N -->


i have changed the problem and now my output looks like this....



Please enter first number --> 123456


Please enter second number --> 123654


-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460
-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460
-858993460-858993460-858993460-858993460
-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460
-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460
-858993460-858993460-858993460-858993460
----------------------
-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460
-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460
-858993460-858993460-858993460-858993460

Continue? Y or N


Last edited on
int CreateArrays (int Digit, int DigitArray); Promising to write a function which takes int as a second parameter.

int CreateArrays (int Digit, int DigitArray[Size]) Writing one which takes an array of ints as parameter.

CreateArrays (Digit1, Array1[Size]);Calling fist variant (wwhich was never implemented) and trying to access out of bounds.

If you want to pass pointer to array, yo need to pass its name as parameter: CreateArrays (Digit1, Array1);

ok i am running debugger to try and figure out where these numbers are coming from.
MiiNiPaa wrote:
In your CreateArrays function you are not creating anything at all.
So your arrays are still uninitializated and contains some garbage values
i thought i created array? it shows that A is an array with the values i enter. however it is gone when i leave the creat functon. i return A and it is gone?
1) you do not return an array, you return integer.
2) Actually C++ does not allows you to return an array. Maximum you can return a pointer
3) all stack allocated variables (like A) are destroyed on function end. You should modify DigitArray directly.
Arrays are always passed by reference, so you only need to pass them as usual to your function.

1
2
const int NDIGITS = 20;
typedef int bignum_t[ NDIGITS ];
1
2
3
4
5
6
7
8
9
10
11
void add_bignum( bignum_t a, bignum_t b, bignum_t result )
{
  int carry = 0;
  for (int n = 0; n < NDIGITS; n++)
//OR
  for (int n = NDIGITS; n--; )
  {
    result[ n ] = a[ n ] + b[ n ] + carry;
    carry = (check for overflow);
  }
}

I don't know what you intend to store as digits in your bignum, so the method of making sure that result[ n ] has a valid value and that you calculate the carry correctly (which will always be either 0 or 1).

Remember your gradeschool addition. The choice of n increasing or decreasing depends on how exactly you store the digits in your bignum. I tend to prefer storing the least-significant first (so use the first loop). But it is not unheard of to store the most-significant first (so use the second loop).

A similar issue with passing arrays (but with "matrices") is here: http://www.cplusplus.com/forum/general/129440/#msg699057

Hope this helps.
How can i call fin.get through a function? i want the user to input number and then the program calls a function to assign integer to integer array. how can this be done? my con.get is canceled out when function is called.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include<iostream>
using namespace std;
const int Size = 20;
int Array1[Size], Array2[Size], ArraySum[Size];
int TempN[20], TempM[20];

void CreateArray (int A[], int Digit);
void AddInteger(int N[], int M[]);

int main()
{
    
	int Digit1, Digit2;
	char Answer;
    
	do
	{
        
        cout << "Please enter first number --> ";
        Digit1 = cin.get();
        CreateArray (Array1, Digit1);
        
		
		cout << "Please enter second number --> ";
		Digit2 = cin.get();
        CreateArray (Array2, Digit2);
        
        AddInteger(Array1,Array2);
        
		cout << endl;
		cout << "Continue? 'Y' or 'N'  --> ";
		cin >> Answer;
		cin.ignore();
	}
	while (Answer == 'y');
    
}

/******************************* AddInteger ****************************************
 Description: this function takes in 2 user entered numbers and adds them togather
 and displays the output of adding the 2 numbers together.
 Parameters:
 IN: takes in 2 Numbers by reference via a number array
 OUT: the sum of the 2 numbers taken in
 Returns: nothing
 Preconditions: must be integers
 ---------------------------------------------------------------------------------*/
void AddInteger(int A1[], int A2[])
{
	int Temp = 0;
	for (int i = 19; i >= 0; i--)
	{
		Temp = A1[i] + A2[i] + Temp;
		ArraySum[i] = Temp % 10;
		Temp = Temp / 10;
	}
    
	for (int i = 0; i < Size; i++)
	{
		cout << A1[i];
        
	}
	cout << endl;
    
	for (int i = 0; i < Size; i++)
	{
		cout << A2[i];
	}
	cout << endl;
    
	for (int i = 0; i < Size; i++)
	{
		cout << "-";
	}
	cout << endl;
    
	for (int i = 0; i < Size; i++)
	{
		cout << ArraySum[i];
	}
	cout << endl;
    
}

/********************************* CreateArray ***********************************
 Description: this function creates an integer array from the number inputed.
 Parameters: single array parameter.
 IN: takes in Numbers by reference.
 Returns: Array with numbers entered.
 Preconditions: must be integers
 ---------------------------------------------------------------------------------*/
void CreateArray ( int A[], int Digit)

{
    int i = 0, k;
    int TempA[Size];
    
    for ( i = 0; i < Size; i++)
    {
        A[i] = 0;
        TempA[i] = 0;
    }
    
    i = 0;
    
    while (Digit != '\n' && i < Size)
    {
        TempA[i] = Digit - '0';
        ++i;
        Digit = cin.get();
    }
    for (k = Size - 1, --i; i >= 0; --i, --k)
    {
        Array1[k] = TempA[i];
    }
    
}
my con.get is canceled out when function is called.

What do you mean? Can you be more specific about what behaviour you're seeing, and how it differs from the behaviour you expect to see?
how can i use cin.get() to obtain the integer that is inputed? i need this cin.get() to then be used in the function CreateArray and create an array with the integer inputed.

1. I want integer inputed.
2. I want function to be called.
3. I want function to create an array with the integer inputed.
4. I want function to return Array with integer in the Array.
5. I want second integer inputed.
6. Redo steps 2-4.
7. I want the Sum of the Arrays to be displayed as follows:


Please enter first number --> 8764
Please enter second number --> 98
00000000000000001234
00000000000000000012
------------------------------
00000000000000001246
Continue Y or N -->

Last edited on
Can you help?
You say in your original post that you want the number to be at most 20 digits long. That won't work with the variable type int, i'm pretty sure. You need a long long for that or a some kind of 2 dimensional array that puts two smaller ints together when doing the math.
the number will be put into an integer array so this should be possible. yes an int type cannot be that long but when...

Digit = cin.get

then the Digit is put into the Array and is 20 digits long. with leading zeros of course if there is not 20 digits.

my problem is that should i use cin.get() from the main function to input numbers into the Array in the function call or do i use it in the function called?
I think it's better practice to not put stuff like that in main. Put it in a function.
Topic archived. No new replies allowed.