Question

One way to represent a very large integer (one that won't fit into a variable of...

One way to represent a very large integer (one that won't fit into
a variable of type short, int, or even long) is to use an array.

The array is of type int, so each element in the array can hold an
integer -- we will store just one digit of our number per array element.

So if a user entered 2375, it might be stored as

--------------------------
| 2 | 3 | 7 | 5 | ... |
--------------------------

(Note that we may want to actually store it right justtified, with
leading 0's on the left, or maybe in reverse order -- but it is a
requirement of this program that you get the digits of the number
from the user 1 at a time, and store them 1 at a time, 1 per array element.)

You should get a number (into an array) using a function called GetNumber.
Keep track of how big it is, if need be. Only allow digits 0-9, no
negatives.)

(you'll call it twice, to get 2 numbers to work with.)

Then call another function called AddNumbers. It takes the 2 numbers and
creates another number (stored in another array) that is the sum of
the 2 given numbers. It should also give back a piece of info that is
boolean (true/false) -- is the 3rd number "valid" ? (no if the addition
causes overflow -- it's too big to fit in an array we use.)

Then call another function called SubNumbers. It takes the 2 numbers and
subtracts them (first minus second.) It gets back to the calling function
the result (and a boolean telling if it is/isn't valid -- if it underflows --
the subtraction would yield a negative, the boolean is false, otherwise
it is true.)

The main function should print out the results (or call a PrintResults
function if you like.)

Allow the user to run for multiple datasets, as usual.

Note that you MUST use functions for GetNumber, AddNumbers, and SubNumbers.
Any other functions are optional -- write them if you feel they help
make this easier.

Make your physical array size 10. (It needs to be exactly 10 to make
the data below, in some instances, meaningful with respect to testing
underflow/overflow.)

-------------------------------------------------------------------------

Data to test/run (note that I'm listing the numbers as multi-digit here,
but they MUST be input 1 digit at a time, and stored 1 digit per array
element.)


9999999999
1


1234567899
8765432101


8765432101
1234567899


2468357955
8000000001


1000000000
1


1000
1


756
437

Homework Answers

Answer #1

#include<iostream>
using namespace std;
int * getno(int n)
{
int a[10],value;
int no=n;
for(int i=0;i<10;i++)
a[i]=0;
for(int i=9;i>=0 && no!=0;i--)
{
value=no%10;
no=no/10;

a[i]=value;
}
return a;
}
bool addno(int *a,int *b)
{
int c[10],r,carry=0,n;
for(int i=9;i>=0;i--)
{
c[i]=a[i]+b[i]+carry;
if(c[i]>9)
{
n=c[i];
r=n%10;
n=n/10;
c[i]=r;
carry=n;
}
if(c[0]>9)
break;
}
if(c[0]>9)
return false;
else
return true;
}
bool subno(int *a,int *b)
{
int c[10],flag=0;
int borrow;
for(int i=9;i>=0;i--)
{
if(a[i]<b[i])
{
a[i]=a[i]+10;
a[i-1]=a[i-1]-1;
}
c[i]=a[i]-b[i];
if(a[0]<b[0])
{
flag=1;
break;
}
}
if(flag==1)
return false;
else
return true;
}
int main()
{
long long int n,m;
cout << "\nenter any two nos:";
cin>>n>> m;
int *a=getno(n);
int *b=getno(m);
cout<< "first no is:";
for(int i=0;i<10;i++)
cout<< a[i];
cout<< "\n";
for(int i=0;i<10;i++)
cout<< b[i];
if(addno(a,b))
cout<< "\ntwo nos can be added :)";
else
cout<< "\noverflow";
if(subno(a,b))
cout<< "\ntwo nos can be subtracted:)";
else
cout<< "\nunderflow";

return 0;
}

Know the answer?
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
Similar Questions
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns...
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns the index of the first occurance of the smallest value in the array. Your function must be able to process all the elements in the array. Create a function prototype and function definition (after the main function). Your main function should declare a 100 element integer array. Prompt the user for the number of integers to enter and then prompt the user for each...
/* This program should check if the given integer number is prime. Reminder, an integer number...
/* This program should check if the given integer number is prime. Reminder, an integer number greater than 1 is prime if it divisible only by itself and by 1. In other words a prime number divided by any other natural number (besides 1 and itself) will have a non-zero remainder. Your task: Write a method called checkPrime(n) that will take an integer greater than 1 as an input, and return true if that integer is prime; otherwise, it should...
In this lab, we want to get some practice with pointers -- specifically, the basic syntax...
In this lab, we want to get some practice with pointers -- specifically, the basic syntax of declaring them, storing memory addresses into them, dereferencing them, and seeing how they work with pointer arithmetic in the context of built-in, C-style arrays. Unless specified otherwise, you can use any valid variable names you wish. The requirements for this program are as follows: Part A Declare a pointer-to-int, initialized to nullptr. Declare an int variable, initialized to some value of your choice....
python If a number num1 divides another number num2 evenly then num1 is a divisor of...
python If a number num1 divides another number num2 evenly then num1 is a divisor of num2. For example, 2 is a divisor of 2, 4, 6, 8, but 2 is not a divisor of 1, 3, 5, 7, 9, 11, 13. Write a function named count_divisors(m,n) that works as follows. Input: the function takes two integer arguments m and n Process: the function asks the user to enter numbers (positive or negative), and counts the total number of inputs...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER QUESTION: 1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers. For example: For n=10 Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 2): Write...
C++ project The project will allow you to create a system for tracking the games and...
C++ project The project will allow you to create a system for tracking the games and the scores in terms of graphics, story and replay value and will compute the highest score in each category and the highest overall score. The system will allow you to track 5 games in the following arrays: 1) A string array for the game titles 2) An int array for graphics scores from 1 to 5 3) An int array for replay value from...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: A positive integer number is said to be a perfect number if its positive factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number because 6=1+2+3. Complete the int Q6(intQ6_input, int perfect[])function that determines all perfect numbers smaller than or equal...
Note: Do not use classes or any variables of type string to complete this assignment Write...
Note: Do not use classes or any variables of type string to complete this assignment Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along...
Write the following program in MIPS: a) declare an array A of the following numbers: 3,...
Write the following program in MIPS: a) declare an array A of the following numbers: 3, 5, 8, 10, 12, 2, 76, 43, 90, 44 b) declare a variable called size which stores the number of element in array A, that is 10. c) write a subroutine to search for a number stored in an array and return true or false. In C++ the subroutine is as follows: search(array, size, number_To_Search) e.g. search(A, 10, 12) The subroutine should return 0...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop a class, using templates, to provide functionality for a set of recursive functions. The functions specified as recursive must be written recursively (not iterativly). The UML class specifications are provided below. A main will be provided. Additionally, a make file will need to be developed and submitted. ● Recursion Set Class The recursion set template class will implement the template functions. recursionSet -length: int...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • In order to conduct a hypothesis test for the population proportion, you sample 450 observations that...
    asked 6 minutes ago
  • Doctor’s Order: Vancomycin 500mg tab i po q12h X 7 days Available: Vancomycin 500mg tablets What...
    asked 19 minutes ago
  • Calculate the ΔG∘rxn for the reaction using the following information. 4HNO3(g)+5N2H4(l)→7N2(g)+12H2O(l) ΔG∘f(HNO3(g)) = -73.5 kJ/mol; ΔG∘f(N2H4(l))...
    asked 20 minutes ago
  • Question 03: Saturn Shoes (Pvt.) Ltd manufacture multi-style fashion boots for the residents of Missouri. Leather...
    asked 22 minutes ago
  • A highway with a design speed of 100 km/hr is designed with a sag curve connecting...
    asked 34 minutes ago
  • Shift Registers can be used for serial/parallel interface applications. True or false?
    asked 1 hour ago
  • Scenario 1: To describe the instructors’ experience, the researcher records the year in which each instructor...
    asked 1 hour ago
  • develop a flowchart or pseudocode to check the prime numbers 1- below 100 what to do...
    asked 1 hour ago
  • Which of the following statements are true? I. The sampling distribution of ¯xx¯ has standard deviation...
    asked 1 hour ago
  • Which of the following methods of reporting cash flows provided by operating activities does the Financial...
    asked 1 hour ago
  • SITUATION 2: EFFECTIVE STRESS An engineer investigates a granular soil deposit, 4 meters thick, overlaying a...
    asked 1 hour ago
  • Suppose that R is a commutative ring and I is an ideal in R. Please prove...
    asked 1 hour ago