Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

I suck, plz help lol...


  • Please log in to reply

#16
yafim

yafim

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 116 posts
Okay, here we go again, i'm getting stuck again..
we have this thing to do where you need to get a number from the user (n) and you need to print this numbers location in the fibonachi series. meaning if you get n=8, then in the fibonachi series (1,1,2,3,5,8,13,21,34....) the location is 6.

This is what i started doing, which is basically nothing, cause i have no clue how to approach this in a recursive way...

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int fibo(int n);
void main();
int n,x;
clrscr();
printf("Enter a number in the fibonachi series:");
scanf("%d", &n);
x=fibo(n);
printf("Your number is in the %d position in the series.", x);
getch();
}
int fibo(int n);
{

}

any tips? (just hints so i could get it by myself)
  • 0

Advertisements


#17
darth_ash

darth_ash

    Member 1K

  • Member
  • PipPipPipPip
  • 1,382 posts
A recursive fibonacci program is quite difficult for new programmers.
I suggest you start of by, doing it in normal (non-recursive) way, first.
Do you know what the fibonacci series is all about?
  • 0

#18
yafim

yafim

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 116 posts
Yeah, a number in the series is the sum of the two numbers before it.
so are you suggesting making the program work in a non-recursive way first? (lol... i havn't done that in like a year but i'll try).
  • 0

#19
darth_ash

darth_ash

    Member 1K

  • Member
  • PipPipPipPip
  • 1,382 posts
Yes, first do it a non-recursive way.
Then try to convert it to recursive.
  • 0

#20
yafim

yafim

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 116 posts
i'm sorry to dissapoint you, but non-recursive is also hard for me, because we havn't done that since last year, it's been like 6 months sincve last time and now i'm finding myself trying to think non-recursive but it doesn't work, now i'm just thinking recursive, but unsucssesfully.
So if you could help me in the recursive way i'll be glad.. i think in this fibonachi thing, i just don't understand how the comp "knows" what the numbers in the series are, i mean if i do (n-1) etc, it will go "8,7,6,5...", not "8,5,3,2...".

hope i didn't confuse you.
  • 0

#21
darth_ash

darth_ash

    Member 1K

  • Member
  • PipPipPipPip
  • 1,382 posts
No, you did'nt confuse me.
Instead of going backward from the number the user has enterned, why don't try going forward (the normal fabonacci way) and keep a counter.
Let me explain in detail, you start of with 1 and 1 here we can start the counter from 2 beacasue we already have the first two number; add the two 1's, which result in 2 and increase the counter by 1. Next you add 1 and 2 and increase the counter by 1, the fibonacci number you get will be 3 and counter is 4, whcih is the posistion of 3.
Hence if you reach 8 in the series, the counter will be 6, which is your final output.
  • 0

#22
yafim

yafim

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 116 posts
I'm trying, but i don't understand how to add the numbers inside the recursive function, because every time i need to add two different numbers (1+1, 1+2, 2+3...), and i don't know how to change the program to do that...
  • 0

#23
darth_ash

darth_ash

    Member 1K

  • Member
  • PipPipPipPip
  • 1,382 posts
Take three variables, viz.: first, second and result.

result = first + second; //this will give us the sum of the two nos.
first = second;             //For the next iteration\recursion we will have the next no.
second = result;          //For the next iteration\recursion we will have the next no.

Let me illustrate how the above code will work, below:

Initial values:
first = 1
second=1

After Iteration 1:
result=2 (i.e. result = first + second;)
first = 1 (i.e. first = second;)
second=2 (i.e. second=result;)

After Iteration 2:
result = 3
first = 2
second = 3

After Iteration 3:
result = 5
first = 3
second = 5
  • 0

#24
yafim

yafim

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 116 posts
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int fibo(int n, int first, int second, int res, int z);
void main(){
int n,pos,first=1,second=1,res=0,z=2;
clrscr();
printf("Enter a number in the fibonachi series:");
scanf("%d", &n);
if (n==1){
printf("Your number is in the first and second position in the series.");
}
pos=fibo(n,first,second,res,z);
printf("Your number is in the %d position in the series.", pos);
getch();
}
int fibo(int n, int first, int second, int res, int z)
{
if (res!=n){
res=first+second;
first=second;
second=res;
z++;
return fibo(n,first,second,res,z);
}
return z;
}


It works, but the only problem is when i enter the number 1, it doesn't work, for all the rest it does.
  • 0

#25
darth_ash

darth_ash

    Member 1K

  • Member
  • PipPipPipPip
  • 1,382 posts
Well done, You nailed it. :tazz: :)

Detecting 1 can be easily done with a small change, you can initalize res to 1 instead of 0 as the initial value.
But, only the 1 at pos.2 will be deteced.
  • 0

Advertisements


#26
yafim

yafim

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 116 posts
Okay, tnx for the help, but now i'v got another excersize (a lot of'em at once.. sry).
in this one, i need to find (in a recursive way) the biggest number in an array. the array is pre-given, i don't have to define it or recieve anything from the user, just print it out, so it's a void function (i think).
So i'm having difficulty approaching this, i already know the exit condition (when there is 1 or no numbers in the array), and i know i need in a recursive way to back down each time on the way to the start of the array, and somehow compare the first number in the array to the second one, and then from those two compare the largest one to the third number in the array and so on.. but give me a clue as to how to do this, cause i dunno how to phrase it in the program... :tazz:
  • 0

#27
darth_ash

darth_ash

    Member 1K

  • Member
  • PipPipPipPip
  • 1,382 posts
Is the approach(logic) you have given compulsory to follow, or can I give an alternative one.
Its your choice.
  • 0

#28
yafim

yafim

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 116 posts
i'm pretty sure mine is the one the teacher wants, but why not show both and we'll see if the other one is better or not.
  • 0

#29
darth_ash

darth_ash

    Member 1K

  • Member
  • PipPipPipPip
  • 1,382 posts
Ok, I'll give you a logic(apporach) for what your teacher said.

First we need a global array and a integer n, which gives the no. elements in the array.

In the recursive-function, we can keep the exit-condition same as what you told earlier, you can check if n is >1.
Now compare if the 1st element in the array is lesser than the second element; if yes, exchange the first and second elements of the array.
The next step(not an else step for the above if-statement) is to shift the array up by one place till only the second element, hence we will get a new second element. Since we have lost the previous 2nd element, we have one less element in the array so reduce n by 1.
And do a recursive call.
When the exit condition is satisfied, the max. no. should be in the 1st element of the array.

Should I illustrate the above with an example?
  • 0

#30
yafim

yafim

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 116 posts
This is what i tried:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int max(int *arr, int n);
void main(){
int x,n,arr[n];
clrscr();
printf("Enter size of array:");
scanf("%d", &n);
x=max(arr,n);
printf("The maximum number in the array is %d", x);
getch();
}
int max(int *arr, int n)
{
int tmp;
if (n>1){
if (arr[n]>arr[n-1]){
tmp=arr[n-1];
arr[n-1]=arr[n];
arr[n]=tmp;
}
else
return max(arr,n-1);
}
return arr[0];
}


but, it says there are a lot of errors in the defining of "n" and arr[n], how do i define the array the size of n?
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP