Here are the requirements: Here is function ReadLargeInt() Requirement . Use int
ID: 3750398 • Letter: H
Question
Here are the requirements:
Here is function ReadLargeInt()
Requirement . Use int arrays to store decimal numerals. As this is sometimes partially filled, we can use a separate int (e.g., decNumberLen) to store the number of digits. const int MAX_CAP-10; int decNumberDigits MAX CAP]; int decNumberLen-0; Il decNumberDigits[O] stores the 1-th digit Il decNumberDigits[1] stores the 10-th digit Il decNumberDigits[2] stores the 100-th digit for example, the number 9845 will be represented as the following int num1[MAX_CAP]-(5,4,8,9) int num1_length-4 Design and implement a function that displays such large integer in standard output. . Design and implement a function that performs addition on two large integers Function ReadLargelnt() has been provided. It reads such large integer from standard input. Test above functions one by one, and then write a main that works as follows: .
Explanation / Answer
//C++ program
#include<iostream>
using namespace std;
#define max_size 10
bool readinput(int arr[]){
string str;
cin>>str;
if(str.length()>max_size){
cout<<"input is two long ";
return false;
}
int length =str.length(),digit;
for(int i=0;i<length ;i++){
digit=str.at(length-i-1)-'0';
if(digit>9||digit<0){
cout<<"invalid input ";
return false;}
arr[i]=digit;
}
}
bool sumBigNumber(int arr1[] ,int arr2[],int sum_arr[]){
int sum,carry=0;
for(int i=0;i<max_size;i++){
sum=arr1[i]+arr2[i]+carry;
sum_arr[i]=sum%10;
carry=sum/10;
}
if(carry==1)return false;
return true;
}
int main(){
int arr1[max_size],arr2[max_size],result[max_size];
bool status;
string str="";
for(int i=0;i<max_size;i++){
arr1[i]=0;
arr2[i]=0;
result[i]=0;
}
while(1){
cout<<"Read array 1 ";
status =readinput(arr1);
if(status==true)break;
cout<<" Please re-enter ";
}
while(1){
cout<<"Read array 2 ";
status =readinput(arr2);
if(status==true)break;
cout<<" Please re-enter ";
}
if(!sumBigNumber(arr1,arr2,result)){
cout<<"sum leads to overflow ";
}
else{
for(int i=max_size-1;i>=0;i--)
cout<<result[i];
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.