Please only answer if it is correct! #include <stdio.h> #include <limits.h> #inc
ID: 2247045 • Letter: P
Question
Please only answer if it is correct!#include <stdio.h> #include <limits.h> #include <math.h>
int f(int);
int main(void){ int i, t, funval; // Make sure to change low and high when testing your program int low=-3, high=11; for (t=low; t<=high;t++){ printf("f(%2d)=%3d ",t,f(t)); }
// Your code here... return 0; }
int f(int t){ // example 1 return (t*t-4*t+5); // example 2 // return (-t*t+4*t-1); // example 3 // return (sin(t)*10); // example 4 // if (t>0) // return t*2; // else // return t*8; }
. (15 points) Complete the attached program plot.c to plot a function f (t),e.g. f (t)-t2-4t+5 for values of t between two values as specified as two variables low and high on line 11 in plot.c. Note. first nested loops are NOT allowed in your program secondIam the definition of f (tl when testi ng your program:third.am g low and high when testing your program, Please find a few example f (t) shown as comments in the bottom ofplot.c. The following are their corresponding outputs
Explanation / Answer
#include<stdio.h>
int low, high;
int f_min, f_max;
int f(int t) {
return t*t + 4*t + 5;
}
void calc_min_max() {
int i;
f_min = f_max = f(low);
printf("f(%d) = %d ",low, f_min);
int y;
for (i = low+1; i <= high; i++) {
y = f(i);
printf("f(%d) = %d ",i, y);
if (y > f_max)
f_max = y;
else if (y < f_min)
f_min = y;
}
}
char *get_new_line() {
char *line = malloc(sizeof(char) * (f_max - f_min + 2));
int i;
for(i = 0; i < f_max - f_min + 1; i++)
line[i] = ' ';
line [f_max - f_min + 1] = '';
return line;
}
void print_plot() {
int i;
char *line;
printf(" ");
for (i = f_min; i <= f_max; i=i+5)
printf("%-5d",i);
printf(" ");
for (i = low; i<=high; i++) {
line = get_new_line();
line[f(i) - f_min] = '*';
printf("t = %3d%s ",i,line);
free(line);
}
}
int main() {
printf("Enter lower limit: ");
scanf("%d",&low);
printf("Enter upper limit: ");
scanf("%d",&high);
calc_min_max();
print_plot();
}
let me know if you like the code