Question

Write a program to subtract large unsigned integers. Your program should prompt and read in two...

Write a program to subtract large unsigned integers. Your program should prompt and read in two large unsigned integers. The two large integers should be stored arrays, one array for each integer. The integers should be stored with one digit per location in the two arrays. The first integer should be no smaller than the second. Your program should subtract the second integer from the first. The result should be stored in an array, again one digit per location in the array. Your program should then output the difference, there should be no leading zeros. Your program should be able to handle integers with up to 80 digits each. You should not use any concepts from object oriented programming. In particular please do not use any classes (or struct) or any class methods. The one exception is that you may use cin, cout and the iostream class method cin.get(). Test data:

10000000000000000000000000000000000

- 9

1111111111111111111111111111111111

- 10000010001

123456789123456789123456789

- 99999

9999999999

- 9999999999

IN C++ PROGRAMMING LANGUAGE PLEASE

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

Code in C++ (used strings instead of arrays because the input would become simple)

#include<bits/stdc++.h>

using namespace std;

// Returns true if str1 is smaller than str2.

bool isSmaller(string str1, string str2) {

    // Calculate lengths of both string

    int n1 = str1.length(), n2 = str2.length();

    if (n1 < n2)

    return true;

    if (n2 < n1)

    return false;

    for (int i=0; i<n1; i++)

    if (str1[i] < str2[i])

        return true;

    else if (str1[i] > str2[i])

        return false;

    return false;

}

string findDiff(string str1, string str2){

    // Before proceeding further, make sure str1

    // is not smaller

    if (isSmaller(str1, str2))

        swap(str1, str2);

    // Take an empty string for storing result

    string str = "";

    // Calculate length of both string

    int n1 = str1.length(), n2 = str2.length();

    // Reverse both of strings

    reverse(str1.begin(), str1.end());

    reverse(str2.begin(), str2.end());

    

    int carry = 0;

    // Run loop till small string length

    // and subtract digit of str1 to str2

    for (int i=0; i<n2; i++)

    {

        // Do school mathematics, compute difference of

        // current digits

        

        int sub = ((str1[i]-'0')-(str2[i]-'0')-carry);

        

        // If subtraction is less then zero

        // we add then we add 10 into sub and

        // take carry as 1 for calculating next step

        if (sub < 0)

        {

            sub = sub + 10;

            carry = 1;

        }

        else

            carry = 0;

        str.push_back(sub + '0');

    }

    // subtract remaining digits of larger number

    for (int i=n2; i<n1; i++)

    {

        int sub = ((str1[i]-'0') - carry);

        

        // if the sub value is -ve, then make it positive

        if (sub < 0)

        {

            sub = sub + 10;

            carry = 1;

        }

        else

            carry = 0;

            

        str.push_back(sub + '0');

    }

    // reverse resultant string

    reverse(str.begin(), str.end());

    return str;

}

// Driver code

int main()

{

    cout<<"Enter two unsigned integer: \n";

    string str1 = "10000000000000000000000000000000000";

    string str2 = "9";

    cin>>str1>>str2;

    cout<<"Difference = "<<findDiff(str1,str2);

    

    return 0;

}

Add a comment
Know the answer?
Add Answer to:
Write a program to subtract large unsigned integers. Your program should prompt and read in two...
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
  • Write a program to subtract large unsigned integers. Your program should prompt and read in two...

    Write a program to subtract large unsigned integers. Your program should prompt and read in two large unsigned integers. The two large integers should be stored arrays, one array for each integer. The integers should be stored with one digit per location in the two arrays. The first integer should be no smaller than the second. Your program should subtract the second integer from the first. The result should be stored in an array, again one digit per location in...

  • a c++ program: For this project you will write a program to add very large (unsigned)...

    a c++ program: For this project you will write a program to add very large (unsigned) integers. The two summands should be stored arrays, one array for each summand. The summands should be stored with one digit per location in the two arrays. Your program should, starting at the least significant digits compute the sum by adding the two integers digit by digit. Do not forget to handle carries. Your program should be able to handle summands with up to...

  • C programming Strictly - Write a program to sort an array of integers via arrays of...

    C programming Strictly - Write a program to sort an array of integers via arrays of pointers to those integers as shown in the figure. Problem 1 (68 points): Write a program to sort an array of integers via arrays of pointers to those integers as shown in the figure. The code should contain the following functions: 1)to randomly generate an array of integer numbers; 2) to allocate memory and build the arrays of pointers as shown in the figure...

  • 3. Write a program to read a (binary) file of integers, sort the integers, and write...

    3. Write a program to read a (binary) file of integers, sort the integers, and write them back to the same file. Assume that all the numbers can be stored in an array. (Exercise 3) 4. Repeat exercise 3, but assume that only 20 numbers can be stored in memory (in an array) at any one time. Hint: you will need to use at least two additional files for temporary output. Please finish the question in C language programming, thank...

  • Write a program that asks the user to enter 1000 integers to be stored in an...

    Write a program that asks the user to enter 1000 integers to be stored in an array called "numbers". Since the same integer might occur (exist) in the array multiple times, your program needs to fill a second array, called "Isolate" that contains all the integers from the first array but NOT REPAPTED (every integer will exist only once). Finally, your program will print the integers of the second array sorted by occurrence (occurrence is: the number of times the...

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

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

  • Write a Program in C language for: 1. Create a C program to read 7 integers...

    Write a Program in C language for: 1. Create a C program to read 7 integers and store them in an array. Next, the program is to check if the array is symmetric, that is if the first element is equal to the last one, the value of the second one is equal to the value of the last but one, and so on. Since the middle element is not compared with another element, this would not affect the symmetry...

  • Suppose we have two integer arrays with the same type, write an AL program to check...

    Suppose we have two integer arrays with the same type, write an AL program to check whether or not there are two integers, one from each array, with sum equal to zero. If there are such integers exist, print out all such combinations to the console window, otherise, print out "No integers in these two arrays, one from each array, with sum equal to zero." to the console window. December 3. 2018 For example, suppose we have the following two...

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

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