Solved

producing the fibonacci sequence in a child process

Posted on 2009-04-03
10
509 Views
Last Modified: 2012-05-06
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.
0
Comment
Question by:errang
  • 6
  • 4
10 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 500 total points
ID: 24057525
 fib = ((i - 1) + (i - 2)); //****This computes the value of the fib number
no, it computes 2*i-3
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 500 total points
ID: 24057534
(i-1) is not the same as fib(i-1)
0
 

Author Comment

by:errang
ID: 24057549
oh... so I need to write a recursive loop like always?
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 24057567
you can do it with a recursive function or with an iterative loop, or with a generating function ...
0
 

Author Comment

by:errang
ID: 24057598
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;
}

0
 

Author Comment

by:errang
ID: 24057613
no.. wait, that function's producing

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

0
 
LVL 84

Expert Comment

by:ozo
ID: 24057641
You seem to be calling printf a lot
0
 

Author Comment

by:errang
ID: 24057650
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.
0
 

Author Comment

by:errang
ID: 24057666
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?
0
 

Author Comment

by:errang
ID: 24057675
sweet, I got it to work, thanks a lot =).
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question