GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 05-Oct-2005, 16:38
stguisy stguisy is offline
New Member
 
Join Date: Oct 2005
Posts: 5
stguisy is on a distinguished road
Question

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:
#include<iostream>
using namespace std;

int main(void)
{
   int N;
   int fminus1 = 1;
   int fminus2 = 1;
   int i = 3;

   cout << "Enter N: ";
   cin >> N;

   while(i <= N)
   {
     N = fminus1+fminus2;
       fminus1 = N;
       fminus2 = fminus1;
     i = i+1;
   }     
   cout << N;
}
Last edited by admin : 05-Oct-2005 at 18:32. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 05-Oct-2005, 17:10
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

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:
int main(void)
but you didnt return any value..
so put a return statement at the end of the program.
CPP / C++ / C Code:
cout << N;
return 0;
}
bye..
Paramesh
  #3  
Old 05-Oct-2005, 17:31
stguisy stguisy is offline
New Member
 
Join Date: Oct 2005
Posts: 5
stguisy is on a distinguished road

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  
Old 05-Oct-2005, 17:57
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

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:
int fminus1 = 1;
int fminus2 = 1;

Then you get the value of N by:
CPP / C++ / C Code:
cin >> N;

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:
while(i<=N)    
is :while(3<=5)
3<=5 is true..
so the while loop will execute..
then you change the value of N by this:
CPP / C++ / C Code:
N = fminus1+fminus2;
fminus1 = N;
fminus2 = fminus1;
so now, the value of the variables will be:
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:
#include<iostream>
using namespace std;

int main(void)
{
int N,temp;         //use an extra variable temp
int fminus1 = 1;
int fminus2 = 1;
int i = 3;

cout << "Enter N: ";
cin >> N;

temp=N;

while(i <= temp)     //use temp to calculate the number of loops
{
N = fminus1+fminus2;
fminus1 = N;
fminus2 = fminus1;
i = i+1;
} 

cout << N;
return 0;
}

Quote:
Please read the guidelines before posting.
Insert your c++ code between the tags as given in the guidelines...

and Please tell us what the program is all about...

Bye..
Paramesh.
  #5  
Old 05-Oct-2005, 20:51
stguisy stguisy is offline
New Member
 
Join Date: Oct 2005
Posts: 5
stguisy is on a distinguished road

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  
Old 05-Oct-2005, 21:18
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,153
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: counter control loops? Aaaah! Help me please!


Quote:
Originally Posted by stguisy
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.

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:
#include<iostream>

using namespace std;

int main(void)
{
   int N;
   int fminus1 = 1;
   int fminus2 = 1;
   int i = 3;

   cout << "Enter N: ";
   cin >> N;

   while(i <= N)
   {
     N = fminus1+fminus2;
     cout << "i = " << i << ": fminus1 = " << fminus1
          << ", fminus2 = " << fminus2
          << ", N = " << N << endl;
     fminus1 = N;
     fminus2 = fminus1;
     i = i + 1;

     cout << "At end of loop: i = " << i << ", N = " << N << endl << endl;

   }     
   cout << N;

   return 0;
}


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  
Old 05-Oct-2005, 23:38
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

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
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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 04: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 · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:44.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.