![]() |
|
#1
|
|||
|
|||
Problems about simple tricked loop questionThis is a part of large question :
5.The result of the transformation f (explained in the following) applied month times in a row to the year. The transformation f sends a positive integer n to n/2 if n is even and to 3n+1 if n is odd. So if the year is 2007, we see that f(2007) = 6022. If the month is 1 we stop there. Otherwise we apply f again to 6022. We see that f(6022) = 3011 and we stop if the month is 2. And so on. If the month is 5, the value generated should be 13552, since f(3011) = 9034, f(9034) = 4517 and f(4517) = 13552. I wrote a function for this question but it seems can not generate expected output, anyone can help me to work it out ,my code is following: CPP / C++ / C Code:
Last edited by admin II : 12-Apr-2007 at 05:02.
Reason: [tagindicator] should be [CPP] ... [/CPP] for C++ code
|
|||
|
#2
|
|||
|
|||
Re: problems about simple tricked loop questionQuote:
Make the program tell you what is seeing and what it is doing at each step; For example, in main: CPP / C++ / C Code:
In the function: CPP / C++ / C Code:
Regards, Dave |
|
#3
|
|||
|
|||
Re: Problems about simple tricked loop questioncheers .... davekw7x ....i 've already worked it out with ur hint , just let tran=year at the bottom of the function,then repeat it . Thanks a lot !!!!!
|
|
#4
|
|||
|
|||
Re: Problems about simple tricked loop questionwhat was the final code output for that question? i tried 2 do the same thing and it only outputs the number two over 200 times then ends.
|
|
#5
|
|||
|
|||
Re: Problems about simple tricked loop questionQuote:
Here's how it works: 1. Post your code. Tell us what compiler you are using. If there were any compiler messages, paste them into the post. Don't edit; don't paraphrase --- give us the entire message(s). 2. Tell us exactly you gave it as an input 3. Tell us what you expected to get from the program. (Did you put any debug print statements to make the program tell you what it was doing?) Regards, Dave |
|
#6
|
|||
|
|||
Re: Problems about simple tricked loop questionc++ hey ok sorry im new to this, im trying to figure out what is happening in allens code. i inputted this but i have a feeling that i am missing some brackets somewhere along the line. i recieve no errors but my function just isnt agreeing with me. as the function int TransYear( int month, int year ) { int tran; tran = year; for (int i = 1; i <= month; i++) { if (tran % 2 == 0) tran = tran / 2; else tran = 3 * tran + 1; } return tran; } in main cout << TransFormer (year, month) << endl; about two mins after posting this however i realised my mistake ...in which my main was returning (year, month) not month year. cout << TransFormer (year, month) << endl; change to cout << TransFormer (month, year) << endl; thanks for the help |
|
#7
|
|||
|
|||
Re: Problems about simple tricked loop questionQuote:
Quote:
And if you hadn't spotted it, then the print statements that I showed in my previous post would have shouted it at you. Really; it would just have jumped off of the page and yelled in your ear. The point of the exercise is not just to write a program to do some trivial task, but to get people into the mindset of attacking a problem and using the two most important resources at their disposal to solve it: 1. The programmer's brain and the brain extension known as the optical nerve. 2. The program itself. Regards, Dave "We can face our problem. We can arrange such facts as we have with order and method." Hercule Poirot --- in Murder on the Orient Express Last edited by davekw7x : 01-May-2007 at 10:26.
|
Recent GIDBlog
Problems with the Navy (Enlisted) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| a simple question | sharwar | .NET Forum | 3 | 28-Sep-2006 05:18 |
| simple file question | dabigmooish | C++ Forum | 2 | 12-Sep-2006 09:01 |
| my 1st simple program, 3 slight problems. | skizer | C++ Forum | 11 | 07-Feb-2006 15:31 |
| Simple question about while loop | Lej | C++ Forum | 6 | 07-Aug-2005 13:57 |
| Simple question about file opening | eddo | C++ Forum | 3 | 09-Jun-2005 22:04 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The