?
Solved

producing the fibonacci sequence in a child process

Posted on 2009-04-03
10
Medium Priority
?
512 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 2000 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 2000 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
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!

 
LVL 84

Accepted Solution

by:
ozo earned 2000 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

Technology Partners: 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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Suggested Courses

801 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