Link to home
Start Free TrialLog in
Avatar of errang
errangFlag for Afghanistan

asked on

producing the fibonacci sequence in a child process

Hey, I'm supposed to produce the fibonacci sequence in a child process, this is what I got:

#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/wait.h>

int main(int argc, char **argv){

  int i, fib, end;
  pid_t pid = fork();

  if(pid==0){
    if(argc<2){
      printf("You did not enter the length of the sequence.\n");
      exit(0);
    }

    else {
      end = atoi(argv[1]);

      for(i = 0; i<=end; i++){
        if(i==0)
          printf("0, ");
        else if((i>0)&&(i<3))
          printf("1, ");
        else if(i>=3){
          fib = ((i - 1) + (i - 2)); //****This computes the value of the fib number
          if(i < end)
            printf("%d, ", fib);
          else
            printf("%d. ", fib);
        }
      }
    }
  }
  else if(pid>0){
    wait(NULL);
    printf("\nThe end.\n");
    exit(0);
  }

  return(0);
}

This is what the program produces:
> cc question4.c
> ./a.out 5
0, 1, 1, 3, 5, 7.
The end.

I made sure that the formula I was using was correct (fib(n) = fib(n-1) + fib(n-2)), and I even tried to print out what the value of i was it got incremented, everything looks right, but that's not the fibonacci sequence...

Appreciate any help.
SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of errang

ASKER

oh... so I need to write a recursive loop like always?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of errang

ASKER

like this?

int fib(int end){
  int i;

  if(end == 0){
    i = 0;
    printf("%d, ", i);
  }
  else if((end > 0) && (end < 3)){
    i = 1;
  }
  else{
    i = fib(end - 1) + fib(end - 2);
    printf("%d, ", i);
  }

  return i;
}

Avatar of errang

ASKER

no.. wait, that function's producing

> cc question4.c
> ./a.out 5
1, 1, 2, 1, 3, 1, 1, 2, 5,
The end.

You seem to be calling printf a lot
Avatar of errang

ASKER

I tried it like this too:

int fib(int end){
  int i;

  if(end == 0){
    i = 0;
  }
  else if((end > 0) && (end < 3)){
    i = 1;
  }
  else{
    i = fib(end - 1) + fib(end - 2);
  }

  printf("%d, ", i);
  return i;
}

That produced the same result as well.
Avatar of errang

ASKER

I was just searching the internet, and found this:

int fib(int n)
{
    if (n <= 1)
        return n;

    return fib(n-1) + fib(n-2);

}

That's pretty much it right? But I'm not sure how we print the sequence with that.  Do we have a loop call it?
Avatar of errang

ASKER

sweet, I got it to work, thanks a lot =).