![]() |
help with a simple question
I need to code a function that takes a number, and if it's not negative or zero (if it's zero then the function quits), it checks if it's digits are rising from left to right (123, for example), and if they are, it prints it.
One problem I have with this is that if I knew the number had only 3 digits then I would divide it by 10, 100, 1000 and check those three numbers, but I don't know how many digits will be in the number the user types in, so I don't know what to do with that. Can anyone help me with this? Thanks. |
Re: help with a simple question
There are two ways you could look at this.
(1) If they're rising from left to right then they're descending from right to left. So modulo (%) the number with 10, record that, divide the test number by 10, repeat until the number is zero. (2) Divide by 10, 100, 1000 etc as originally planned, but do it in a loop multiplying the number you divide by, by 10, until that power of 10 is greater than the number. So if you've got 1234, then you stop at 10000. so: x=10 divide num by x x*=10 repeat until x>num |
Re: help with a simple question
Thank you very much, xpi0t0s!
|
Re: help with a simple question
Here is the code I wrote for the function (I deleted my comments to make the code cleaner):
Code:
int number() |
Re: help with a simple question
hey guys, I like the subject here, I personally had some problems with this stuff
so I done some time looking for a path and I found one! ok what you do is get the number of course (must get it as string for best results) then with an if statement check if it's lower then zero, if it is you start a new array string, and with a for loop you will do this list_num[i] = num; list_num[i].Remove(1); num.Remove(0, 1); that way you will have the array having all the numbers for you, then with a while loop you can check wether the number are increasing or not, you can also switch some number's positions or remove any index or even replace a single number with a high number like 1000 :P anyway I hope I helped you enough, sorry that I didn't just post the code but it's better to use logic so the reader gets better view of what will happen and why, if you can't get it don't hasitate to reply, I will post code then! --- ClayC |
Re: help with a simple question
Quote:
I think my code does the job okay, though it had a mistake that I fixed (= instead of a == near the end). About the array idea, we aren't allowed to use arrays yet, since we haven't studied them in the course yet (this is the first exercise). |
Re: help with a simple question
oh ok, well I tried to help you out ;)
and you haven't done arrays yet and you did the % stuff after the line which I never saw lol, that's kinda confusing cause arrays are variables and variables are first steps to learn a new language lol anyway it's good to know you have a working code for what you wanted, :) if there's something else you want help with ask me and I will help you out |
Re: help with a simple question
Here is the entire program(with comments):
Code:
#define _CRT_SECURE_NO_WARNINGSAdvice, anyone? |
Re: help with a simple question
I don't know what "the first menu item when entering the character to be printed" means - could you clarify? Could you perhaps give the line that crashes (commented) plus one or two lines each side for context?
e.g. Code:
some code |
| All times are GMT +5.5. The time now is 20:49. |