View Full Version : C++ Help

loldongs

27th January 2006, 02:19 AM

Yo. sup.

I have to write code for a math problem in C++ for part of my C++ course i'm now taking. I have all that I can think of for the code, it compiles and runs...just not correctly. By not correctly I mean it doesn't do the calculation correctly, which is a big problem. Anyways, heres the problem:

Display n terms of the Fibonacci series. Display the sum of the n terms.

The Fibonacci series is an infinite series of numbers as follows:

1, 1, 2, 3, 5, 8, 13, 21 ....

The first two terms of the series are 1. Each successive term in the series is the sum of the

preceding two terms. The user will be asked to supply the number of terms, n.

aight? sounds good. Ok, so heres my C++ code:

#include <iostream>

using namespace std;

int main()

{

double sum, num, prenum, terms;

sum=0;

num=1;

prenum=0;

cout<<"Enter number of terms\n";

cout<<"If you wish to stop the program enter a negative value\n";

cin>>terms;

while (terms<0)

{

sum=num+prenum;

prenum=num;

num=sum;

terms=terms-1;

}

cout<<"The sum of the selected amount of terms is\n";

cout<<'\t'<<sum<<endl;

return 0;

}

Seraphim

27th January 2006, 07:42 AM

You're missing the first term, it looks like. I played around with your algorithm a little bit just now and had it display the entire sequence, this is what it came up with for the first 10 terms.

1.0, 2.0, 3.0, 5.0, 8.0, 13.0, 21.0, 34.0, 55.0, 89.0

Looks alright at first, except it's missing the second 1 at the front of the sequence. It's kind of a lame fix, but if you added a condition inside your loop to accomodate for the first term before letting your algorithm kick in you could get it to count the second 1.0 term

I used a for loop to make things easier when incrementing

for(int x=1;x<=terms;x++)

{

if(x>1)

{

sum=0;

sum=num+prenum;

prenum=num;

num=sum;

}

else

sum=1;

}

Hope that helps. By the way, is it really necessary to use a double for this program? I'm pretty sure the same task could be accomplished with an int, sans the .0 on the end of every number.

Xull

27th January 2006, 08:56 AM

In terms of the original code...

first issue:

while (terms<0)

If you wish to stop the program enter a negative value

The two kind of go against each other :nervous:

It'd actually run through your loop if it's terms > 0 ;)

second, your code actually does run through each number in the sequence (as far as I could tell) but it does it for the prenum variable instead of the num...

and lastly, though its not critical in this case:

By the way, is it really necessary to use a double for this program? I'm pretty sure the same task could be accomplished with an int, sans the .0 on the end of every number.The .0 wouldn't necessarily be noticeable... depending on the compiler you use (and the options), it won't include the .0... (for me it shows both doubles and ints the same unless there actually is a decimal part to it)

I agree though... doubles are slower, take up more memory, and are more unpredictable than ints (gets a little annoying when it does the calculations but is off by .000000000000000001 or whatever)... since we are only dealing with integers, int would probably be a better choice...

Btw... does your system have a debugger? What compiler are you using?

Even if it does, it can help to include some debugging statements...

for example, in this case you could include this in the loop:

cout << sum << '\t' << prenum << '\t' << num << '\t' << terms << endl;helps out a lot to see what the program is actually doing :)

loldongs

27th January 2006, 01:43 PM

for the double instead of int thing...was just me playing around with things. I changed it back to int.

You're missing the first term, it looks like.

In my calculations, initializing prenum to 0 and num to 1 compensated for this.

Seraphim, I added in that for loop you have there. I remember now my teacher recomended using one, i just forgot. However, im still not getting the right sum.

and Xull, I added in that cout to display what value each variable is at the end of the loop as well. pretty handy.

heres my code as of now:

#include <iostream>

using namespace std;

int main()

{

int sum, num, prenum, terms, x;

num=1;

prenum=0;

cout<<"Enter number of terms\n";

cout<<"If you wish to stop the program enter a negative value\n";

cin>>terms;

for (x=1;x<=terms;x++)

{

if (x>1)

{

sum=0;

sum=num+prenum;

prenum=num;

num=sum;

}

else

sum=1;

}

cout<<"Have a nice day\n";

cout<<"The sum of the selected amount of terms is\n";

cout<<'\t'<<sum<<endl;

cout<<'\t'<<sum<<'\t'<<num<<'\t'<<prenum<<'\t'<<terms<<endl;

return 0;

}

Ive tried changing the initialiaztion of prenum from 0 to 1, still same problem.

heres what it says when I run the program:

23 mcall624: ~$ g++ Lab2b.cpp

24 mcall624: ~$ ./a.out

Enter number of terms

If you wish to stop the program enter a negative value

5

Have a nice day

The sum of the selected amount of terms is

8

8 8 5 5

25 mcall624: ~$

Xull

27th January 2006, 03:14 PM

Personally I like using variables as counters where applicable (in this case I think a while loop would do nicely, but... its easier debugging with a for loop in this case :P)

now the assignment is to count x number of terms of the fibonacci sequence... in this case, num is supposed to keep track of that...

so a

sum += num; should take care of that, provided num runs through each number in the sequence.

I included the following debugging statements:

cout<< x << '\t'<<sum<<'\t'<<num<<'\t'<<prenum<<'\t'<<terms<<endl;after the previous statement

and

cout<< "x\tsum\tnum\tprenum\tterms" << endl;before the for loop.

anyway, provided n runs through the correct numbers, sum should be correct as well (though the debugging statements can help with checking that :P)

so... next to update num and prenum: prenum gets num's old value, and num gets prenum's value + its own... since each references the other's [older] value, doing this won't work:

num += prenum;

prenum = num; or for that matter

prenum = num;

num += prenum;

in either case there is a conflict in which value you want and which is available...

so... making a temporary variable to store one of them would be one solution.

say... tnum for example.

tnum = num;

num = num + prenum;

prenum = tnum;now the temporary variable will keep the older number we want to use with prenum while allowing us to update num.

oh and... in case it split up is confusing...

for (x=1;x<=terms;x++)

{

sum += num;

cout<< x << '\t'<<sum<<'\t'<<num<<'\t'<<prenum<<'\t'<<terms<<endl;

tnum = num;

num = num + prenum;

prenum = tnum;

}

loldongs

29th January 2006, 12:32 PM

Ok sweet Xull. I added in the tnum variable and that seems to be what was missing in the algorithm, thanks for that. Now I just have to go search my textbook for how to display the n terms

edit: haha, bitches and blunts. I got it now, everything works.

here is my code as of now:

#include <iostream>

using namespace std;

int main()

{

int sum, num, prenum,tnum, terms, x;

sum=0;

num=1;

prenum=0;

cout<<"Enter number of terms\n";

cout<<"If you wish to stop the program enter a negative value\n";

cin>>terms;

cout<<"The following numbers are the 'n' terms being added, which you just chose.\n";

for (x=1;x<=terms;x++)

{

if (terms>=0)

{

sum += num;

tnum = num;

num = num + prenum;

prenum = tnum;

cout<<'\t'<<prenum<<endl;

}

else

sum=1;

}

cout<<"The sum of the selected amount of terms is\n";

cout<<'\t'<<sum<<endl;

cout<<"Have a nice day\n";

return 0;

}

and here is my code in action:

45 mcall624: ~$ g++ Lab2b.cpp

46 mcall624: ~$ ./a.out

Enter number of terms

If you wish to stop the program enter a negative value

9

The following numbers are the 'n' terms being added, which you just chose.

1

1

2

3

5

8

13

21

34

The sum of the selected amount of terms is

88

Have a nice day

thanks for all the help Xull and Seraphim.

Xull

29th January 2006, 04:34 PM

Might I ask why the if (terms>=0) ... else?

when it gets to this line

for (x=1;x<=terms;x++) it will loop through the block until x<=terms no longer is satisfied...

say terms = -1...

so that line would read

for (x=1; x<= -1; x++)

since x starts at one, 1<= -1 is false, so it doesn't run through the loop...

(or any other case in which terms isn't positive... smallest number of terms for which it loops through the loop is 1: 1<=1)

Basically what i'm saying is that it'll never run the else part ;)

loldongs

29th January 2006, 08:47 PM

hmm yeah good point. I never realized that :P

Would putting the whole loop inside another if branch work? Say something like

if (terms>=0)

{

for (x..what I already have)

{

the algorithm

}

}

else sum=1

or something along those lines work?

Xull

29th January 2006, 09:05 PM

What is the reason for the if statement? This version will run the else, but why?

I'm assuming its meant to be the catch for if a negative value is entered, so to just exit... in that case setting sum=1 doesn't do anything for it... other than oddly report that the sum of negative # of terms is 1?

If all you want it to do is simply handle the exit for the negative terms, you could try putting the if statement after the input for terms and the rest of the code within the if block... then maybe a goodbye message for the else (if you really want one :heh: )

or the other way around... either way same effect... check to see if its negative & display appropriate result depending on the case...

loldongs

29th January 2006, 09:11 PM

the reason for it is if the user inputs the wrong number nothing will f up and the program will just stop. It's not in the question though my instructer said we should have a condition like that.

Xull

29th January 2006, 09:24 PM

well as is

for (x=1;x<=terms;x++)will not be run if terms is less than 1... so if you enter a 0 or less there isn't an issue of it hanging here.

however,

cout<<"The following numbers are the 'n' terms being added, which you just chose.\n";and

cout<<"The sum of the selected amount of terms is\n";

cout<<'\t'<<sum<<endl;

cout<<"Have a nice day\n";will be run... obviously if you enter a negative number this output won't make much sense (especially since the prompt says it'll stop the program)

so anyway i'd suggest including those statements in your if block... so your output makes more sense (and so the if statement actually does something lol)