[OT] Quick, I need help with C++!

Welverin

First Post
THe assignment is posted here.

My problem is that I'm at a complete loss on how to set up a function that will calculate and display a string of Fibonacci numbers based on what is input by the user (i.e. if the user enters 5 the first five fib numbers will be displayed).

I about ready to give up and just submit without that part.
 

log in or register to remove this ad


Try google.

Better yet, try google, then print the links out for your prof assignment, teach him to use such an easy problem for homework.
 

Functions

Ye gods, that's hideously easy.

There are several ways to go about it, but all of them involve control loops, and that is the whole point of this homework exercise; become familiar with and understand the differences between the various loop structures.

I suggest you take a few minutes to think about this one. Plot it in your head; what happens at each step of the loop? What variables do you need? how many variables do you need?

Programming is not just hacking out a solution; programming involves planning and logical, step-by-step thinking. If you haven't already, take a logic class. It should help you with your approach to programming.

Tarek
 

I have to agree with Tarek. It is very easy, if you have troubles doing that you should make a lot more exercises. Have you tried asking your course mates? There are two main ways to do it; one uses recursion, is super easy to implement (3 lines of code), and is extremely inefficient to the point where it'll lock a supercomputer for even a very small N. The other uses a loop and is the right way. It is the classical example used to introduce computational complexity and why you'd better plan your algorithms in advance.

I see that the due date was yesterday; sorry I didn't spot the thread before. :( Do you still need a solution?
 

What the above guys said, it is indeed very easy. You should try to become more familiar with either algorithms or psuedo code, such as

function starts here ( number input by user )
loop until the number input by user is below zero <
get previous number
get the number before previous number
print the sum of the previous number and the previous previous number
remove one from the number input by user
>
function ends here
 

Zappo said:
I see that the due date was yesterday; sorry I didn't spot the thread before. :( Do you still need a solution?

Well I do still want to figure it out, I have the week off so there's no rush at the moment (spring break in winter, imagine that).
So you know this is my first time doing any kind of programming and for what ever the reason I just drew a blank on how to do this part, mostly setting up the formula and a little on how to get it to display everything. I did figure it'd take a loop of some kind, though.

Thanks for the suggestions.
 
Last edited:

Welverin said:


Well I do still want to figure it out, I have the week off so there's no rush at the moment (spring break in winter, imagine that).
So you know this is my first time doing any kind of programming and for what ever the reason I just drew a blank on how to do this part, mostly setting up the formula and a little on how to get it to display everything. I did figure it'd take a loop of some kind, though.

Thanks for the suggestions.

I'll write some basic pseudocode for it, if you're that stuck. You'll still have to figure out how to wite the program itself. (Also, you should name your variables properly -- not just A, B, M, and T.)

Ask for number, store in M
Store 0 in A // Fib. number 0
Store 1 in B // Fib. number 1
Store 0 in T // Temporary variable
If X < 1 then exit
Write 1
If X < 2 then exit
Start loop with I = 2; continue while I <=M; otherwise add 1 to I
...Store the value of A + B in T
...Store the value of B in A
...Store the value of T in B
...Display T // Or the value of B -- they're the same
Loop back
 

Since everyone else has posted enough pseudocode that you should be able to figure it out, I'll go ahead and post the actual code. Please don't re-use this as is, it may not use naming conventions acceptable in your class, and perhaps some of the output is more advanced:

// Function DoFib(x) - find the Fibonacci value of x.
// 03/08/03 Chris Johanson, Twin Rose Software
void DoFib(int nArg)
{
// Declarations
int nTot = 0, nSecond = 0, nFirst = 1;
// For Loop
cout << "Fib value for << nArg << "\r\n"
for (int i = 0; i < nArg; i ++)
{
// Add the first and second values
nTot = nFirst + nSecond;
// Output the total
cout << (i > 0) ? "," : "" << nTot;
// Set the second and first values accordingly
nFirst = nSecond;
nSecond = nTot;
// End of for loop
}
// Cleanup
cout << "\r\n"
}
 

Twin Rose said:
(i > 0) ? "," : ""

Welverin: If you don't get this expression, it means that, if (i > 0) is TRUE, return what's after the ?-mark (","), otherwise if it's false, return what's after the comma ("").
 
Last edited:

Remove ads

Top