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

1. (15 points) Complete the attached program plot.c to plot a function f (t), e.

ID: 2246704 • Letter: 1

Question

1. (15 points) Complete the attached program plot.c to plot a function f (t), e.g. f (t)-t 2-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 secondI am going to change the definition of f (t) when testing your program: third.Lam 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) * (high - low + 2));
   int i;
   for(i = 0; i < high - low + 1; i++)
       line[i] = ' ';
   line [high - low + 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();
}

Lt me know if this worked!!