Golden Ratio Help

I have a program and it works in my head, but not in code.

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 4368 Views - Last Post: 03 June 2009 - 03:03 PM Rate Topic: -----

#1 100100001101001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-March 09

Golden Ratio Help

Posted 03 June 2009 - 11:06 AM

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
	int a = 1;
	int b = 1;
	int c;
	if (a < 10) {
		  a + b == c;
		  a = c;
		  cout << ""<< c <<"\n";
		  a + b == c;
		  b = c;
		  cout << ""<< c <<"\n";
		  }
	system("PAUSE");
}



This should give me the Fibonacci sequence, but it doesn't.

(1) + (1) = 2
then I make "a" = "c" so now its
(2) + (1) = 3
then I make "b" = "c" so then it should continue to do this
(2)+(3) = c
etc.

but it doesn't work... help? :D

hopefully bumps are legal... if not.. I appolgize

This post has been edited by 100100001101001: 03 June 2009 - 11:35 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Golden Ratio Help

#2 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Golden Ratio Help

Posted 03 June 2009 - 11:07 AM

hope this helps :)

and if bumps as in meaningless post boosts then no, but if it's to help you, then yes. ask any questions you need help with.

This post has been edited by computerfox: 03 June 2009 - 11:08 AM

Was This Post Helpful? 0
  • +
  • -

#3 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1201
  • View blog
  • Posts: 7,309
  • Joined: 07-September 06

Re: Golden Ratio Help

Posted 03 June 2009 - 11:17 AM

Quote

and if bumps as in meaningless post boosts then no, but if it's to help you, then yes. ask any questions you need help with.

Lol, that is a little confusing.

The common stand on bumps is that they are frowned upon, however if your topic is falling off the first page and hasn't been answered then feel free to bump it to get it back to where people may see it.
Was This Post Helpful? 0
  • +
  • -

#4 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Golden Ratio Help

Posted 03 June 2009 - 11:19 AM

lol sorry beta, but yeah my point was that it's frowned upon, but if you're still not getting it then keep asking questions. bumps are classified as meaningless posts.
Was This Post Helpful? 0
  • +
  • -

#5 100100001101001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-March 09

Re: Golden Ratio Help

Posted 03 June 2009 - 11:30 AM

Thanks! kk, i knew that bumps are frowned upon but ya...

Thank you very much for the snippet, but I don't see what the difference is between that and mine. Isn't mine the same principle?
Was This Post Helpful? 0
  • +
  • -

#6 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Golden Ratio Help

Posted 03 June 2009 - 11:36 AM

i don't believe you have a loop :)
Was This Post Helpful? 0
  • +
  • -

#7 100100001101001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-March 09

Re: Golden Ratio Help

Posted 03 June 2009 - 11:44 AM

I thought an if statement was a loop...
Was This Post Helpful? 0
  • +
  • -

#8 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 916
  • View blog
  • Posts: 3,209
  • Joined: 12-May 09

Re: Golden Ratio Help

Posted 03 June 2009 - 11:46 AM

View Post100100001101001, on 3 Jun, 2009 - 12:44 PM, said:

I thought an if statement was a loop...


Not quite. It's a control statement, but it only gets executed in the one pass through of the statement. It's a one trick pony. Try using a while or something similar.

This post has been edited by xclite: 03 June 2009 - 11:47 AM

Was This Post Helpful? 0
  • +
  • -

#9 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Golden Ratio Help

Posted 03 June 2009 - 11:49 AM

that's what i was gonna say lol

but yeah if statements are not loops. they are used within loops to control certain instances. the only way there would be a loop with an if statement is if you were using recursion, which in many cases Fibonacci is done. the snippet i gave you is simpler than if you wanted to use recursion.
Was This Post Helpful? 0
  • +
  • -

#10 xiii1408  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 42
  • Joined: 30-May 09

Re: Golden Ratio Help

Posted 03 June 2009 - 12:28 PM

a + b == c;

Is there a reason for your using the logical equals operator here? The assignment operator is =.
Was This Post Helpful? 0
  • +
  • -

#11 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Golden Ratio Help

Posted 03 June 2009 - 01:03 PM

What are we expecting a + b == c; to do anyway?

an assignment goes var = expression so this looks backwards to me. I think the only reason you did not get an error is because you use the == operator which makes the entire thing an expression.

a + b is a R-Value expression -- it does not have an address, you can not store a value into it.
Was This Post Helpful? 0
  • +
  • -

#12 tepito240  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 03-June 09

Re: Golden Ratio Help

Posted 03 June 2009 - 01:29 PM

Hey 100100001101001,
So in your mind you have the right idea, but your code seems a bit off. This is a very typical problem in programming when your code doesnt do what you expect it to do.

First off, why are you passing a variable and an array in your main function? Is it part of a bigger program? If not, it should just read

#include <iostream>
using namespace std;
int main(){
...
}


Second, as mentioned before, remember that <code> ==</code> is a logic tester and doesnt assign any values.

Then sudo code would be
-Set a limit to what number you want to go to
-print out the first numbers you have

a+b=c
print c
c+b=d
d+c =a
a+d =b

and so forth and so on...
I know this is a little sloppy and the running time is not very effective but since you set a=10, it seems that you just want it to go up to 10.
If you want it to print out the list up to a veyr large number, i would suggest using a recursive function call with 2 parameters. One that is the sum of the addition and another one that is what the next number added should be.

*mod edit: fixed code tags: :code:
Was This Post Helpful? 0
  • +
  • -

#13 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Golden Ratio Help

Posted 03 June 2009 - 01:37 PM

Quote

First off, why are you passing a variable and an array in your main function? Is it part of a bigger program? If not, it should just read



This is just the standard format for main -- the version you gave is just the lazy shortcut most of us take.
Was This Post Helpful? 0
  • +
  • -

#14 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Golden Ratio Help

Posted 03 June 2009 - 02:00 PM

C++ has certain standards and certain syntax. usually if you have the things in they will work. and even if you don't have int before the main it will work, but it's just a standard for C++. it helps keep the code neat and less confusing. if something is not in the standard location, usually it's frowned upon and i believe professors also deduct points for that.

This post has been edited by computerfox: 03 June 2009 - 02:01 PM

Was This Post Helpful? 0
  • +
  • -

#15 100100001101001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-March 09

Re: Golden Ratio Help

Posted 03 June 2009 - 02:40 PM

Sorry, the internet bummed out.

Thanks for telling me about the if statement.

I used == because my compiler didn't like = .

I will try the for loop. I'll get back when I am done.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2