944,103 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1661
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 27th, 2007
0

Please help newbie:

Expand 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?




C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shannonpaul is offline Offline
10 posts
since Aug 2007
Aug 27th, 2007
0

Re: Please help newbie:

Draw your flow chart first...
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Aug 27th, 2007
0

Re: Please help newbie:

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!!!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sathyam38 is offline Offline
2 posts
since Aug 2006
Aug 27th, 2007
0

Re: Please help newbie:

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
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Aug 27th, 2007
0

Re: Please help newbie:

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.....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sathyam38 is offline Offline
2 posts
since Aug 2006
Aug 27th, 2007
0

Re: Please help newbie:

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Aug 27th, 2007
0

Re: Please help newbie:

I still have errors please help?
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shannonpaul is offline Offline
10 posts
since Aug 2007
Aug 27th, 2007
0

Re: Please help newbie:

some one please help
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shannonpaul is offline Offline
10 posts
since Aug 2007
Aug 27th, 2007
0

Re: Please help newbie:

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Aug 27th, 2007
0

Re: Please help newbie:

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shannonpaul is offline Offline
10 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Visual C++ 2005 Express Edition IDE Help
Next Thread in C++ Forum Timeline: calculator with password





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC