![]() |
|
#1
|
|||
|
|||
counter control loops? Aaaah! Help me please!I keep getting a syntax error when I go to compile this, somewhere in the while() statement. I don't see it. I've never done this before, so I need help! Someone please tell me what I'm doing wrong!!!
CPP / C++ / C Code:
Last edited by admin : 05-Oct-2005 at 18:32.
Reason: Please insert your C code between [c] & [/c] tags
|
|||
|
#2
|
||||
|
||||
Re: counter control loops? Aaaah! Help me please!hi..
Please tell the exact error message you get.. I dont find any syntax error.. there is one error(warning in some compilers) in this program.. You declared the main function as CPP / C++ / C Code:
so put a return statement at the end of the program. CPP / C++ / C Code:
Paramesh |
|
#3
|
|||
|
|||
Re: counter control loops? Aaaah! Help me please!Ok, thank you, it compiles now, but the while() statement won't run through more than once. It should for i <= N, right? So when i gets assined a value bigger than N it should stop and print N? Right now it always prints out 2, which means it's only gone through the statement once.
![]() |
|
#4
|
||||
|
||||
Re: counter control loops? Aaaah! Help me please!hi...
that is the logical error you have in your program.. you declare fminus1 and fminus2 as: CPP / C++ / C Code:
Then you get the value of N by: CPP / C++ / C Code:
initially your i value is 3.. consider that you give the value of N as 5.. so in the first loop: CPP / C++ / C Code:
3<=5 is true.. so the while loop will execute.. then you change the value of N by this: CPP / C++ / C Code:
N=1+1=2; fminus1=2; fminus2=2; there is your problem... everytime you run the loop, the value of N automatically changes to 2.. so in the next loop, i = i+1 = 4 ; while(i<=N) is while(4<=2) which is false and the while loop does not execute.. and you get the output as two always...... How to remove this logical error? use an extra variable. assign the value of N to that variable. that is done by: CPP / C++ / C Code:
Quote:
and Please tell us what the program is all about... Bye.. Paramesh. |
|
#5
|
|||
|
|||
Re: counter control loops? Aaaah! Help me please!My program is supposed to give the Nth term in the set of fibonacci numbers. (1, 1, 2, 3, 5, 8, ...) Now that I have made the variable temp, the program outputs 0 for any input. I'm not sure what to do.
|
|
#6
|
|||
|
|||
Re: counter control loops? Aaaah! Help me please!Quote:
1. Get out a pencil and paper. 2. Decide on a value for N: say you want to use N = 4 3. Write down the values fminus1 = 1, fmunus2 = 2, i = 3, N = 4. 4. Pretend you are the computer, and step through the loop. Write down each new value as you calculate it. 5. Back to the drawing board. Don't have pencil or paper? Then make the computer tell you what's happening to your Fibonacci number sequence: CPP / C++ / C Code:
It's called debugging, and if you are like most of us, you will spend lots of time scratching your head and trying to figure out what the heck is happening. Your two most important important tools: 1. Your "little gray cells". 2. The computer Regards, Dave |
|
#7
|
||||
|
||||
Re: counter control loops? Aaaah! Help me please!hi..
the algorithm of generating fibonacci series is this: 1. have three variables say a, b and c. 2. initially a=0 and b=1 3. compute c=a+b; 4. print c. c is the variable that has the fibonacci numbers 5. assign a=b and b=c 6. go to step 3 again hope you understand this algorithm. bye, Paramesh |
Recent GIDBlog
GID Spam Detector 1.1.0 by gidnetwork
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| MS DataGrid control... | shinyhui | MS Visual C++ / MFC Forum | 0 | 22-Aug-2005 01:45 |
| [winapi] edit control is lying to me... | ubergeek | C Programming Language | 8 | 15-May-2005 18:02 |
| Reading and Writing to a text file | raptorhawk | C++ Forum | 16 | 14-Apr-2005 13:09 |
| Help! Some basal questions about MFC | xutingnjupt | MS Visual C++ / MFC Forum | 1 | 05-Dec-2004 03:38 |
| Detecting a mouse click outside of control | arran.s | MS Visual C++ / MFC Forum | 1 | 27-Oct-2004 06:01 |
Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The