Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is C programming question. A program is to be compiled using three files: f

ID: 650648 • Letter: T

Question

This is C programming question.

A program is to be compiled using three files: f.c, f.h, and main.c.
f(1)=f(2) =1
for n>2, f(n)=sqrt(f(n-1)+(n-2)), and we want to print out f(10).

I believe I have my file done correctly but wasn't too sure about the output value it was producing.

I have the f.h, as:   

double f(int n);

f.c as:

#include
#include "f.h"
double f(int n){
if(n==1 || n==2){ return 1;}
else{ return (sqrt(f(n-1)+(n-2)));}
}

and finally, main.c as:

#include
#include "f.h"
int main (void) { printf("%f ", f(10));         return 0; }

When it's compiled using makefile, I get f(10) = 3.339953 but I thought f(10) would be equivalent to sqrt((10-1)+(10-2)) which is = sqrt(17) or 4.123105. So my question is, is there an error somewhere in, maybe, f.c?

***note that f(1) and f(2) outputs are both 1, so that's good as intended***  

Thanks!

Explanation / Answer

#include
#include "f.h"
double f(int n){
if(n==1 || n==2){ return 1;}
else{ return (sqrt(f(n-1)+(n-2)));}
}

Your f.c is as above, I see that it is geting recursive and because of that your are not geting required results. when u give n = 10 then it calculates sqrt of all the numbers from 17.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote