The purpose of this function is to return the index i of the segment [xi, Xi+1] that contains x. Its input arguments are: int n: the size of the grid (number of grid points) double *xi: the array of size n containing the grid coordinates double x: the coordinate of the point that we want to locate There are 4 mistakes (bugs) in this function. Find them and fix them! Answer: (penalty regime: 10, 20, ... %) Reset answer 1 2 3 4- 5 6 7 8 9 10 11 68694AWNE 14 15 16 17 18 19 20 E221222222222222223333 Given an array of coordinates xi for an * ordered uniform grid of size n and a * value x, locate the index i for which x \in [xi[i], xi [i+1]). (We assume that *the grid is given in incremental order * * i.e. xi[i+1] - xi[i] > 0) * int locate(int n, double *xi, double x) { int idx; double a, b, dx; /* assign edge values of interpolation interval */ a = xi [0] b = xi[n-1]; /* Calculate step size */ dx = (b-a)/(n); /* Special treatment if x-b */ if (x b) return n-1; /* Check that x \in [a,b] */ if (x b) { 23 24 25 26 } 27 28 29 30 32} printf("ERROR: Point lies outside the grid.\n"); return -1; Find index assuming uniform grid */ idx round((x-a)/dx); return idx;
The purpose of this function is to return the index i of the segment [xi, Xi+1] that contains x. Its input arguments are: int n: the size of the grid (number of grid points) double *xi: the array of size n containing the grid coordinates double x: the coordinate of the point that we want to locate There are 4 mistakes (bugs) in this function. Find them and fix them! Answer: (penalty regime: 10, 20, ... %) Reset answer 1 2 3 4- 5 6 7 8 9 10 11 68694AWNE 14 15 16 17 18 19 20 E221222222222222223333 Given an array of coordinates xi for an * ordered uniform grid of size n and a * value x, locate the index i for which x \in [xi[i], xi [i+1]). (We assume that *the grid is given in incremental order * * i.e. xi[i+1] - xi[i] > 0) * int locate(int n, double *xi, double x) { int idx; double a, b, dx; /* assign edge values of interpolation interval */ a = xi [0] b = xi[n-1]; /* Calculate step size */ dx = (b-a)/(n); /* Special treatment if x-b */ if (x b) return n-1; /* Check that x \in [a,b] */ if (x b) { 23 24 25 26 } 27 28 29 30 32} printf("ERROR: Point lies outside the grid.\n"); return -1; Find index assuming uniform grid */ idx round((x-a)/dx); return idx;
Advanced Engineering Mathematics
10th Edition
ISBN:9780470458365
Author:Erwin Kreyszig
Publisher:Erwin Kreyszig
Chapter2: Second-order Linear Odes
Section: Chapter Questions
Problem 1RQ
Related questions
Question
Using C++
![The purpose of this function is to return the index i of the segment
[xi, Xi+1]
that contains x.
Its input arguments are:
int n: the size of the grid (number of grid points)
double *xi: the array of size n containing the grid coordinates
double x: the coordinate of the point that we want to locate
There are 4 mistakes (bugs) in this function. Find them and fix them!
Answer: (penalty regime: 10, 20, ... %)
Reset answer
1
2
3
4-
5
6
7
8
9
10
11
68694AWNE
14
15
16
17
18
19
20
E221222222222222223333
Given an array of coordinates xi for an
* ordered uniform grid of size n and a
* value x, locate the index i for which
x \in [xi[i], xi [i+1]). (We assume that
*the grid is given in incremental order
*
* i.e. xi[i+1] - xi[i] > 0) *
int locate(int n, double *xi, double x) {
int idx;
double a, b, dx;
/* assign edge values of interpolation interval */
a = xi [0]
b = xi[n-1];
/* Calculate step size */
dx = (b-a)/(n);
/* Special treatment if x-b */
if (x b) return n-1;
/* Check that x \in [a,b] */
if (x <a && x > b) {
23
24
25
26
}
27
28
29
30
32}
printf("ERROR: Point lies outside the grid.\n");
return -1;
Find index assuming uniform grid */
idx round((x-a)/dx);
return idx;](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F5704136c-277b-4154-88f1-e18dbecfa29c%2F3931414a-6dcd-4c86-a1b1-a294b59d216d%2F0xhgghv_processed.jpeg&w=3840&q=75)
Transcribed Image Text:The purpose of this function is to return the index i of the segment
[xi, Xi+1]
that contains x.
Its input arguments are:
int n: the size of the grid (number of grid points)
double *xi: the array of size n containing the grid coordinates
double x: the coordinate of the point that we want to locate
There are 4 mistakes (bugs) in this function. Find them and fix them!
Answer: (penalty regime: 10, 20, ... %)
Reset answer
1
2
3
4-
5
6
7
8
9
10
11
68694AWNE
14
15
16
17
18
19
20
E221222222222222223333
Given an array of coordinates xi for an
* ordered uniform grid of size n and a
* value x, locate the index i for which
x \in [xi[i], xi [i+1]). (We assume that
*the grid is given in incremental order
*
* i.e. xi[i+1] - xi[i] > 0) *
int locate(int n, double *xi, double x) {
int idx;
double a, b, dx;
/* assign edge values of interpolation interval */
a = xi [0]
b = xi[n-1];
/* Calculate step size */
dx = (b-a)/(n);
/* Special treatment if x-b */
if (x b) return n-1;
/* Check that x \in [a,b] */
if (x <a && x > b) {
23
24
25
26
}
27
28
29
30
32}
printf("ERROR: Point lies outside the grid.\n");
return -1;
Find index assuming uniform grid */
idx round((x-a)/dx);
return idx;
Expert Solution
![](/static/compass_v2/shared-icons/check-mark.png)
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 1 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
Recommended textbooks for you
![Advanced Engineering Mathematics](https://www.bartleby.com/isbn_cover_images/9780470458365/9780470458365_smallCoverImage.gif)
Advanced Engineering Mathematics
Advanced Math
ISBN:
9780470458365
Author:
Erwin Kreyszig
Publisher:
Wiley, John & Sons, Incorporated
![Numerical Methods for Engineers](https://www.bartleby.com/isbn_cover_images/9780073397924/9780073397924_smallCoverImage.gif)
Numerical Methods for Engineers
Advanced Math
ISBN:
9780073397924
Author:
Steven C. Chapra Dr., Raymond P. Canale
Publisher:
McGraw-Hill Education
![Introductory Mathematics for Engineering Applicat…](https://www.bartleby.com/isbn_cover_images/9781118141809/9781118141809_smallCoverImage.gif)
Introductory Mathematics for Engineering Applicat…
Advanced Math
ISBN:
9781118141809
Author:
Nathan Klingbeil
Publisher:
WILEY
![Advanced Engineering Mathematics](https://www.bartleby.com/isbn_cover_images/9780470458365/9780470458365_smallCoverImage.gif)
Advanced Engineering Mathematics
Advanced Math
ISBN:
9780470458365
Author:
Erwin Kreyszig
Publisher:
Wiley, John & Sons, Incorporated
![Numerical Methods for Engineers](https://www.bartleby.com/isbn_cover_images/9780073397924/9780073397924_smallCoverImage.gif)
Numerical Methods for Engineers
Advanced Math
ISBN:
9780073397924
Author:
Steven C. Chapra Dr., Raymond P. Canale
Publisher:
McGraw-Hill Education
![Introductory Mathematics for Engineering Applicat…](https://www.bartleby.com/isbn_cover_images/9781118141809/9781118141809_smallCoverImage.gif)
Introductory Mathematics for Engineering Applicat…
Advanced Math
ISBN:
9781118141809
Author:
Nathan Klingbeil
Publisher:
WILEY
![Mathematics For Machine Technology](https://www.bartleby.com/isbn_cover_images/9781337798310/9781337798310_smallCoverImage.jpg)
Mathematics For Machine Technology
Advanced Math
ISBN:
9781337798310
Author:
Peterson, John.
Publisher:
Cengage Learning,
![Basic Technical Mathematics](https://www.bartleby.com/isbn_cover_images/9780134437705/9780134437705_smallCoverImage.gif)
![Topology](https://www.bartleby.com/isbn_cover_images/9780134689517/9780134689517_smallCoverImage.gif)