Problem 2: The program reads from the keyboard interval start and end values, de
ID: 3676384 • Letter: P
Question
Problem 2:
The program reads from the keyboard interval start and end values, denoted by left and right in the program. The program finds an approximation to the length of the curve the function traces between its endpoints corresponding to x=left and x=right. This is done by adding the lengths of the line segments between successive pairs of points on the curve. The formatted output should match the example provided.
An example run with the program for this problem follows:
$ ./a.out
lab 3(b) solution by <STUDENT NAME>
Enter left and right ends of interval
-1.5 5.5
From
( -1.50, 26.75)
( -1.03, 22.30)
( -0.57, 18.29)
( -0.10, 14.71)
( 0.37, 11.57) ( 0.83, 8.86) 2.75
( 0.83, 8.86) ( 1.30, 6.59) 2.32
( 1.30, 6.59) ( 1.77, 4.75) 1.89
( 1.77, 4.75) ( 2.23, 3.35) 1.48
( 2.23, 3.35) ( 2.70, 2.39) 1.07
( 2.70, 2.39) ( 3.17, 1.86) 0.71
( 3.17, 1.86) ( 3.63, 1.77) 0.48
( 3.63, 1.77) ( 4.10, 2.11) 0.58
( 4.10, 2.11) ( 4.57, 2.89) 0.91
( 4.57, 2.89) ( 5.03, 4.10) 1.30
( 5.03, 4.10) ( 5.50, 5.75) 1.71
Approximate line length=30.485319
A partial C program to accomplish the goals of this problem follows, and there are some missing codes that have to be in to run the porogram:
//lab 3(b) solution by <STUDENT NAME>
#include <stdio.h>
#include <math.h>
#define N 15
void fillVector(int n, double left,double right,
To SegmentLength
( -1.03, 22.30) 4.47
( -0.57, 18.29) 4.04
( -0.10, 14.71) 3.61
( 0.37, 11.57) 3.18
double xValues[],double yValues[]);
double dist(double x1,double y1,double x2,double y2);
double lineLength(int n,double x[],double v[]);
double f(double x);
int main(){
double left,right,length;
double xValues[N+1],yValues[N+1];
printf("lab 3(b) solution by <STUDENT NAME> ");
printf("Enter left and right ends of interval ");
scanf("%lf%lf",&left,&right);
fillVector(N
length= lineLength(N
return 0;
}
void fillVector(int n, double left,double right,
double xValues[],double yValues[]){
// fills arrays xValues and yValues with x and y values
// respectively for function f according to mesh with n equal
// intervals between x=left and x=right
double del,fVal,xVal;
int i;
del=(right-left)/n;
for(i=0;i<=n;i++){
xVal=left
} }
double lineLength(int n,double x[],double v[]){
// finds approximet line length using (x,y) coordinates as
// stored in array x[] and y[] which were filled using fillVector
int i;
double len=0,segmentLen;
printf("From To SegmentLength ");
for(i=1;i<=n;i++){
}
printf("Approximate line length=%lf ",len);
}
double dist(double x1,double y1,double x2,double y2){
// returns Euclidean distance between points (x1,y1) and (x2,y2)
return }
double f(double x){
return (x-5.0)*(x-2.0)+4.0;
}
Explanation / Answer
#include <stdio.h>
#include <math.h>
#define N 15
void fillVector(int n, double left,double right,
To SegmentLength
( -1.03, 22.30) 4.47
( -0.57, 18.29) 4.04
( -0.10, 14.71) 3.61
( 0.37, 11.57) 3.18
double xValues[],double yValues[]);
double dist(double x1,double y1,double x2,double y2);
double lineLength(int n,double x[],double v[]);
double f(double x);
int main(){
double left,right,length;
double xValues[N+1],yValues[N+1];
printf("lab 3(b) solution by <STUDENT NAME> ");
printf("Enter left and right ends of interval ");
scanf("%lf%lf",&left,&right);
fillVector(N
length= lineLength(N
return 0;
}
void fillVector(int n, double left,double right,
double xValues[],double yValues[]){
// fills arrays xValues and yValues with x and y values
// respectively for function f according to mesh with n equal
// intervals between x=left and x=right
double del,fVal,xVal;
int i;
del=(right-left)/n;
for(i=0;i<=n;i++){
xVal=left
} }
double lineLength(int n,double x[],double v[]){
// finds approximet line length using (x,y) coordinates as
// stored in array x[] and y[] which were filled using fillVector
int i;
double len=0,segmentLen;
printf("From To SegmentLength ");
for(i=1;i<=n;i++){
}
printf("Approximate line length=%lf ",len);
}
double dist(double x1,double y1,double x2,double y2){
// returns Euclidean distance between points (x1,y1) and (x2,y2)
return }
double f(double x){
return (x-5.0)*(x-2.0)+4.0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.