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

CS 1325? Introduction to Programming page: 3 4. Calculate the interpolated resul

ID: 649361 • Letter: C

Question

CS 1325? Introduction to Programming page: 3 4. Calculate the interpolated result for y (meters) using linear interpolation and print the result on the screen. 5. Give the user the chance to enter another number if desired Functional and Other Requirements: Besides the main () function, your program should, at minimum, contain the following function. Note that the exact specifications for the input parameters were deliberately not provided as part of the exercise. double getPosition (, , , ); This function will first scan through the time array to determine which points constitute the ?end points? with respect to the calculation, i.e., the (xi, y1) and (x1, y) points in the above equation. It will then calculate the interpolated value of y according to the equation above, and return the result as a double. Note that because of the way the formula works, if the user enters a value that is exactly equal to a time value in the array, the function should just return the corresponding y (meters) value. This includes the endpoints. For example, if the user enters an 18, the function should return the value 18. [f the user inputs O, just return a 2. The program should be able to accept multiple inputs from the user without having to stop and restart the program every time. After a given distance value is reported, have the program ask the user if he/she wants to continue. If yes, then get another time value from the user. If not, then exit the program.

Explanation / Answer

#include <stdio.h>
#include <conio.h>


double getpos(float time[], float dist[], int, float);

main()
{
   float time[]={0,3,5,8,10,12,15,18};
   float dist[]={2,5,2,6,10.4,13.4,14.8,18};
   float num;
   int arrsize=8,i=0;
   getpos(time,dist,arrsize,num);
  
      
  
  
  
  
}
double getpos(float time[], float dist[], int arrsize, float num)
{
   int i=0, c;
   float yi,xi,yii,xii,x,y;
   Repeat:
   printf("Enter a number between 0 and 18 inclusive: ");
   scanf("%f",&num);
   x=num;
   if(num>18||num<0)
   {
       printf("Number is beyond the range. Please re-enter. ");
       goto Repeat;
   }
   while(i<arrsize)
   {
       if(num==time[i])
       {
           xii=time[i];
           xi=time[i-1];
           yii=dist[i];
           yi=dist[i-1];
           y=dist[i];
           break;
       }
       else if(num<time[i])
       {
           xii=time[i];
           xi=time[i-1];
           yii=dist[i];
           yi=dist[i-1];
           y= yi+ (yii-yi)/(xii-xi)*(x-xi);
           break;
       }
       else
       i++;
   }
   printf(" The Number lies between time values of %f and %f;",xi,xii);
  
  
  
   printf(" The interpolated valueof y is %f",y);
   printf("
   1. Continue 2. Exit ");
   scanf("%d",&c);
   if(c==1)
   {
       goto Repeat;
   }
  
      
}