Large ints/ functions

This won't compile, it won't recognize any of the math operators?

Also, how do I know if the sum is larger than 20 digits? I'm supposed to output an error message if it is larger than 20 digits.

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
  
#include <iostream>
#include <string>


using namespace std;

void readNum (string, string);
void sumNum (int, int);

int main(int argc, char** argv) {
	
	string firstNum;
	string secondNum;
	
	cout << "Please enter an integer: ";
	cin >> firstNum;
	cout << "Please enter another integer: ";
	cin >> secondNum;
	
	readNum(firstNum, secondNum);

	return 0;
}

void readNum (string num1, string num2)
{
	int maxSize = 20;
	int array1 [maxSize];
	int array2 [maxSize];
	
	int rev1 = 0;
	int rev2 = 0;
	
	for (int i = 0; i < maxSize; i++)
	{
	
    //	while (num1)
    //	{
        	rev1 *= 10;
       		rev1 += (num1 % 10);
        	num1 /= num 10;
   	   // }
   	 	array1 [i] = rev1;
   	 }
   	 
   	for (int i = 0; i < maxSize; i++)
	{
	
    	while (num2)
    	{
        	rev2 *= 10;
       		rev2 += num2 % 10;
        	num2 /= 10;
   	 	}
   	 	array1 [i] = rev2;
   	 }
   	 
   	 	sumNum(array1[maxSize], array2[maxSize]);

}


void sumNum (int num1[20], int num2[20])
{
	int maxSize = 20;
	int sum[maxSize];
	int index;
	
	
	for (int j = 0; j < maxSize; j++)
	{
		sum[j] = num1[j] + num2[j];
	}
	
	for (int k = 0; k < maxSize; k++)
	{
		cout << sum[k];
	}
}


Last edited on
In your ReadNum() function, you can't do math on string types. You will have to convert the string to some kind of number. There are lots of C and C++ ways to do this. Look into atoi() and stringstream.
Okay I have converted the strings to ints (I think?)

Now my compiler is saying "undefined reference to sumNum
Id returned 1 exit status"

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

#include <iostream>
#include <string>
#include <sstream>


using namespace std;

void readNum (string, string);
void sumNum (int, int);

int main(int argc, char** argv) {
	
	string firstNum;
	string secondNum;
	
	cout << "Please enter an integer: ";
	cin >> firstNum;
	cout << "Please enter another integer: ";
	cin >> secondNum;
	
	readNum(firstNum, secondNum);

	return 0;
}

void readNum (string firstNum, string secondNum)
{
	int maxSize = 20;
	int array1 [maxSize];
	int array2 [maxSize];
	
	int rev1 = 0;
	int rev2 = 0;
	
    int num1, num2;

    istringstream(firstNum) >> num1;
    istringstream(secondNum) >> num2;
    
	
	for (int i = 0; i < maxSize; i++)
	{
		while (num1)
    	{ 
        	rev1 *= 10;
       		rev1 += num1 % 10;
        	num1 /=  10;
   	 		array1 [i] = rev1;
   	 	}
   	 }
   	 
   	for (int i = 0; i < maxSize; i++)
	{
	
    	while (num2)
    	{
        	rev2 *= 10;
       		rev2 += num2 % 10;
        	num2 /= 10;
   	   }
   	 	array1 [i] = rev2;
   	 }
   	 
   	 	sumNum(array1[maxSize], array2[maxSize]);

}


void sumNum (int num1[20], int num2[20])
{
	int maxSize = 20;
	int sum[maxSize];
	int index;
	
	
	for (int j = 0; j < maxSize; j++)
	{
		sum[j] = num1[j] + num2[j];
	}
	
	for (int k = 0; k < maxSize; k++)
	{
		cout << sum[k];
	}
}
closed account (iAk3T05o)
What's istringstream () . Shouldn't it be stringstream () ?
Typically, the point of large ints is that they handle numbers larger than the computer can. As a result, the normal mathematical operators don't work on them... You have to write them yourself.

It is actually pretty easy to do... Presuming you remember your basic maths. For example, how do you solve

  123
+ 294
-----

?

In your program, the "numbers" are actuall strings of digits: "123" and "294".


Also, be careful how you name things. The "readNum" function does not actually 'read' anything. (I'm not actually sure what it is supposed to be doing.)

Good luck!

[edit]Fixed iPad-evil-"autocorrect"-typo.
Last edited on
Thank you!
I rewrote my code to this, but my sum isn't calculating correctly..?

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
#include <iostream>

 using namespace std;


 const int maxSize = 20;

 void readIn (int num1[], int& sizeNum1); 
 void printOut(int num1[], int sizeNum1); 
 void add(int num1[], int sizeNum1, int num2[], int sizeNum2, int sum[], int & sumSize); 
 
 int main()
 {
 
 	int num1[maxSize], num2[maxSize], sum[maxSize];
 
 	int sizeNum1, sizeNum2, sumSize;
 

 	for(int i = 0; i < maxSize; i++)
 	{
 		num1[i] = 0;
 		num2[i] = 0;
 		sum[i] = 0;
 	}

 

 		readIn(num1, sizeNum1);
 		readIn(num2, sizeNum2);
 		add(num1, sizeNum1, num2, sizeNum2, sum, sumSize);
 
 		cout << "The sum of ";
 		printOut(num1, sizeNum1);
 		cout << " and ";
 		printOut(num2, sizeNum2);
 		cout << " is ";
 		printOut(sum, sumSize);
 		cout << endl;
 

 	return 0;
 }


 
 void readIn(int num1[], int& sizeNum1)
 {
 	char digit[maxSize];
 	char convert;
 	int index = 0;
 	cout << "Please enter an integer up to 20 digits: ";
	cin.get(convert);
	
 	if (convert == '\n')
 	{
 		cin.get(convert);
 	}
 	
 	while (isdigit(convert) && index < maxSize)
 	{
 		digit[index] = convert;
 		index++;
 		cin.get(convert);
 	}

 	sizeNum1 = index;
 	int jIndex = 0;

 
 	while (index > 0)
 	{
 		index--;
 		num1[jIndex] = digit[index] - '0';
 		jIndex++;
	 }

 }

 
void printOut(int num1[], int sizeNum1)
{
 	for (int i = 0; i < sizeNum1; i++)
 	cout << num1[sizeNum1 - i - 1];
}

 void add(int num1[], int sizeNum1, int num2[], int sizeNum2, int sum[], int & sumSize)
 {
 	int index;
 	for(index = 0; index < maxSize - 1; index++)
 	{
 		sum[index] = (num1[index] + num2[index]) % 10;
 		sum[index + 1] = (num1[index] + num2[index]) / 10;
 		index++;
 	}
 	sumSize = index;

 	if ((num1[index] + num2[index]) /20 > 0)
 	{
 		cout << "Sum is too large. " << endl;
 	}
 		
}
Last edited on
Lines 90 and 94: Look at what you are doing to index.

Line 93: You are correctly calculating the carry, but what happens to sum[index + 1] on the next iteration of the loop (line 92)?

Keep it in a separate variable.

89
90
int index;
int carry = 0;

Don't forget to add the carry to the calculation on line 92.

Finally, when you are done and you are ready to check for overflow (line 98), all you have to do is check to see if index == maxSize and carry != 0.

Hope this helps.
Topic archived. No new replies allowed.