So I need to add a number of up to 512 digits. This number will be provided by the user. I have the general idea of how to do it, I was wondering if it makes sense?
Here is my code (I don't have the actual arrays yet nor do I have the math part yet)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
#include <iostream>
#include <string>
using namespace std;
int main(){
//declared variables
string firstNum;
string secondNum;
//prompt user for input
cout << "Program: Advanced Repition" << endl
<< "Author: Lisa" << endl
<< endl << "Please enter a whole number (up to 512 digits): " << endl;
cin >> firstNum;
cout << endl << "Please enter another whole number (up to 512 but less "
<< "than the first number)." << endl;
cin >> secondNum;
//putting the characters from the string into an array.
//start by putting the last character into the last element
//of the array. Do this for both firstNum and secondNum,
//until it reaches the first. So there will be a for loop.
//adding the numbers in the array and output
//add the elements starting from the right and going to the left
//ignore all the zeros (leading zeroes that will be at the right)
cout << endl << "The sum of the two numbers is " << endl;
//subtracting the numbers in the array and output
//subtract the elements starting from the right and going to the left
//ignore all the zeros (leading zeroes that will be at the right)
cout << "The difference of the two numbers is " << endl;
//pause and exit
getchar();
getchar();
return 0;
}
|
I have a string that holds the two numbers. I think I need to place each character into the array, but place into the last element and work up until the first. There will be a for loop for this right? Will I also need to convert these characters into integers?
Then, after everything is placed into two arrays, I will need to:
1) add each element together, and ignore the zeroes that will be in the front of the array.
2) subtract each element, and again, ignore the zeroes that will be at the front of the array.
That's basically the whole program. I was wondering how you place characters from a string into an array, but placing them into the LAST element rather than the first?
I can only use the stdlib.h, <string>, and <cmath> libraries.