SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Arrays
Exercise:
Programming Projects
Question:5 | ISBN:9780132846813 | Edition: 5

Question

An array can be used to store large integers one digit at a time. For example, the integer 1234 could be stored in the array a by setting a[0] to 1 , a[1] to 2 , a[2] to 3 , and a[3] to 4 . However, for this exercise you might find it more useful to store the digits backward, that is, place 4 in a[0] , 3 in a[1] , 2 in a[2] , and 1 in a[3] . In this exercise you will write a program that reads in two positive integers that are 20 or fewer digits in length and then outputs the sum of the two numbers. Your program will read the digits as values of type char so that the number 1234 is read as the four characters '1' , '2' , '3' , and '4'. After they are read into the program, the characters are changed to values of type int . The digits will be read into a partially filled array, and you might find it useful to reverse the order of the elements in the array after the array is filled with data from the keyboard. (Whether or not you reverse the order of the elements in the array is up to you. It can be done either way, and each way has its advantages and disadvantages.) Your program will perform the addition by implementing the usual paper-and-pencil addition algorithm. The result of the addition is stored in an array of size 20 and the result is then written to the screen. If the result of the addition is an integer with more than the maximum number of digits (that is, more than 20 digits), then your program should issue a message saying that it has encountered “integer overflow.” You should be able to change the maximum length of the integers by changing only one globally defined constant. Include a loop that allows the user to continue to do more additons until the user says the program should end.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:
 

#include <iostream>
using namespace std;

const int MAX_SIZE = 20;

void readNumber(int number[], int &count)
{
	char ch;

	cin.get(ch);
	while (ch != '\n' && count < MAX_SIZE)
	{
		number[count] = ch - '0';
		count++;

		cin.get(ch);
	}
}

void reverseDigits(int number[], int count)
{
	int i = 0;
	int j = count - 1;

	while (i < j)
	{
		int temp = number[i];
		number[i] = number[j];
		number[j] = temp;

		i++;
		j--;
	}
}

bool addNumbers(int number1[], int count1, int number2[], int count2,
		int result[], int &size)
{
	int sum = 0;
	int carry = 0;
	int i = 0;
	int j = 0;

	while (i < count1 && j < count2 && size < MAX_SIZE)
	{
		sum = number1[i] + number2[j] + carry;

		if (sum > 9)
		{
			carry = sum / 10;
			sum = sum % 10;
		}
		else
		{
			carry = 0;
		}

		result[size] = sum;
		size++;
		i++;
		j++;

		if(carry != 0 && size == MAX_SIZE)
			return false;
	}

	while (i < count1 && size < MAX_SIZE)
	{
		sum = number1[i] + carry;

		if (sum > 9)
		{
			carry = sum / 10;
			sum = sum % 10;
		}
		else
		{
			carry = 0;
		}

		result[size] = sum;
		size++;
		i++;

		if(carry != 0 && size == MAX_SIZE)
			return false;
	}

	while (j < count2 && size < MAX_SIZE)
	{
		sum = number2[j] + carry;

		if (sum > 9)
		{
			carry = sum / 10;
			sum = sum % 10;
		}
		else
		{
			carry = 0;
		}

		result[size] = sum;
		size++;
		j++;

		if(carry != 0 && size == MAX_SIZE)
			return false;
	}

	if (carry > 0)
	{
		result[size] = carry;
		size++;
	}

	return true;
}

void printNumber(int number[], int count)
{
	for (int i = count - 1; i >= 0; i--)
	{
		cout << number[i];
	}
}

int main()
{
	int number1[MAX_SIZE];
	int number2[MAX_SIZE];
	int result[MAX_SIZE];
	int count1 = 0;
	int count2 = 0;
	int size = 0;

	cout << "Enter the first number:  ";
	readNumber(number1, count1);

	cout << "Enter the second number: ";
	readNumber(number2, count2);

	reverseDigits(number1, count1);
	reverseDigits(number2, count2);

	bool success = addNumbers(number1, count1, number2, count2, result, size);

	if(success)
	{
		cout << endl;
		printNumber(number1, count1);
		cout << " + ";
		printNumber(number2, count2);
		cout << " = ";
		printNumber(result, size);
		cout << endl;
	}
	else
	{
		cout << "integer overflow" << endl;
	}

	return 0;
}

Output:
 

Enter the first number:  369874521478
Enter the second number: 963215874

369874521478 + 963215874 = 370837737352

 

0 0

Discussions

Post the discussion to improve the above solution.