Question

This is programming project 1 chapter 9(Book ISBN:1292222824 (1-292-22282-4) Walter Savitch Problem Solving with C++: Global Edition, 10/E)

Question Do Programming Project 7 in Chapter 7 using a dynamic array. In this version of the problem, use dynamic arrays to store the ditits in each large integer. Allow an arbitrary number of digits instead of capping the number of digits at 20.

TER 7/ Arrays time. For example digit at a the integer 1234 could be stored in the array a by setting a[0] to 1, al1] to 2, a

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Programming language : c++

An individual function is used for adding elements in array. You can include it into main function if u want.

hope u understand.

source code:

#include <iostream>
#include <new>
using namespace std;
int sum[20];//globally declared sum array.

//function to calculate sum of the two array intteger elements.
int calculateSum( int num1[],int num2[],int n1,int n2)
{
try
{
int i = n1 - 1;
int j = n2 - 1;
int k = n1 - 1;
int carry = 0;
int s = 0;
while (j >= 0)
{
s = num1[i] + num2[j] + carry;
sum[k] = (s % 10);
carry = s / 10;
k--;
i--;
j--;
}
while (i >= 0)
{
s = num1[i] + carry;
sum[k] = (s % 10);
carry = s / 10;
i--;
k--;
}
int answer = 0;
if (carry)
answer = 10;
for (int i = 0; i <= n - 1; i++) {
answer += sum[i];
answer *= 10;
}
return answer / 10;
}
}
catch(runtime_error e)
{
cout << "integer overflow";
}
  

}
int main()
{
while(1)
{
int i,j;
char num1[20],num2[20];
try
{
cout << "Enter the two numbers :"<<endl;
cin >> num1 >> num2;
int cn1=sizeof(num1)/sizeof(num1[0]);
int cn2=sizeof(num2)/sizeof(num2[0]);
int *inum1,*inum2;
if(cn1>20 || cn2 >20)
{
throw cn1;//exception throwing
}
else
{
//dynamic allocation of integer array
inum1=new int[cn1];//integer of num1 is inum1
inum2=new int[cn2];//integer of num2 is inum2

//converting charactor into integer array.
for (i=0;i<cn1;i++)
{
inum1[i]=atoi(num1[i]);
  
}
for (j=0;j<cn2;j++)
{
inum2[j]=atoi(num2[j]);
  
}
int n1=sizeof(inum1)/sizeof(inum1[0]);//calculating number elements in inum1 array
int n2=sizeof(inum2)/sizeof(inum2[0]);//calculating number elements in inum2 array
if(n1<=n2)//checking wheteher the size of two array is different or not.
{
cout<< "The total sum : "calculateSum(inum1,inum2,n2,n1)<< endl;//calling function
}
else
{
cout<< "The total sum : "calculateSum(inum1,inum2,n1,n2)<< endl;
}
delete[] inum1;//deallocation of memory
delete[] inum2;
  
  
}
  
}
catch(exception& e)
{
cout << "integer overflow";
}
  

}
}

Add a comment
Know the answer?
Add Answer to:
This is programming project 1 chapter 9(Book ISBN:1292222824 (1-292-22282-4) Walter Savitch Problem Solving with C++: Global...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • please use c++ programming and single dimensional arrays to solve this problem thank you Problem 02:...

    please use c++ programming and single dimensional arrays to solve this problem thank you Problem 02: Large Integer (20 points) In CH, the largest int value is 2147483647. So, an integer larger than this cannot be stored and processed as an integer. Similarly, if the sum or product of two positive integers is greater than 2147483647, the result will be incorrect. One way to store and manipulate large integers is to store each individual digit of the number in an...

  • 3. (Dynamic Array) In C++, the largest int value is 2147483647. So. an integer larger than this cannot be stored and pr...

    3. (Dynamic Array) In C++, the largest int value is 2147483647. So. an integer larger than this cannot be stored and processed as an integer. Similarly, if the sum or product of two positive integers is greater than 2147483647, the result will be incorrect. One way to store and manipulate large integers is to store each individual digit of the number in an array. Your program must, at least, contain a function to read and store a number into an...

  • C++ pleasr Extra Credit: Large Integer (25 points) In C++, the largest int value is 2147483647....

    C++ pleasr Extra Credit: Large Integer (25 points) In C++, the largest int value is 2147483647. So, an integer larger than this cannot be stored and processed as an integer. Similarly, if the sum or product of two positive integers is greater than 2147483647, the result will be incorrect. One way to store and manipulate large integers is to store each individual digit of the number in an array. Write a program that inputs two positive integers of, at most,...

  • Write a C++ program that simulates a lottery game. Your program should use functions and arrays....

    Write a C++ program that simulates a lottery game. Your program should use functions and arrays. Define two global constants: - ARRAY_SIZE that stores the number of drawn numbers (for example 5) -MAX_RANGE that stores the highest value of the numbers ( for example 9 ) The program will use an array of five integers named lottery, and should generate a random number in the range of 0 through 9 for each element of the array. The user should enter...

  • Write a C++ console application that reverses an array of characters, and counts the number of:...

    Write a C++ console application that reverses an array of characters, and counts the number of:           Lower case characters (islower)           Upper case characters (isupper)           Digits (isdigit)           Other (not one of previous) Create four global variables to hold these counts. Create function void count(char input[], char reverse[]) that takes as input two arrays: one that contains characters and another that will contain the reverse of that array. The function will reverse the input array and do the...

  • daily21. Computing 1. C programming Please provide comments and write pre-condition and post-condition for the function....

    daily21. Computing 1. C programming Please provide comments and write pre-condition and post-condition for the function. Description Create a project called Daily21. Add a source file called daily21.c into the project. In this exercise, you will practice working with an array of integers. In the main function, you first need to create an integer array with 10 elements in t. Use a loop to read 10 integers from the user and store the values to the array (in the order...

  • In C... Write a program to simulate a pick-5 lottery game. Your program must generate and...

    In C... Write a program to simulate a pick-5 lottery game. Your program must generate and store 5 distinct random numbers between 1 and 9 (inclusive) in an array. The program prompts the user for: an integer random seed five distinct integers between 1 and 9 (which are stored in another array) The program then compares the two arrays to determine if they are identical. If the two arrays are identical, then the user wins the game. otherwise the program...

  • This C++ Programs should be written in Visual studio 2017 PROGRAM 1: Comparing Using Arrays to...

    This C++ Programs should be written in Visual studio 2017 PROGRAM 1: Comparing Using Arrays to Using Individual Variables One of the main advantages of using arrays instead of individual variables to store values is that the program code becomes much smaller. Write two versions of a program, one using arrays to hold the input values, and one using individual variables to hold the input values. The programs should ask the user to enter 10 integer values, and then it...

  • in C++ please ELET 2300 Programming Assignment # 2 Write a program that generates an array...

    in C++ please ELET 2300 Programming Assignment # 2 Write a program that generates an array filled up with random positive integer number ranging from 15 to 20, and display it on the screen. After the creation and displaying of the array, the program displays the following: [P]osition [Reverse, [A]verage, (Search, [Q]uit Please select an option: Then, if the user selects: -P (lowercase or uppercase): the program displays the array elements position and value pairs for all member elements of...

  • In basic c develop the code below: (a) You will write a program that will do...

    In basic c develop the code below: (a) You will write a program that will do the following: prompt the user enter characters from the keyboard, you will read the characters until reading the letter ‘X’ You will compute statistics concerning the type of characters entered. In this lab we will use a while loop. We will read characters from stdin until we read the character ‘X’. Example input mJ0*5/]+x1@3qcxX The ‘X’ should not be included when computing the statistics...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT