### Adding numbers up to 512 digits, placing them into arrays?

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)

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445`` ``````#include #include 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.
Last edited on
To add and subtract you have to convert the strings to integers. But why do you need arrays?
Also consider that ints can store numbers up to 10 digits (http://en.wikipedia.org/wiki/Integer_%28computer_science%29#Common_integral_data_types for more info) so having a 512 digits number is unlikely to work
I need arrays to hold the characters (which are integers), because the user can input up to 512. Doesn't mean that they will, but there's always a possibility. Int/Floats don't have enough room to hold digits up to 512.

So I need to convert the characters into integers. Probably by using ASCII.
Aah ok, now I get what you want to do. Though sounds like coding the math of the arrays will be a pain
To insert elements backwards you need to know the size of the array (which isn't an issue here) and do something like
 ``1234`` ``````// size = 512 int last_element = size - 1 // because arrays start from 0 for(i = 0; i < string.size(); ++i) array[last_element - i] = string[i];``````

This doesn't remove zeroes at the beginning nor converts the character to numbers

I suppose you could use directly std::strings for the operations, but I don't feel like thiking too hard about it if you don't have problems
Last edited on
Using string objects would be fine though. Of course, you might find it easier to reverse each string to work from left to right as you add or subtract. Otherwise you'd have to start any interation through the string at the end. But then you have the problem of how to deal with one string being shorter (as in number of total char wise) then the other string.
maeriden:

so the size would be like, the length of the array (which the user gives of course)?

so something like

 ``123456789`` ``````string firstNum; firstNum.size //and then your code int last_element = size - 1 for(i = 0; i < firstNum.size; i++) array[last_element - i] = firstNum[i];``````

would the array just a normal array that I define? or would the for loop just be placing the characters from the string into the array?

BUT before I do this, don't I need to convert the characters of the array into integers?

Sorry if I'm confusing you. C++ has always been a challenge for me and I'm finding it kind of difficult.
Last edited on
Raezzor:

I would do that, but I am trying to make this as easy as possible. :P Plus I have to do math with all of the characters from the string! This is very time consuming haha.
The array is a normal char array. I should have specified that, sorry. Since I don't know how you intend to implement the process I just provided an answer for your original question, which is how to place characters in an array from the end.
Of course making an int array and directly placing the characters converted to numbers is possible and only requires adding an operation
 ``1234`` ``````int array[size]; int last_element = size-1; for(i = 0; i < string.size(); ++i) // note that size() is a function of the string class array[last_element - i] = string[i] - '0';``````

 so the size would be like, the length of the array (which the user gives of course)?

Yes and no. The size of an array must be known at compile time, unless that array is created using dynamic memory allocation. If you do that the user input can be used to create an array just as big as you need, thus saving memory, but for a program this small I wouldn't bother with it.
So, the size is the lenght. Since you ask for a maximum of 512 digits the size should be 512
 Yes and no. The size of an array must be known at compile time, unless that array is created using dynamic memory allocation. If you do that the user input can be used to create an array just as big as you need, thus saving memory, but for a program this small I wouldn't bother with it. So, the size is the lenght. Since you ask for a maximum of 512 digits the size should be 512

With respect to my program, since the size of the string inputted by the user can be any size up to 512, I think I should make the array the size of the string. So something along the lines of this:

`size = firstNum.length() //since firstNum.length would be the length that the user inputs whether the length is 15 or 512. `

Is that correct?

`array[last_element - i] = string[i] - '0';`
as the for loop is going, is it taking the 0's?

I'm sorry if it seems like you're talking to a noob, haha. :P I am just trying to understand this, and get my program to work.
Last edited on
 With respect to my program, since the size of the string inputted by the user can be any size up to 512, I think I should make the array the size of the string.

Of course, I just meant it's not critical to do it. An array of 512 ints needs 2KB to be stored and the usual stack size is 1MB, so there's plenty of space. But this could be a good opportunity to learn about dynamic memory allocation. If you feel like it go for it

 Is that correct?

Very correct

 I'm sorry if it seems like you're talking to a noob

Don't be, I wouldn't post in the beginners section if I wanted to see people with full understanding of the language (which, by the way, I don't have)
Last edited on
Aright! So I almost have it down! But there is a error message and says "int size expression must have constant value." Do you know what that means? Here is the code that I have so far (with your help of course!)

I am going to convert the characters from the strings into ints first, and then apply this code to both of the strings (firstNum and secondNum)

 ``123456`` ``````int size = firstNum.size(); int array[size]; int last_element = firstNum.length()-1; for(int i = 0; i < firstNum.size(); ++i){ array[last_element - i] = firstNum[i] - '0'; }``````

Is it because firstNum.size() can't be set as an integer?
like I said, the size of an array must be known at compile time unless you use dynamic memory allocation. The value of size will be known only when the program is run and the user inputs a string, so the compiler gives you an error

 and with your code array[last_element - i] = string[i] - '0'; as the for loop is going, is it taking the 0's?

No, that is the algorithm to convert a number represented as a charater to an actual number on which math can be performed. You said
 So I need to convert the characters into integers. Probably by using ASCII.

This is how is done
Ohhh, okay. So your code placed the characters from the string into an array from the last element to the first element and also converted them into actual numbers.

I will try and figure out the rest on my own! But thank you so much for your help. :) It is very much appreciated!
Topic archived. No new replies allowed.