One way to represent a very large integer (one that won\'t fit into a variable o
ID: 3864692 • Letter: O
Question
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
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
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
Explanation / Answer
#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 << " enter 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<< " ";
for(int i=0;i<10;i++)
cout<< b[i];
if(addno(a,b))
cout<< " two nos can be added :)";
else
cout<< " overflow";
if(subno(a,b))
cout<< " two nos can be subtracted:)";
else
cout<< " underflow";
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.