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
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
Create a program that generates a file of random numbers, and then prints them in neat...
Create a program that generates a file of random numbers, and then prints them in neat fashion to another file, and also saves to that file the average and standard deviation of those numbers. I) First, you would need to generate a file of random numbers that consists of N random numbers (100 < N < 1000). Each random digit should be a real number (type double) between 0 and 50. This file and its digits would now serve as...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics relating to data supplied by the user. The program will ask the user to enter the number of items making up the data. It will then ask the user to enter data items one by one. It will store the data items in a double array. Then it will perform a number of statistical operations on the data. Finally, it will display a report...
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...
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...
Write a java program that creates an integer array with 50 random values, prompts the user...
Write a java program that creates an integer array with 50 random values, prompts the user to enter the index of an element in the array between 0 and 49, then displays the corresponding element value. If the specified index is out of bounds, display an error message (e.g. “Out of Bounds”) and ask the user to enter another index. Use a while loop that will keep prompting the user until a valid input is received. To handle invalid inputs,...
Problem: Our Armstrong number Please write code for C language So far we have worked on...
Problem: Our Armstrong number Please write code for C language So far we have worked on obtaining individual digits from 4 digits or 5 digit numbers. Then added them to find the sum of digits in various examples and assignments. However, the process of extracting individual digits is actually can be solved using a loop as you were doing a repetitive task by using mod operation and division operation. Now, we know how loops work and we can remove the...
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....
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...
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT