| nomnom123 (7) | |||
|
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)
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
|
|||
| maeriden (230) | |
|
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 | |
|
|
|
| nomnom123 (7) | |
|
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. | |
|
|
|
| maeriden (230) | |||
|
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
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
|
|||
| Raezzor (223) | |
| 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. | |
|
|
|
| nomnom123 (7) | |||
|
maeriden: so the size would be like, the length of the array (which the user gives of course)? so something like
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
|
|||
| nomnom123 (7) | |
|
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. | |
|
|
|
| maeriden (230) | ||||
|
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
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 | ||||
|
|
||||
| nomnom123 (7) | ||
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? and with your code 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
|
||
| maeriden (230) | ||||
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
Very correct
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
|
||||
| nomnom123 (7) | |||
|
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)
Is it because firstNum.size() can't be set as an integer? | |||
|
|
|||
| maeriden (230) | |||
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
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
This is how is done | |||
|
|
|||
| nomnom123 (7) | |
|
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! | |
|
|
|