views:

4067

answers:

5

The problem I am having is that when say for instance the user enters 7, then the display shows:

0 11 2 3 5 8 13 21 child ends.

I cannot seem to figure out how to fix the 11 and why is it displaying that many numbers in the sequence! Can anyone help?

The number of the sequence will be provided in the command line. For example, if 5 is provided, the first five numbers in the Fibonacci sequence will be output by the child process. Because the parent and child processes have their own copies of the data, it will be necessary for the child to output the sequence. Have the parent invoke the wait() call to wait for the child process to complete before exiting the program. Perform necessary error checking to ensure that a non-negative number is passed on the command line.

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

int main()
{
   int a=0, b=1, n=a+b,i,ii;
   pid_t pid;

   printf("Enter the number of a Fibonacci Sequence:\n");
   scanf("%d", &ii);

   if (ii < 0)
      printf("Please enter a non-negative integer!\n");
   else
   {
      pid = fork();
      if (pid == 0)
      {
         printf("Child is producing the Fibonacci Sequence...\n");
         printf("%d %d",a,b);
         for (i=0;i<ii;i++)
         {
            n=a+b;
            printf("%d ", n);
            a=b;
            b=n;
         }
         printf("Child ends\n"); 
      }
      else 
      {
         printf("Parent is waiting for child to complete...\n");
         wait(NULL);
         printf("Parent ends\n");
      }
   }
   return 0;
}
+1  A: 

It looks to me like your program is correctly producing the fibonacci sequence, maybe the first two '1' characters look like an '11' because they are squashed together on your screen?

1800 INFORMATION
Nope. Ellen really is missing the white space between them. Look carefull t the first printf...
dmckee
yeah thats what i figured, but all the other elements of the sequence are seperated by spaces, and then i still have the problem of too many elements, 9 elements displayed when only 7 should be displayed if 7 was inputed by the user
Ellen
+10  A: 

The 11 is actually two 1's with no space between them. The first comes from the second %d here, because b's value is 1:

printf("%d %d",a,b);

The second comes from the first printf in the loop, where n = 1:

printf("%d ", n);
Don Neufeld
Fast fingers wins...
dmckee
+7  A: 

Without the ending space here

printf("%d %d",a,b);

you get into trouble the first time you do

printf("%d ", n);

The most elegant thing you could do would be to change the printf in the loop to prepend the needed space like this:

printf(" %d", n);

That way you're not left with a hanging space at the end...


You get "too many" elements displayed because you haven't counted the one written in first printf...

dmckee
A: 
Abhijeet
A: 

your code has some misleading expressions......

u are getting the value as keyboard input, not as the command line argument... so u can modify it as follows...

        main(int argc, char *argv[])

and then u can get the value and store it as a integer value...

        int num = atoi(argv[1]);

and also u can check that user may enter the value by using...

        if(argc < 2)
        {
            printf("You must enter a value to proceed this operation..\n");
            return;         
        }

and if u enter no 1 there, it will print 0 1. but the answer should be only 0; so to avoid this u can use two if conditions... if(n == 0) printf("0 "); if(n == 1) printf("1 "); by using above two if conditions u can avoid printing output 11 as well....

have a fun by coding..... feel the amazing... bye u all buddies..... give me ur feedback [email protected]

Aza