0

I have to evaluate this fraction using a recursive function where "n" is a count of fractions

enter image description here

But I get this errors: "implicit declaration of function" and "conflicting types"

#include <stdlib.h>
#include <stdio.h>

int main(void) {

    printf("Enter size of n\n");
    int n;
    scanf ("%d", &n);

    float result = 2 / recursion(n); //implicit declaration of function
    printf("Result of dividing is = %f", result);

    return 0;
}

float recursion(int n) { //conflicting types

    float res = 2 + recursion(n - 1);

    return res;
}

Help me find out what's wrong with it

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Luchnik
  • 1,067
  • 4
  • 13
  • 31
  • 3
    Put the function declaration `float recursion(int n);` above `main()`. – juanchopanza Sep 28 '14 at 12:05
  • But you need to have a conditional in your recursive function to seperate the initial from the recursive case, and your recursive function should do some division! – Basile Starynkevitch Sep 28 '14 at 12:06
  • 1
    The compiler sees you used `recursion` in `main` but it was not declared there yet. Move the function above it, or add `float recursion(int n);` at the top. (Then you'll find you have other problems--that's another question.) – Jongware Sep 28 '14 at 12:06
  • other than the missing prototype declaration the program has a problem with the way he is calling the recursion. – Haris Sep 28 '14 at 12:09
  • The recursion does not seem to stop (infinite recursion) as there is no check for the base case(where recursion should end), you should add a condition, at which you want to stop the recursion, in the recursive function. As far as conflicting types are concerned, either give a prototype for your recursive function at the top, before main or define the function before main. – Abhishek Choubey Sep 28 '14 at 12:10
  • @Luchnik you should add stop condition & division in recursion function: http://ideone.com/pRuAaS – 4pie0 Sep 28 '14 at 12:15
  • @Luchnik I am at your service sir – 4pie0 Sep 28 '14 at 15:32
  • 1
    @0d0a Beautiful woman. She's very similar to my teacher of English at school. You're lucky – Luchnik Sep 28 '14 at 15:46
  • @Luchnik thank you, focus on mastering your C & C++ anyway ; ) – 4pie0 Sep 28 '14 at 16:16

0 Answers0