# New problem from book.

Discussion in 'Mac Programming' started by cybrscot, Feb 17, 2011.

1. ### cybrscot macrumors 6502

Joined:
Dec 7, 2010
Location:
Somewhere in Southeast Asia
#1
My new problem is based on the code below. The code was written in Chapter 4 and allows a user to enter 3 numbers, (or a 3 digit number, however you want to look at it) and then prints the numbers in reverse order, ie 123 becomes 321.

The current problem in Chapter 6 reads as follows.
Generalize the program so that the number can have 1, 2, 3, 4 or more digits. Hint: Use a do loop that repeatedly divides the number by 10, stopping when it reaches 0.

I have several problems that are on my mind.
1) The current code is setup to accept exactly what I want the user to enter, so how can I possibly write the code to accept a number of any amount of digits.
example: Below, for a 3 digit number, we do this..
Code:
`printf ("The reversal is: %d%d%d\n", userNumber % 10, (userNumber / 10) % 10, userNumber / 100)`
I'd have to write something like ((userNumber/100) % 10) / 10 for the 4th number, then something for the 5th digit, 6th digit, etc. etc, it could get very long and ugly.

2) Since the above "formulas" are how we reverse the numbers, what does the books suggestion of "repeatedly divide the userNumber by 10, stopping when it reaches 0", have to do with this? If the user enters 4000. Then we divide it down to 0, what does that get me? It doesn't reverse the numbers. It's obviously loop behavior, but for what end?

printf (Enter a number)
scanf (%d, userNumber)

do
(divide userNumber / 10)
while (userNumber > 0 ) //so that's my loop, I think it's what the book wants

So this will all end in 0, but what about the number reversal? And how can I use this loop to get that? I know I can only use the "formulas"(referenced above from my 3 digit example) if I know in advance how many digits the user is expected to enter. So that's out of the question, right? I'm stuck thinking about this divide by 10 business, I see how it works for the loop to keep looping and terminate when 0 has been reached, but how does it get my numbers reversed?

Can anybody give me a solid clue here? Get me thinking in the right direction?

Again I'm thinking, okay we want to enter a number and reverse it.
The only way I know how is to do......

Code:
`("The reversal is: %d%d%d\n", un % 10, (un / 10) % 10, un / 100)`
Say, user enters 100000
then 100000/10 = 10000
then 10000/10 = 1000
then 1000/10 = 100
then 100/10 = 10
then 10/10 = 1
then 1/10 = and so on.. I can do this with a loop, but where does my result come from?

I tried to look at this from a more simple perspective also, assuming the user only enters 2 numbers, then how will / 10 reverse the numbers I asked myself.
So, 58/10 = 5
5/10 < 0 , so I can use this to tell me there are two digits.

Likewise
102 / 10 =10
10/10 =1
1/10 < 0, so I can use this to see that there are 3 digits.
But none of these give me anything I can use to reverse the numbers.

Code:
```#include <stdio.h>

main ()

{
int userNumber ;

printf ("Enter a 3 digit number: ") ;
scanf ("%d", &userNumber) ;
printf ("The reversal is: %d%d%d\n", un % 10, (un / 10) % 10, un / 100) ;

return 0 ;

}	```

### Staff Member

Joined:
Aug 16, 2005
Location:
New England
#2
Here's just a suggestion.

Break the problem down.

Do one pass (loop) to get the number of digits, then another loop to print them in reverse order once you already know how. (There are simpler ways, but I think this is where you may be headed).

B

3. ### lee1210 macrumors 68040

Joined:
Jan 10, 2005
Location:
Dallas, TX
#3
An approach to this might be:
Create a resultNumber and set it to 0
Loop while userNumber is greater than 0
--Somehow scoot over the digits of resultNumber 1 to the left
--Grab a digit from userNumber with % 10
--Scoot over the digits of userNumber 1 to the right

At the end of this process resultNumber should have the reversed result.

I had originally given more precise instructions but didn't want to ruin it.

-Lee

4. ### chown33 macrumors 604

Joined:
Aug 9, 2009
Location:
Sailing beyond the sunset
#4
Suppose the user enters a number (call the variable 'num'). You then loop doing the following:
1. print num % 10 (i.e. least significant digit).
2. divide num by 10.
3. repeat until num is 0.

What does this output when num is 123? What does it output when num is 48769?

In general, the body of a loop is there to do something. If all you do is divide by 10, there's not much "something" going on. This is why you need to start thinking in terms of breaking the problem down into smaller and smaller steps, and have each step solve part of the overall problem. You have to analyze the problem and break it down. I see no analysis or breakdown occurring at all. You're just starting right away with code, expecting the analysis and algorithm to somehow happen. It won't. You have to do that part before you start coding.

If you started from "I have a number 48769" and worked from that, then what is the reversed number? (96784, or printed lowest digit first) How do you do to print the lowest digit? (% 10) Then how do you remove that digit? (/ 10) How long do you keep going? (Until no more digits) How do you recognize "no more digits"? (Is the value zero?) All of those are steps in the algorithm, and would be there whether you coded it in C, JavaScript, Fortran, or any other language.

5. ### cybrscot thread starter macrumors 6502

Joined:
Dec 7, 2010
Location:
Somewhere in Southeast Asia
#5
So with this method, I certainly could get the number of digits easily, Let's say there are 9 digits. I was thinking that then I would store each of the 9 digits, into a, b, c, d, e, f, g, h, i then print i, h, g, f, e, d, c, b, a BUT, I only know there are 9 digits, I don't know what they are. And I can't have the program create variables on the fly? Because before the user enters the input, I have no way of knowing how many numbers. So I can't declare int variables ahead of time for the storage of each number.

### Staff Member

Joined:
Aug 16, 2005
Location:
New England
#6
Wirelessly posted (Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_2_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5)

No need to store them only to know how many there are.

Actually now that I think of it you should already know a way to do it in a single loop. What's the bit left over after you do a division called?

Pull off each digit on the right and print them in that order left to right.

EDIT: That's what chown33 was saying.

B

7. ### Bill McEnaney macrumors 6502

Joined:
Apr 29, 2010
#7
Scott seems to think he would need a separate variable for each digit. But if he uses chown33's algorithm, he won't need indefinitely many variables.

8. ### lee1210 macrumors 68040

Joined:
Jan 10, 2005
Location:
Dallas, TX
#8
chown33's approach is similar to what I was suggesting, though I gunned to have the "number" with the digits reverse, rather than just printing the result a digit at a time, but both will result in the same thing happening.

I coded this up, and in my solution:
loop while the userNumber is more than 0
--Grab the first digit of userNumber
--multiply the current result by 10
--Add the new digit to the result
--divide userNumber by 10

At the end, print the result.

There is a type of storage that lets you hold a big list of values, but your book hasn't covered that yet. Even if it had, you don't need that this time. In your loop you can display a digit at a time, or just add the most recent digit to a running sum. You don't need N variables to hold N digits in this case.

-Lee

9. Feb 17, 2011
Last edited: Feb 17, 2011

### cybrscot thread starter macrumors 6502

Joined:
Dec 7, 2010
Location:
Somewhere in Southeast Asia
#9
First, allow me to say that this stupid book guided me in the wrong direction, the hint told me to divide by 10 until 0. Never said anything about using the %. Everything in my analysis was geared toward dividing by 10, and I couldn't see how they would get that method to work, turns out that's not the method, I need to use % instead.

Thank you for your reply, once you mentioned the %, it became clearer, although I still haven't got it yet. I can take any number, 43343555 % 10
and get 5, then 4334355 % 10, and get 5, and so on and so on. I can print the result of each % in the order it came and I will get 55534334. Seems easy enough, and no division by 10 needed at all, only %.

Code:
`You have to analyze the problem and break it down.  I see no analysis or breakdown occurring at all.  You're just starting right away with code, expecting the analysis and algorithm to somehow happen.  It won't.  You have to do that part before you start coding.`
I thought I wrote a ton of analysis in my first post. I didn't write any code. I did everything on paper, I only wrote what I thought my do loop should be, do (usernum/10) while (usernum > 0). The code as I mentioned was from Ch 4. It was already written, I just posted it so you could see the program the book was asking me to modify.

I didn't understand Lee's reply, as I was trying to figure out what he meant by "scooting" digits over to the left. Specifically, scoot over the digit of resultNumber 1 to the left. Assuming resultNumber1 is the result of division by 10, then a userNumber of 134 / 10, would have the resultNumber of 13 now, not 0 anymore. So resultNumber is now 13, scoot what over? To where? Scoot the 3 to the left in front of the 1 (gives us 31), then I could do 134 % 10 (which is 4), and scoot the 4 over to the left in front of the 31, now we would have 431, instead of 134. I think that's what he means, but I was kinda confused, and while I just did that on paper, I'm totally not sure how to put that into code, all the scooting over. How do I take 13 and make it 31 (which is basically scooting the 3 to the left), I have to again, do 13 % 10 = 3 and 13 /10 = 1, and print them in that order. I'm still confused, not sure if even this is doing what he is talking about.

Once I saw balamw and chown33 responses together, I was starting to think a bit more in a way that will get me what I want. I never thought of the % as I said because the book said /10 until 0.. Sent me in the wrong direction thought wise.

But I can take any number and do % 10, and each pass of the loop it will remove the number from the right end, then I just need to print each of those numbers as they are removed.

Like 34438392850 % 10 = 0
3443839285 % 10 = 5
344383928 % 10 = 8
34438392 % 10 = 2
3443839 % 10 = 9 so far I can print (05829
keep going, but eventually I will be faced with
3 % 10 = 3 (the last number)
but then what? I don't have a condition to satisfy exiting the loop, I took out the 3, so now it's (what % 10)??
I don't have anything = 0, nor < 0. I think this is where the division could come in, because 3/10 would be 0, and can exit the loop and complete the program at that point. I can't think of how to get the program to do the division on only the last remaining digit, and not the others. Dividing all the numbers by 10 would be of no use as I only need the % to give me the last number in the series. I'm confused at this point.

Lee, You said...

-Grab the first digit of userNumber
--multiply the current result by 10
--Add the new digit to the result
--divide userNumber by 10

So I did this..Lets say the user enters 348
You say grab the first digit. 348/100 = 3
Multiply the current result (I imagine you mean what's left)(48) by 10 Gives us 480.
Add the new digit (3) to the result (480 +3) = 483
divide 348/10 = 30
Now what?

Or did you mean,
Grab the fist digit 348/100 = 3
Multiply the current result by 10, literally, (3 was the result of the prev grab) 3 * 10 = 30
Add the new digit (still 3) to the result of the above step 30 + 3 = 33
divide 348/10 = 30
Now what?

10. ### lee1210 macrumors 68040

Joined:
Jan 10, 2005
Location:
Dallas, TX
#10
You've got a lot of stuff there, but I'm just going to respond to a bit of it... you do need to / by 10 to get rid of each digit and work your value towards 0, otherwise your loop will never exit.

I wasn't specific enough when i said "first digit", i meant the least significant digit. So that would be using mod.

So:
user entered: 1234
result: 0

Multiply result by 10, result is still 0
Get the least signficant digit, 4.
Add the digit to result, result is 4.
Divide user entered value by 10, setting it to 123

Multiply result by 10, result is 40
Get the least significant digit of user number, 123: 3
Add the digit to result, result is now 43
Divide user number by 10, setting it to 12

Multiply result by 10, result is 430
Get the least significant digit of user number, 12: 2
Add the digit to result, result is now 432
Divide user number by 10, setting it to 1

Multiply result by 10, result is 4320
Get the least significant digit of user number, 1: 1
Add the digit to result, result is now 4321
Divide user number by 10, setting it to 0

user number is 0, loop is over.

result is now 4321, display.

-Lee

### Staff Member

Joined:
Aug 16, 2005
Location:
New England
#11
Wirelessly posted (Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_2_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5)

Your exit condition is that the remaining number is a single digit.

B

12. Feb 17, 2011
Last edited: Feb 17, 2011

### cybrscot thread starter macrumors 6502

Joined:
Dec 7, 2010
Location:
Somewhere in Southeast Asia
#12
Yeah Lee, sorry there was so much stuff, I had to edit and add some stuff so I wouldn't get a consecutive post. I'd rather have a separate post, so the ideas are separate and more clear, instead of having everything lumped together in one post.

This is where I was at so far before your last post. I'm looping the same userNumber % 10, but I didn't know how to remove the last digit, so the userNumber is one digit shorter each time through. It would just be 348 % 10 every time through. And the userNumber / 10 will always be > 0 because the userNumber isn't changing away from 348. Should be 34, then 3. I don't know how to "get rid" of the last digit. I can get the mod, but can't remove the last digit.

The darn mathematical programs are the hardest for me to deal with. Mod this and divide by that and what to do with the numbers/results, where and when to print them?? I can loop words and phrases, my posted code from two days ago was perfect, and I made it up myself. My embedded message inside of a switch and a loop. No problem with that, from my head not the book. These number games get me angry. Who's ever going to write a program reversing numbers around, and printing GCD's. Aren't most programs, if you want this, enter this, if you enter that, then this, or that. I think I understand it when using commands and words/phrases. But all these darn numbers are making my head spin. The original code is so short on printing the reversal of 3 digits, I thought this would be easy when I started 3.5 hours ago. Now I'm just angry at myself again. Earlier, I promised myself that I would stay calm and not get frustrated, take it slow and easy and the answer will come. I stepped away from the PC several times, had a snack, went to the bathroom, etc. But here it's 5am. Soon I'll be up until 6 doing this stuff. It used to be 4, now 5. The sad part is I'm not even tired. Can't fall asleep like this.

Code:
```#include <stdio.h>

main ()

{
int userNumber, resultNumber ;

printf ("Enter a number: ") ;
scanf ("%d", &userNumber) ;

do {
(userNumber % 10 ) ;
}  while ((userNumber / 10) > 0 ) ;
resultNumber = userNumber % 10 ;

printf ("%d", resultNumber) ;

return 0 ;

}	```

Lee, how did you get 0?? 0 is the result of what? Not 1234 % 10, not 1234 / 10.

So:
user entered: 1234
result: 0

Below still isn't working.

No matter what I do first, it messes up the number for the next operation, if I do the % first, then it removes the last digit say 348 % 10, 8 is gone, now we're left with 34. 34/10 is 3, so it will return only 3 to the loop. I need it to return 34 to the loop.

And if I do division first. 348 / 10, I get 34, then 34 is sent to the mod, so 34 % 10 is 4, I have only a 3 left to return to the loop. Again that won't work either.

Code:
```#include <stdio.h>

main ()

{
int userNumber, resultNumber ;

printf ("Enter a number: ") ;
scanf ("%d", &userNumber) ;

do {                        //ex. usernumber is 348
(userNumber % 10 ) ;  //taking out the last digit
(userNumber / 10 ) ;  //dividing 34/10, then returning 3 to loop Doesn't
//work
}  while ((userNumber / 10) > 0 ) ;

printf ("%d", resultNumber) ;

return 0 ;

}	```

Code:
```#include <stdio.h>

main ()

{
int userNumber, resultNumber ;

printf ("Enter a number: ") ;
scanf ("%d", &userNumber) ;

do {         //example usernumber is 348

(userNumber / 10 ) ; //now makes it 34
(userNumber % 10 ) ;// takes out 4, and returns a 3 to the loop, Not
// correct either.  No matter which step I do first, it messes up the number for
//the next step.
}  while ((userNumber / 10) > 0 ) ;

printf ("%d", resultNumber) ;

return 0 ;

}	```

13. ### McGordon macrumors member

Joined:
Dec 28, 2010
Location:
Scotland
#13
Remember that it's just a hint, not a solution. It said to divide by 10, which you need to do. It didn't say you also need to calculate the remainder too for every digit. You're meant to work that part out yourself.

Lee's solution should also work, but I think it's confusing you. There's a simpler way where you don't have to store anything apart from the number you're dividing by 10. I'm not going to give you the solution but the body of the loop I have here is only two lines (not yet, not until you're having your 4:30a.m. give-up moment)

14. Feb 17, 2011
Last edited: Feb 17, 2011

### chown33 macrumors 604

Joined:
Aug 9, 2009
Location:
Sailing beyond the sunset
#14
This is wrong, as others have already explained.

You wrote code for the entire loop, but you didn't analyze anything. An analysis would have shown what the values were at each step. If you'd done that with a sample input, say the value 123, and actually written out each step, it would have been obvious that nothing of consequence was happening. You'd be dividing by 10 at each step, but not doing anything else. I urge you to actually do this: break each step down and write out what happens.

This may seem trivial or even silly, but it's crucial to accurately visualize what happens. In a nutshell, that's what an analysis is: an accurate visualization of what happens.

For example, take your assertion that division isn't needed. Suppose you start with a number, say 123. Do the % 10: 123 % 10 gives 3. Print the 3. What do you have now? Well, you still have 123, because the only calculation or arithmetic operation you've done so far is modulus, and % 10 had no effect on the 123. How do you get from 123 to something else? You could subtract 3 (i.e. the %10 value): 123 - 3 gives 120. Hmm, seems closer, but still not right. Think about what you want (12) and what you have (120), and what operation is needed to get from what you have to what you want. That operation is divide by 10: 120/10 gives 12. Conveniently, 123 /10 also gives 12, so subtracting 3 is unnecessary.

That's a proper analysis: breaking it down and actually showing what each step does, and how to go from step to step. If you don't show what the step does, using an example, you're not analyzing.

Analysis is also explaining why you should go from step A to step B, e.g. from 123 to 12 by division. The reason is that division by 10 brings the next most significant digit (i.e. the 10's) into the least-significant position (the 1's), and it brings all the higher digits down as well. Since % 10 only works on the least-significant digit, you have to setup the number each time around so the part you're working on is in the right place. It's the setup that needs a divide by 10, and is a crucial step to making it loopable.

15. ### McGordon macrumors member

Joined:
Dec 28, 2010
Location:
Scotland
#15
If you don't like solving problems like this, you might not like programming very much. They could both have practical applications, but even if they didn't they're training you how to solve problems which you might encounter. The ones you might encounter with real applications can be a lot more complicated than these.

To get rid of the last digit, you just divide by 10 as they're int variables and the remainder gets thrown away.

These are maths problems, like find the formula for the sequence:

10, 100, 1000, ...

You had that pattern in one of your attempts at this problem. You need to convert that in your head into a formula like 10^n, 10 to the power of n. This could be a loop that executes n times in this case, or maybe you need to calculate the 23rd number in the sequence.

Programming can make you an insomniac, or insomniacs like to program?

16. ### MaynardJames macrumors newbie

Joined:
Aug 27, 2008
#16
No, the book is not stupid, and it did tell you want you needed to know. Remember that the '%' (mod) operator IS division. So is the '/' operator. They both perform a division, they just return different parts of the result of that division. The '/' operator returns the quotient, while the '%' operator returns the remainder.

Also, in your specific case, you still need to use the '/' operator to reduce your original number. For example, take the number myNum = 12345, where myNum is just your integer variable.

remainder = myNum % 10 will return 5 (because 12345 divided by 10 is equal to 1234 with a remainder of 5) and assign it to remainder, but myNum still has the value 12345. It hasn't changed. You would then need to perform
myNum = myNum / 10 which will return 1234 and assign it to myNum

If you continue to do this, at some point myNum will be a single digit. So, the result of remainder = myNum % 10 will be whatever value myNum has ie. the single digit. But, the result of myNum = myNum / 10 will be 0, which is when you would end.

So, as you can see, you do need to divide by 10 until 0, just as the book says.

Hope this helps.

17. ### lee1210 macrumors 68040

Joined:
Jan 10, 2005
Location:
Dallas, TX
#17
Others have said it, and I mentioned it in my posts... you get rid of the last digit by dividing by 10.

Coding involves a lot of math. A lot. Binary math, decimal math, switching between the two. You will need to get a lot more comfortable with it. Sure, a lot of programs are just "words" and not much math, but it all comes down to shuffling bits around. I know it's easier said than done, but... relax. Your family isn't being held hostage, and you must reverse a number with a program or they will be executed. The most important part of learning is the learning part. It takes time.

I was just doing shorthand analysis of the algorithm i was expressing. I was just saying that the user entered 1234, so one variable starts with that in it. The other variable is where you're storing your result, which starts at 0. I then iterated through the loop trying to show the value changes of the variables at each step. This is the analysis that chown33 was explaining.

I have a dozen line solution, as I'm sure many others do, but I don't want to post it and ruin it for you. Your existing code doesn't do anything with the digit you get from % 10. You need to print it or store it. You need to store userNumber / 10 to itself, you can do that like this:
userNumber /= 10;
or userNumber = userNumber / 10;
Presently you are not altering userNumber ever.

Take it easy, leave it for the night if you have to. It's not productive to smash your face into something over and over. Time away from the computer can make these things stick sometimes. After you take a break, you can re-read people's input here, and start fresh. Don't be married to code, throwing it away doesn't cost you anything. Even if you were headed in a right direction, having a clean slate can make it easier to think than trying to wedge the solution into the bit of code you started.

-Lee

18. ### chown33 macrumors 604

Joined:
Aug 9, 2009
Location:
Sailing beyond the sunset
#18
Try writing the steps out in English, not pseudo-code. Example:

oneDigit and userNumber are numeric variables.
("Numeric variable" means you can perform arithmetic on it.)
Top of loop:
1. set oneDigit to userNumber mod 10.
2. print oneDigit.
3. set userNumber to userNumber divided by 10.
4. continue at top of loop only while userNumber is non-zero.
I leave it to you to write out example values at each step, and walk through it on paper. Assume the input value of userNumber is 123.

I'm recommending this partly because I think you're confusing yourself by writing pseudo-code, and partly because your pseudo-code (as posted above) is making a lot of assumptions. For example, it's assuming that something is being printed, that the %10 calculation is being assigned to another variable (or to userNumber, which would be wrong), that resultNumber outside the loop is meaningful, etc.

Analysis also involves exposing assumptions, and making them explicit and clearly stated.

19. ### cybrscot thread starter macrumors 6502

Joined:
Dec 7, 2010
Location:
Somewhere in Southeast Asia
#19
Gotcha, going to step away and go to sleep, this can't be good for my health long term. I just feel like an idiot. Doing this makes me feel stupid. I put up half cocked code that makes me look like a newb. Then I get hours of tips and hints and I'm still too dense to figure it out. There must me someone among you that feels the same way, like "why waste our time with this guy, he can't figure it out." In reality I don't think I'm an idiot. I understand economics and international trade theory. I give myself props for taking the initiative to set out to learn this on my own. (trying to rebuild my self esteem here) my pseudo code sucks too, but the book doesn't teach that. I picked it up from you guys, looked at it on the web, and I'm trying to put something together on paper/pseudo code before I begin typing. I feel like I'm trying to build a rocket from scratch. I know I should go to sleep, I know that stopping doesn't mean quitting. But somewhere in my mind I must link the two, because I feel like if I stop, I quit. At least subconsciously anyway. I set out to "do" this problem tonight. For me I guess "do", meant complete, not just get started and finish some other day. So a feeling of a lack of accomplishment tonight. Try again tomorrow.

20. ### chown33 macrumors 604

Joined:
Aug 9, 2009
Location:
Sailing beyond the sunset
#20
I often start a design by writing down notes. Not pseudo-code, just notes.

There will be some paragraphs about "It must do X" or "It should do Y". Those will be Requirements notes. There will be other paragraphs that look like steps in some procedure. Those will be Algorithm Design notes. Write it all down and organize it later.

The parts that look like algorithm design notes get broken down even further. Step by step. With example values. Still no pseudo-code, at least not anything that looks like C. Numbered steps and brief sentences only. Sequence and logic are the keys at this stage.

If some calculation is important, I'll write it down and also explain why it's important. Again, this is for logic checking (does the logic of the explanation make sense, do the logical conditions that lead to this calculation still hold true). Often enough, the exercise of explaining a calculation will show shortcomings of it, or it will show that it's logically wrong and have to be discarded. No problem, I still have the notes on what's needed, so coming up with a new algorithm is mainly a matter of clearly understanding what I want (outputs) and what I have (inputs).

I started doing "notes" instead of "pseudo-code" when I realized that pseudo-code wasn't giving enough "why", yet having the "why behind the code" was instrumental to actually producing good code.

And yes, I still do the "walk it through on paper" thing a lot, too. I'm usually working from my typed-in notes, but I still write out examples on paper. It's like having the printout from a printing calculator, so you can see every past step and what the value was.

21. ### ender land macrumors 6502a

Joined:
Oct 26, 2010
#21
I think 90% of code is written in 10% the time.

The other 10% takes 90% of the time

(probably a worse ratio than that, something like 95/5)

Programming has a period at the beginning which is really frustrating and eventually your knowledge base is built up enough that you can really go places. But getting to that point takes a lot of effort, frustration, and hours spent staring at a program to find things which seem so obvious once you see them.

I had a friend yesterday do something like

if (X1 >= x2 <= x3) {
//stuff
};

which had him completely hung up because it compiles fine but has completely weird runtime results. (you HAVE to have an "And" there with two conditions, btw, so it looks like

if (X1 >= x2 and X1 <= x3) {
//stuff
};

)

Anyways, my point is that this stuff takes time, frustrations, and efforts - you are perfectly justified in feeling that way - it happens to most everyone. Especially learning on yoru own with only an online community for support.

22. ### pianojoe macrumors 6502

Joined:
Jul 5, 2001
Location:
N 49.50121 E008.54558
#22
Code:
```#include <stdio.h>

int main (void) {
int Input,Result=0;

printf ("Enter a number: ") ;
scanf ("%d", &Input) ;
while(Input) {
Result = Result * 10 + Input % 10;
Input = Input / 10;
}
printf("Result: %d\n",Result);

return 0;
}
```

23. ### wlh99 macrumors 6502

Joined:
Feb 7, 2008
#23
Don't beat yourself up. The hardest problems I have figured out have been lying in bed. You brain isn't working full force while you sit at the computer. There is also a period where you have to learn to think differently. That too, probably happens best away from the computer.

Consider, if you had finished it in one night that would imply you already at least mostly knew how to write the program.

I'm going to visit Europe soon and picked up some language CD's. How many times does a person normally need to listen to a half hour lesson before he knows the material? Do you think anyone can listen to the 4 hour course all at once and speak German?

24. ### Bill McEnaney macrumors 6502

Joined:
Apr 29, 2010
#24
Please try to treat yourself gently, Scott.

Years ago, Kekule worked hard to discover the structure of the benzine molecule. One night, when he dozed beside his fireplace, flames "danced" around in a ring. Kekule rant to his lab, where he found out that the benzine molecule was ring-shaped.

Your subconscious mind keeps working on problems, even when you're thinking about something. If you're like me and Kekule, you'll enjoy some unexpected "aha moments" when solutions to problems will seem to pop almost magically into your head.

Relax in a chair, shut your eyes, and imagine every detail of the problem you need to solve. Do that as though you're already solving the problem, because your subconscious mind can't tell the difference between appearance and reality. The idea is to program your subconscious mind to solve the problem when you're doing other things.

My buddy Tim is a mathematician with a P.h.D. in mathematics. When we were in college together, he'd wake up at 1:00 in the morning because some new mathematical idea had just occurred to him. He filled notebook after notebook with what he calls his "bibbling and scribbling," is mathematical thoughts, many of which came to him when he slept.

25. Feb 17, 2011
Last edited: Feb 18, 2011

### cybrscot thread starter macrumors 6502

Joined:
Dec 7, 2010
Location:
Somewhere in Southeast Asia
#25
Thanks, man, I still wasn't going in this direction at all. I don't think I would've got this one. Not even close to what I was thinking, even with Lee's notes. I never understood that I would need to set an int variable to 0, only to multiply it by 10, which still gives 0 then add to the input % 10. I kept thinking only about % 10, again and again.

I was starting to think that maybe I would take the userNumber %10 before the while loop began, and pull out my first least significant digit there. Then work with the rest inside the loop. That's where I was going to go with this today. I need to think through your code, reverse engineer it, and see "why" it works, then I can understand it. I was going nowhere with this one. No, aha! Moment yet!

What I want to know is how did you know you needed a 2nd int variable and set it to 0? That's the part I'm most missing. At what point in the thought process did you see you needed another variable and how did you know you would need it set to 0 and use it the way you did?

Another thing I didn't know was your while (input).. I have thought up to this point that it had to be while (expression), by expression I thought something like while (something = something) or while (something > something), or while (something != something). That while (input) totally threw me, I would've never done it, because I didn't know it could be done.

Bill, wlh99, and ender land. Thanks for your moral support as well. Yes this is the most challenging thing I've learned. Nice to be reassured that it is, or at least was difficult for many of you at one time, that your not all genius savants!