views:

151

answers:

3

Yeah right... we are forced to programm some good old C at our university... ;) So here's my problem:

We got the assignment to program a little program that show a fibonacci sequence from 1 to n 1 to 18 works great. But from 19 the program does nothing at all and just exit as it's done. I can not find the error... so please give me a hint. :)

#include<sys/types.h>
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>

int main(int argc, char **argv)
{
  pid_t pid;
  int fib[argc];
  int i, size;
  size = strtol(argv[1], NULL, 0L);
  fib[0] = 0;
  fib[1] = 1;
  pid = fork();
  printf("size = %d \n", size);
  if(pid == 0){
    for(i = 2; i < size; i++){
      fib[i] = fib[i-1] + fib[i-2];
    }
    for(i = 0; i < size; i++){
      printf("\n\t %d ", fib[i]);
    }
  }
  else if(pid > 0){ // Parent, because pid > 0
    wait(NULL);
    printf("\n");
    exit(1);
  }
}

Thanks already!

A: 

I already solved it... what a silly error... I initalized fib[] not good... Sorry ;) It's still very early here.

shevron
I used argc... what's wrong... :) now I use size so everything is fine. :)
shevron
A: 

Still this code doesn't look very good. You haven't posted includes and for-loop is incomplete. It doesn't even contain code that will actually compute the sequence values.

Next problem is the purpose of forking here. What you want to do is to perform simple sequential calculation. There is no need for fork at all.

I recommend a bit more work on this before submitting this for grade.

stmi
I know that I don't need forking here... But it's an assignment. It's not my fault ;)
shevron
+1  A: 

Some issues are:

  1. fib[0] should be 1, not 0.
  2. Size of fib array is wrong.
  3. And array fib is defined wrong too.
  4. Seems that for fill random membery, not fib array elements.
Grzegorz Gierlik
You are wrong about `fib[0]`: http://en.wikipedia.org/wiki/Fibonacci_number
IVlad
Can you add "there is no validation of the command line arguments".
JeremyP