-
Large Numbers
I Have Another Homework Assignment This Time Adding Large Numbers. The Program Must Ready Up to 12 large positive numbers and add them accurately. Here Are A Few Examples On How The Teacher Wants The Numbers Displayed.
First Ask How Many Then Get the Input i.e.
How many numbers? -----> 4
Input Number #1 -----> 4
Input Number #2 ------>1000000009.12345
Input Number #3 ------> 99999.999
Input Number #4 -----> 723456.
The Sum Is:
123.00000
1,000,000,009.12345
99,999.99900
723,456.00000
+)
--------------------------------
1,000,823,588.12245
Here is what I have so Just To Get The Math Right. I Still Need To Ask For The Input And Display The Outcome Any Suggestions Will Be Helpful.
Code:
#include <iostream>
using namespace std;
int main() {
//large numbers:
int one[10] = { 1,2,3,4,5,6,7,8,9,9}; //actual number 1,234,567,899
int two[10] = { 2,3,3,4,5,6,7,8,9,0}; //actual number 2,334,567,890
int result[10]; // we'll use this to store digits
//some holders we'll need
int carryOver = 0, value = 0;
for(int i = 9; i >=0; i--) {
//add
value = one[i] + two[i];
if (value > 9){
value -=10;
result[i] = value + carryOver;
carryOver = 1;
}
else {
result[i] = value + carryOver;
carryOver = 0;
}
}
//display
for(int i = 0; i < 10; i++){
cout << result[i] << " ";
}
return 0;
}
-
Your input doesn't match your output.
Anyway: you will need to always add in the carry from the previous. The carry can be more than 1, so there's no reason to hard-code that in.
-
Oh, come on!! Just use long int or even long long int to input large numbers! (I don't know about long long int or LARGE_INT, because your OS might not support them.
-
Also use "unsigned" if you want to input only positive values, to make the max number twice as large
-
-
-
-
There's a good way to increase your post count...
-
Sorry... i'm not used to this posting technic...
-
You mean "technique"
...
with lowercase letters.
mcdant01: What is the largest number your program needs to be able to accept? Also, how many decimal places are you going to accept?
How do you plan to deal with the decimal point in your internal representation?
We can't really give any help until you've said what you're having trouble with though.