Solved

producing the fibonacci sequence in a child process

Posted on 2009-04-03
10
506 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now