Question

Problem 02: Large Integer (20 points) In CH, the largest int value is 2147483647. So, an integer larger than this cannot be s

please use c++ programming and single dimensional arrays to solve this problem
thank you

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

Answer: Hey dear student finds the solution of your query if you have any doubt feel free to ask. Thanks!!

Dear student, this program has four functions. Input, reverse, sum and display. It will take inputs of two numbers as your input function. After that call reverse function to reverse two nums, this is necessary for doing the sum of two nums. then call function addNums() this performs addition of two nums(sum process with carry as normally we add num numbers) this is boolean type function if the sum exceeds MAX_SIZE it returns false otherwise true.

C++ Code:

#include <iostream>
using namespace std;

const int MAX_SIZE = 20; //constant for size

void inputNum(int num[], int &count) //function to take inputs numbers
{
   char ch;

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

       cin.get(ch);
   }
}

void reverseNums(int num[], int count) //function to reverse a number
{
   int i = 0;
   int j = count - 1;

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

       i++;
       j--;
   }
}

bool addNums(int num1[], int count1, int num2[], int count2, //function to add two numbers returns true if sum size < MAX_SIZE
       int res[], int &size) //if sum>MAX_SIZE return false
{
   int sum = 0;
   int carry = 0;
   int i = 0;
   int j = 0;

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

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

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

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

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

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

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

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

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

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

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

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

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

   return true;
}

void display(int num[], int count) //display numbers and sum
{
   for (int i = count - 1; i >= 0; i--)
   {
       cout << num[i];
   }
}

int main()
{
   int num1[MAX_SIZE]; //array1 to store num1
   int num2[MAX_SIZE]; //array to store num2
   int res[MAX_SIZE]; //array to store sum
   int count1 = 0;   
   int count2 = 0;
   int size = 0;

   cout << "Enter the first number: ";
   inputNum(num1, count1); //prompts for num1

   cout << "Enter the second number: ";
   inputNum(num2, count2); //prompts for num2

   reverseNums(num1, count1); //reverse num1
   reverseNums(num2, count2); //reverse num2

   bool ans = addNums(num1, count1, num2, count2, res, size); //call addNums() and returning to bool variable

   if(ans) //if true display sum and numbers
   {
       cout << endl;
       display(num1, count1);
       cout << " + ";
       display(num2, count2);
       cout << " = ";
       display(res, size);
       cout << endl;
   }
   else //display message
   {
       cout << "Sum is overflow" << endl;
   }

   return 0;
}

Enter the first number: 2315423621 Enter the second number: 1243025123 2315423621 + 1243025123 = 3558448744

Add a comment
Know the answer?
Add Answer to:
please use c++ programming and single dimensional arrays to solve this problem thank you Problem 02:...
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
  • 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,...

  • This is programming project 1 chapter 9(Book ISBN:1292222824 (1-292-22282-4) Walter Savitch Problem Solving with C++: Global...

    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...

  • [C++, don't do more advance code than single-dimmension arrays and c-strings] (Sum of Even random numbers)...

    [C++, don't do more advance code than single-dimmension arrays and c-strings] (Sum of Even random numbers) Write a program that generates twenty five random integers between 0 and 25 and displays the sum of even integers. (Hint: Use rand() % 25 to generate a random integer between 0 and 25. Use an array of 25 integers, say num, to store the random integers generated between 0 and 25.)

  • Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program...

    Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program that contains the following functions: 1. A function to read integer values into a one-dimensional array of size N. 2. A function to sort a one-dimensional array of size N of integers in descending order. 3. A function to find and output the average of the values in a one dimensional array of size N of integers. 4. A function to output a one-dimensional...

  • C++ Single Dimensional Arrays

    Exercise #1: Design and implement a program (name it AssignGrades) that stores and processes numeric scores for a class. The program prompts the users to enter the class size (number of students) to create a single-dimensional array of that size to store the scores. The program prompts the user to enter a valid integer score (between 0 and 100) for each student. The program validates entered scores, rejects invalid scores, and stores only valid scores in the array.  The program...

  • C- PROGRAMMING PROJECT #4 Design and Write a C program to calculate an average of an...

    C- PROGRAMMING PROJECT #4 Design and Write a C program to calculate an average of an array of numbers and produce the following output: 1. Your first and last name 2. Course Number 3. C Project Number 4. All the numbers in the array 5. The sum of all the numbers in the array 6. The count of all the numbers in the array 7. The average of all the numbers in the array Double-space after lines 3, 4, 5,...

  • Please solve this question using c++ language Problem 2. More Probleml. The program builds the array...

    Please solve this question using c++ language Problem 2. More Probleml. The program builds the array of integers that contains duplicates as well. Your program should find the sum of all unique elements in the array using a function findUniqueSum Arrays Write a program that as before asks the user to enter input as in оn Sample Input/Output Enter a list of numbers ending with -999 3 1 4 55-999 The sum of unique numbers is: 13 Enter a list...

  • please solve it before 11:15 today Subject :C++ programming - Lab_9_Sec 51.pdf 1411113: Programming for Engineers...

    please solve it before 11:15 today Subject :C++ programming - Lab_9_Sec 51.pdf 1411113: Programming for Engineers Fall 2017/2018 LAB #9 Name: ID: Date Section Objectives: After completing this lab, you will be able to .Analyze a problem. .Implement the solution in C++ . Practice arrays and strings. Lab Exc. Mark Score Correct input/output Computational correctness Correct input/output Computational correctness Exercise#1: Number of occurrences in an array Write a program that fills an array with 10 random numbers between 0 and...

  • i need help writing these programs in c++ format 1. Enter two integer arrays: array1-129, 0,...

    i need help writing these programs in c++ format 1. Enter two integer arrays: array1-129, 0, -56, 4, -7 and array2-19, 12, -36, -2, 12 3. Write the code to form and display another array array3 in which each element is the sum of numbers in the position in both arrays. Use pointers and functions: get array0. process arrays0 and show array0 (50 points) 2. Enter a positive 5-digit integer via the keyboard. Display each digit and fol- lowed by...

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