Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For C++ without string Write a program for the following: Assume that your compu

ID: 3735003 • Letter: F

Question

For C++ without string

Write a program for the following: Assume that your computer has the very limited capability of being able to read and write only single-integer digits and to add two integers consisting of one decimal digit each. Write a program that can read two integers up to 50 digits each, add these integers together, and display the result. Test your program using pairs of numbers of varying lengths including one exceeding 50 digits. Hint: Store the two numbers in two int arrays of size 50, one digit per array element. If the number is less than 50 digits in length, enter enough leading zeros (to the left of the number) to make the number 50 digits long. You will need a loop to add the digits in corresponding array elements. Don’t forget to handle the carry digit if there is one! Make sure you test your program with enough test data. Hint: Digits from the large integer values should be read in as characters and then converted to integers one character at a time to be stored in the 51 element integer array. Since the number is large and you cannot input it as an integer. MAKE SURE YOU CHECK FOR OVERFLOW IF SUM IS MORE THAN 50 DIGITS. TO CHECK THESE THE TWO NUMBERS MUST BE 50 DIGITS LONG AND THE SUM IS 51 DIGITS LONG.

Explanation / Answer

Please find my answer:

#include <iostream>
#include <stdio.h>
using namespace std;
void inputValue1( int[]);
void inputValue2(int[]);
void Sum(int[],int[],int[]);
const int SIZE=50;
int main()
{
int val1[SIZE];
int val2[SIZE];
int total[SIZE];
inputValue1(val1);
//inputValue2(val2);
Sum(total,val1,val2);
   return 0;

}
void inputValue1( int val1[])
{
   int i=30,tempArray[SIZE];
   char temp;
cout<< "Enter first number:";
   for(i=0;i<=SIZE;i++)
   {
       cin>>temp;
       if(temp!=' ')
           tempArray[i]=temp - '0';
       else
           break;
   }
   i--;
   for(int j=SIZE;j>=0;j--,i--)
   {
       if(i>=0)
           val1[j]=tempArray[i];
       else
           val1[i]=0;
   }
return;
}
void inputValue2(int val2[])
{
int i=SIZE,tempArray[SIZE];
   char temp;
cout<< "Enter second number:";
   for(i=0;i<=SIZE;i++)
   {
       cin>>temp;
       if(temp!=' ')
           tempArray[i]=temp - '0';
       else
           break;
   }
   i--;
   for(int j=SIZE;j>=0;j--,i--)
   {
       if(i>=0)
           val2[j]=tempArray[i];
       else
           val2[i]=0;
   }
return;
}
void Sum(int total[],int val1[],int val2[])
{
   int carry=0,temp;
for(int i=SIZE;i>=0;i--)
   {
       temp=val1[i]+val1[i]+carry;
       if(temp>10)
       {
           temp=temp-10;
           carry=1;
           total[i]=temp;
       }
       else{
           total[i]=temp;
           carry=0;
       }
   }
   cout<<"The result is :"<<endl;
   for(int i=0;i<=SIZE;i++)
       cout<<total[i];
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote