Thread: Write a program that recursively calculates the X-th Fibonacci number.....

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    9

    Write a program that recursively calculates the X-th Fibonacci number.....

    Hi guys can someone give me an example of code that respects what is asked of this exercise that I found?
    Write a program that recursively calculates the X-th Fibonacci number using a separate process for each call.
    - The X value must be passed as an argument
    - this value must be between 0 and 10

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,910
    Why don't you show us an example of a program that recursively calculates the X-th Fibonacci number?

    Why don't you show us an example of a program that uses a separate process?

    These are two things that you can do along the way to arriving at a desired solution.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2020
    Posts
    9
    I tried to do it, technically it works, I'm not 100% sure that it uses a distinct process for each call, can you confirm it?



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include<unistd.h>
    #include<sys/wait.h>
    
    size_t fibonacci(size_t n)
    {
      pid_t pid1, pid2;
      size_t fib;
      int status;
    
      switch (n) {
      case 0:
        return 0;
      case 1:
      case 2:
        return 1;
      default:
        break;
      }
    
      pid1 = fork();
    
      if (pid1 == 0)
        exit(fibonacci(n - 1));
      else if (pid1 < 0) {
        printf("ERRORE");
        exit(EXIT_FAILURE);
      }
    
      pid2 = fork();
    
      if (pid2 == 0)
        exit(fibonacci(n - 2));
      else if (pid2 < 0) {
        printf("ERRORE");
        exit(EXIT_FAILURE);
      }
      if (waitpid(pid1, &status, 0) == -1) {
        printf("ERRORE");
        exit(EXIT_FAILURE);
      }
    
      fib = WEXITSTATUS(status);
    
      if (waitpid(pid2, &status, 0) == -1) {
        printf("ERRORE");
        exit(EXIT_FAILURE);
      }
    
      return fib + WEXITSTATUS(status);
    }
    
    int main(int argc, char *argv[])
    {
    
      int a = atoi(argv[1]);
      printf("Calcolo fibonacci di %d \n", a);
      printf("L' %so numero di Fibonacci e' %d.\n", argv[1], fibonacci(a));
    
    
    }
    Last edited by Salem; 05-14-2020 at 09:16 PM. Reason: Removed crayola

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    952
    You really need to improve your spacing.

    You seem to be trying to return the fib through the program's exit status.
    That's not going to work.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/wait.h>
     
    int main() {
        if (fork() == 0)
            exit(12345);
        int status = -1;
        wait(&status);
        printf("%d\n", WEXITSTATUS(status));  // prints 57 (12345 % 256) for me
        printf("%d\n", status); // prints 14592 for me
        return 0;
    }
    BTW, don't post code with markup in it. Copy/paste it as plain text.
    Last edited by john.c; 05-14-2020 at 03:46 PM.
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  5. #5
    Registered User
    Join Date
    May 2020
    Posts
    9
    actually my program works ... I wanted to know if for each call I use a different process ... I didn't understand your example with the main ...

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    952
    I didn't realize that the input was restricted to 0 to 10.
    In that case it will sort of work, but it's an incredibly stupid idea.
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  7. #7
    Registered User
    Join Date
    May 2020
    Posts
    9
    for what reason?










  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,162
    > I didn't realize that the input was restricted to 0 to 10.
    Probably because the homework relies on using the extremely limited range of what is allowed in an exit status.

    Other than that, the OPs answer looks like it does what is asked.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-09-2015, 09:24 AM
  2. Replies: 6
    Last Post: 12-01-2014, 03:00 PM
  3. Replies: 3
    Last Post: 02-23-2012, 11:35 PM
  4. Fibonacci number program problem(assembly)
    By ok_good in forum Tech Board
    Replies: 7
    Last Post: 04-07-2006, 07:27 PM
  5. Calculates the number of leaves
    By NightWalker in forum C Programming
    Replies: 4
    Last Post: 09-15-2003, 03:02 AM

Tags for this Thread