C7PJ7 Please Code in C++ Additional Notes(from my class): -No global variables m
ID: 3828835 • Letter: C
Question
C7PJ7 Please Code in C++
Additional Notes(from my class):
-No global variables may be used in any of our assignments. If I see a global variable being declared, a zero points will be assigned.
Clarification:
-While variables may not be global, all functions and constants should be declared global.
-Beginning from Chapter 4, all assignment must contain functions, even if it is possible to do without one.
-If the assignment calls for just a function, you must code a driver to test the function. The driver and the function both must be submitted.
Thank you
:)
7. 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 r 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 additions until the user says the program s endExplanation / Answer
//Main.cpp
//Addition of two numbers by storing them to array
#include<iostream>
#include<string>
#include<cstring> //for strlen
using namespace std;
#define MAX 20
//to find minimum number
int min_num(int a, int b)
{
return (a < b ? a : b);
}
//To swap to numbers
void swap(int &a, int &b)
{
int c = a;
a = b;
b = c;
}
//Converting a char array to integer array
void converttointarray(int a_int[],char a[])
{
for (int i = 0; i < strlen(a); i++)
{
a_int[i] = a[i] - 48; //any char subtraction from 48 is ascii of zero will give a corresponding integer
}
}
//print the number in the array
void printnumber(int num[],int len)
{
for (int i = 0; i<len; i++)
cout << num[i];
cout << endl;
}
//reverse the number so that calculation can be easy
void reverse1(int num[], int len)
{
for (int i = 0; i<len/2; i++)
swap(num[i],num[len-1-i]);
}
//Addition of two number based on pen pencil algorithm
//takes two reversed numbers and perform addition
bool addition(int sum_arr[],int &len,int num1[], int num2[], int len1, int len2)
{
int min_n = min_num(len1, len2); //iterate till that point as it will be common in two numbers
int sum = 0;
len = 0; //length of result ie addtion
int carry = 0;
for (int i = 0; i < min_n; i++)
{
sum = num1[i] + num2[i]+carry;
carry = 0;
if (sum>9) //if sum is greater than 9 calculate the carry
{
if (i == MAX - 1) //if the number result greater than MAX ie 20 do not proceed
{
cout << "Integer overflow";
return false;
}
sum_arr[i] = sum % 10;
carry = sum / 10;
len++;
}
else
{
sum_arr[i] = sum;
len++;
}
}
//iterate till end of loop for number1 and keep adding
for (int i = min_n; i < len1; i++)
{
sum_arr[i] = num1[i];
len++;
}
//iterate till end of loop for number2 and keep adding
for (int i = min_n; i < len2; i++)
{
sum_arr[i] = num2[i];
len++;
}
return true;
}
int main()
{
char num1[MAX], num2[MAX];
int numb_int1[MAX], numb_int2[MAX];
int add[MAX];
int numb_len;
char ch = 'y';
do
{
cout << "Enter number 1:";
cin >> num1;
cout << "Enter number 2:";
cin >> num2;
//convert both numbers to integer array
converttointarray(numb_int1, num1);
converttointarray(numb_int2, num2);
//calculate length for numbers
int length1 = strlen(num1);
int length2 = strlen(num2);
//printing the nummbers
cout << "Number1:";
printnumber(numb_int1, length1);
cout << "Number2:";
printnumber(numb_int2, length2);
//reverse the number for addition as it is easy this way
reverse1(numb_int1, length1);
reverse1(numb_int2, length2);
//Perform the addition
bool res =addition(add, numb_len, numb_int1, numb_int2, length1, length2);
if (res)
{
//Since the addtion will be in reversed format we have to change it again to normal number
reverse1(add, numb_len);
//print the number after addtion
cout << "Addition:";
printnumber(add, numb_len);
}
cout << "Do you want to continue:(y/n):";
cin >> ch;
} while (ch != 'n');
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.