Adding 2 large integers as a list

I'm supposed to write a program that would add 2 integers of up to 300 digits. I implemented the use of an array list with a header file, but I keep getting error messages that I can't figure out. Can anyone offer some assistance?

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
template <class elemType>
class arrayListType
{
public:
	const arrayListType<elemType>& operator= (const arrayListType<elemType>&); //assignment op

	void insert(const elemType& insertItem); //insert element at the end of the list

	arrayListType<elemType>& operator+(const arrayListType<elemType>&) const; //addition op

	arrayListType(int size = 300); //constructor

protected:
	elemType *list; //array to hold elements
};

template <class elemType>
const arrayListType<elemType>& arrayListType<elemType>::operator=(const arrayListType<elemType>&)
{
	if (this != &otherList)
	{
		delete [] list;
		maxSize = otherList.maxSize;
		length = otherList.length;

		list = new elemType[maxSize];
		assert(list != NULL); //terminate program if memory cannot be allocated

		for(int = 0; i < length; i++)
		{
			list[i] = otherList.list[i];
		}
	}
	
	return *this;
}


template <class elemType>
void arrayListType<elemType>::insert(const elemType& insertItem)
{
	int loc;
	if (length == 0)
		list[length++] = insertItem;
	else if (length == maxSize)
		cerr << "Enough numbers have been entered." << end; 
}

template <class elemType>
arrayListType<elemType>& arrayListType<elemType>::operator+(const arrayListType<elemType>& total) const
{
	arrayListType sumTotal;
	
	sumTotal = int1[i] + int2[i];

	return sumTotal;
}



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
#include <iostream>
#include <iostream>
#include "arrayListType.h"

using namespace std;

int main()
{
	arrayListType<int> int1, int2;
	int total [300];
	
	int number; 
	cout << "Enter the first line of digits:" << endl;
	cin >> number;

	for (int i = 0; i < 20 ; i++)
	{
		cin >> number;
		int1.insert(number);
	}
	
	int number2;

	cout << "Enter the second line of digits:" << endl;
	cin >> number2;
	
	for (int i = 0; i < 20 ; i++)
	{
		cin >> number2;
		int2.insert(number2);
	}
	
	int carryOver = 0;
	int num = 0;

for (int i = 9; i>= 0; i--)
	{
		number = int1. + int2[i];
		if (num > 9)
		{
			num -= 10;
			
			total[i] = num + carryOver;
			carryOver = 1;
		}
		else
		{
			total[i] = num + carryOver;
			carryOver = 0;
		}
	
	for (int i = 0; i < 10; i++)
	{
		cout << total[i]; 
	}
	return 0;
	}
}
Last edited on
Topic archived. No new replies allowed.