Solved

producing the fibonacci sequence in a child process

Posted on 2009-04-03
10
510 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

749 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