Please help newbie:

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2007
Posts: 10
Reputation: shannonpaul is an unknown quantity at this point 
Solved Threads: 0
shannonpaul shannonpaul is offline Offline
Newbie Poster

Please help newbie:

 
0
  #1
Aug 27th, 2007
I need to display 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 … I “must” use the “for” …This is what I have but it only counts by two. I don’t want the answer…however can you please give me some clues on the proper way to add the prior two numbers to get the needed sum?




  1. #include <iostream>
  2.  
  3. using std::cout;
  4. using std::endl;
  5.  
  6.  
  7. //
  8. int main()
  9. {
  10. int count = 0;
  11.  
  12. for ( int count = 1; count <= 55; count = count + 2)
  13. cout << count << endl;
  14.  
  15.  
  16. return 0;
  17. } //end of main function
Last edited by Ancient Dragon; Aug 27th, 2007 at 8:35 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Please help newbie:

 
0
  #2
Aug 27th, 2007
Draw your flow chart first...
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2
Reputation: sathyam38 is an unknown quantity at this point 
Solved Threads: 0
sathyam38 sathyam38 is offline Offline
Newbie Poster

Re: Please help newbie:

 
0
  #3
Aug 27th, 2007
Have 2 variables outside the for loop. These two variables will contain the first 2 values. Display them. Inside the loop add these two variables which will result in third variable. Display the third variable's value. Once added and displayed, move second variable value to first variable, and move third variable value to second variable. The for loop condition should check for the last value of the third variable.

Hope this clarifies your query!!!!!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Please help newbie:

 
0
  #4
Aug 27th, 2007
Originally Posted by shannonpaul View Post
I need to display 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 … I “must” use the “for” …This is what I have but it only counts by two. I don’t want the answer…however can you please give me some clues on the proper way to add the prior two numbers to get the needed sum?





#include <iostream>

using std::cout;
using std::endl;


//
int main()
{
int count = 0;

for ( int count = 1; count <= 55; count = count + 2)
cout << count << endl;


return 0;
} //end of main function
Take a look at http://en.wikipedia.org/wiki/Fibonacci_number
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2
Reputation: sathyam38 is an unknown quantity at this point 
Solved Threads: 0
sathyam38 sathyam38 is offline Offline
Newbie Poster

Re: Please help newbie:

 
0
  #5
Aug 27th, 2007
eg code :

first = 0;
second = 1;
cout << first;
cout << second;

This should come before the for loop.

inside the for loop :

third = first + second;
count << third;
first = second;
second = third;

the for loop should be

for(;third<=55


check with your code with these changes.....
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Please help newbie:

 
0
  #6
Aug 27th, 2007
If you learnt how to draw a flow chart you could then use that to convert into code.

Flow charts help to visualise how problems such as these are solved.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 10
Reputation: shannonpaul is an unknown quantity at this point 
Solved Threads: 0
shannonpaul shannonpaul is offline Offline
Newbie Poster

Re: Please help newbie:

 
0
  #7
Aug 27th, 2007
I still have errors please help?
  1. #include <iostream>
  2.  
  3. using std::cout;
  4. using std::endl;
  5.  
  6.  
  7.  
  8. int main()
  9.  
  10.  
  11.  
  12.  
  13. {
  14. int num1 = 0;
  15. int num2 = 1;
  16.  
  17. cout << num1 << endl;
  18. cout << num2 << endl;
  19.  
  20. for(int num = num1 + num2;)
  21. cout << num << endl;
  22. num1 = num2;
  23. num2 = num;
  24.  
  25.  
  26.  
  27.  
  28. return 0;
  29. } //end of main function
Last edited by Ancient Dragon; Aug 27th, 2007 at 8:36 am. Reason: code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 10
Reputation: shannonpaul is an unknown quantity at this point 
Solved Threads: 0
shannonpaul shannonpaul is offline Offline
Newbie Poster

Re: Please help newbie:

 
0
  #8
Aug 27th, 2007
some one please help
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,639
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1497
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Please help newbie:

 
0
  #9
Aug 27th, 2007
what are the errors. maybe you need to add braces { and } around lines 21, 22 and 23 to make them all part of the loop.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 10
Reputation: shannonpaul is an unknown quantity at this point 
Solved Threads: 0
shannonpaul shannonpaul is offline Offline
Newbie Poster

Re: Please help newbie:

 
0
  #10
Aug 27th, 2007
  1. #include <iostream> using std::cout;using std::endl; int main() { int num1 = 0; int num2 = 1; cout << num1 << endl; cout << num2 << endl; for(int num = num1 + num2;) cout << num << endl; num1 = num2; num2 = num; return 0;} //end of main function#include <iostream>
  2.  
  3. using std::cout;
  4. using std::endl;
  5.  
  6.  
  7.  
  8. int main()
  9.  
  10.  
  11.  
  12.  
  13. {
  14. int num1 = 0;
  15. int num2 = 1;
  16.  
  17. cout << num1 << endl;
  18. cout << num2 << endl;
  19.  
  20. for(int num = num1 + num2;)
  21. cout << num << endl;
  22. num1 = num2;
  23. num2 = num;
  24.  
  25.  
  26.  
  27.  
  28. return 0;
  29. } //end of main function
Last edited by stymiee; Aug 27th, 2007 at 12:39 pm. Reason: fixed code tags
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1425 | Replies: 15
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC