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;
}