Finding Golden ratio in fibonacci series

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 1876 Views - Last Post: 23 December 2016 - 01:52 PM Rate Topic: -----

#1 shiftadelete  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 04-November 16

Finding Golden ratio in fibonacci series

Posted 21 December 2016 - 12:49 PM

Hi , I just started coding and doing lots of exercise and there is a problem which is I couldn't. The problem is : display Fibonacci series and its golden ratio.
I wrote this code but it gives golden ratio as "0"

#include<stdio.h>
int main()
{
    int n,i,a,b,c;
    printf("Enter a number: ");
    scanf("%d",&n);
    i=1;
    a=0;
    b=1;
    while(i<=n)
    {
        printf("%d ",a);
        c = a + b;
        a = b;
        b = c;
        i++;
    }
    p=a/b;
     printf("Golden ratio is %lf", p);
    return 0; 
}




Is This A Good Question/Topic? 0
  • +

Replies To: Finding Golden ratio in fibonacci series

#2 jimblumberg  Icon User is offline

  • member icon

Reputation: 5125
  • View blog
  • Posts: 16,051
  • Joined: 25-December 09

Re: Finding Golden ratio in fibonacci series

Posted 21 December 2016 - 12:54 PM

Quote

I wrote this code but it gives golden ratio as "0"

And what exactly did you expect the program to output? What did you input into the program?

By the way where did you define the variable p?

Jim
Was This Post Helpful? 1
  • +
  • -

#3 shiftadelete  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 04-November 16

Re: Finding Golden ratio in fibonacci series

Posted 21 December 2016 - 12:59 PM

I defined the p as "double p;" forgot writing here because I am using phone.
I was expecting from the program give me exact golden ratio. I mean the Last two numbers' division. Like n/n-1

This post has been edited by Skydiver: 23 December 2016 - 07:36 AM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon

Reputation: 5125
  • View blog
  • Posts: 16,051
  • Joined: 25-December 09

Re: Finding Golden ratio in fibonacci series

Posted 21 December 2016 - 01:03 PM

Quote

I was expecting from the program give me exact golden ratio.

Which is? Also what exactly is your program outputting?


Do you realize that when you divide two integers you get an integer result, meaning no fractions? 1 / 10 would yield zero.

Now, knowing that look at your line 18, that calculation produces an integer value that is implicitly converted to a double for the assignment.


Jim

This post has been edited by jimblumberg: 21 December 2016 - 01:03 PM

Was This Post Helpful? 1
  • +
  • -

#5 shiftadelete  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 04-November 16

Re: Finding Golden ratio in fibonacci series

Posted 21 December 2016 - 01:22 PM

View Postjimblumberg, on 21 December 2016 - 01:03 PM, said:

Quote

I was expecting from the program give me exact golden ratio.

Which is? Also what exactly is your program outputting?


Do you realize that when you divide two integers you get an integer result, meaning no fractions? 1 / 10 would yield zero.

Now, knowing that look at your line 18, that calculation produces an integer value that is implicitly converted to a double for the assignment.


Jim


You mean like that
P=(double)a/b; ?

It is still giving 0

View Postjimblumberg, on 21 December 2016 - 01:03 PM, said:

Quote

I was expecting from the program give me exact golden ratio.

Which is? Also what exactly is your program outputting?


Do you realize that when you divide two integers you get an integer result, meaning no fractions? 1 / 10 would yield zero.

Now, knowing that look at your line 18, that calculation produces an integer value that is implicitly converted to a double for the assignment.


Jim


I think the program is getting the first value of a and b that's why it is giving 0. How can I make it take the last value of a and b ?
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon

Reputation: 5125
  • View blog
  • Posts: 16,051
  • Joined: 25-December 09

Re: Finding Golden ratio in fibonacci series

Posted 21 December 2016 - 01:24 PM

Quote

You mean like that

Yes.

Quote

It is still giving 0

So where exactly did you declare your variable p or is it now P?

And what exactly do the variables a and b contain when you do that assignment?

Did you try printing out all the variables in your loop to see what they contain in each iteration of the loop?

Jim
Was This Post Helpful? 1
  • +
  • -

#7 shiftadelete  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 04-November 16

Re: Finding Golden ratio in fibonacci series

Posted 21 December 2016 - 01:28 PM

View Postjimblumberg, on 21 December 2016 - 01:24 PM, said:

Quote

It is still giving 0

So where exactly did you declare your variable p or is it now P?

And what exactly do the variables a and b contain when you do that assignment?

Did you try printing out all the variables in your loop to see what they contain in each iteration of the loop?

Jim


It is not P it is p.

What do you mean with "Did you try printing out all the variables in your loop to see what they contain in each iteration of the loop?" Is a in here last value or first value I am really confused
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon

Reputation: 5125
  • View blog
  • Posts: 16,051
  • Joined: 25-December 09

Re: Finding Golden ratio in fibonacci series

Posted 21 December 2016 - 04:12 PM

Quote

What do you mean with "Did you try printing out all the variables in your loop to see what they contain in each iteration of the loop?"

I mean place a printf() statement inside the loop that prints each of the variables as the loop executes.

Quote

It is not P it is p.

You still didn't answer the main question: Where did you declare that variable? And how??

Did you print the values of a and b after your loop? Are a and b the values you should be using to compute your "Golden Ratio"?

Jim
Was This Post Helpful? 1
  • +
  • -

#9 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3609
  • View blog
  • Posts: 13,021
  • Joined: 08-August 08

Re: Finding Golden ratio in fibonacci series

Posted 22 December 2016 - 08:30 AM

Functions make writing code easier. The code below is C++, but you should be able to convert it to C easily if you know how to dynamically allocate memory. Another possibility would be to set a fixed size array. Using long integers (64 bits) you can't go beyond fibonacci(92) anyway!
long fibonacci(long num) {
	static vector<long> fibs;
	if(fibs.size() < 2) {
		fibs.push_back(0);
		fibs.push_back(1);
	}
	for(size_t i = fibs.size(); i <= num; i++) {
		fibs.push_back(fibs[i-1]+fibs[i-2]);
	}
	return fibs[num];
}


I'll leave it to you to figure out the ratio.
Was This Post Helpful? 1
  • +
  • -

#10 shiftadelete  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 04-November 16

Re: Finding Golden ratio in fibonacci series

Posted 22 December 2016 - 08:35 AM

I won't be able to convert it. I have already wrote codes of "how to find Fibonacci". I need to write golden ratio...

This post has been edited by Skydiver: 23 December 2016 - 07:35 AM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#11 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3609
  • View blog
  • Posts: 13,021
  • Joined: 08-August 08

Re: Finding Golden ratio in fibonacci series

Posted 22 December 2016 - 08:54 AM

Your code to find the fibonacci number is cryptic and confusing. That's why you're having trouble calculating a simple ratio!
You need to do two things to make your code readable:

Use descriptive variable names. Single characters have no meaning, so they make code difficult to read.

Use functions. Functions break up code into manageable components. You wouldn't try to build a house all at once, right? You'd build a frame before adding electrical wiring and plumbing, then insulation, and sheet rock. Each of these is a separate component, or function.
Was This Post Helpful? 2
  • +
  • -

#12 shiftadelete  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 04-November 16

Re: Finding Golden ratio in fibonacci series

Posted 22 December 2016 - 12:29 PM

View Postjimblumberg, on 21 December 2016 - 12:54 PM, said:

Quote

I wrote this code but it gives golden ratio as "0"

And what exactly did you expect the program to output? What did you input into the program?

By the way where did you define the variable p?

Jim


Now I have got some sensible output. But still it is wrong, it is supposed to give 1.6180 :(
Was This Post Helpful? 0
  • +
  • -

#13 shiftadelete  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 04-November 16

Re: Finding Golden ratio in fibonacci series

Posted 22 December 2016 - 12:34 PM

View PostCTphpnwb, on 22 December 2016 - 08:54 AM, said:

Your code to find the fibonacci number is cryptic and confusing. That's why you're having trouble calculating a simple ratio!
You need to do two things to make your code readable:

Use descriptive variable names. Single characters have no meaning, so they make code difficult to read.

Use functions. Functions break up code into manageable components. You wouldn't try to build a house all at once, right? You'd build a frame before adding electrical wiring and plumbing, then insulation, and sheet rock. Each of these is a separate component, or function.

Here is my new output it is giving wrong again...
Was This Post Helpful? 0
  • +
  • -

#14 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3609
  • View blog
  • Posts: 13,021
  • Joined: 08-August 08

Re: Finding Golden ratio in fibonacci series

Posted 22 December 2016 - 04:24 PM

It's nice to have your output, but that is far less important than your code. Always remember that we can only see the limited information you provide.

By the way, if you know anything about recursion, you could easily make a recursive function to do this. It would be slower than what I've shown, but it would work.

This post has been edited by CTphpnwb: 22 December 2016 - 04:29 PM

Was This Post Helpful? 1
  • +
  • -

#15 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6869
  • View blog
  • Posts: 14,355
  • Joined: 16-October 07

Re: Finding Golden ratio in fibonacci series

Posted 23 December 2016 - 07:27 AM

So, instead of being 1.6180, it's closer to 0.6180?

Using your program:
n = 20
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
a = 6765
b = 10946
(double)a/b = 0.618034
(double)b/a = 1.618034



Hope this helps.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2