C program The program to be written must load a 2dimensional
C++ program
The program to be written must load a 2-dimensional array where the first column of the array represents the x-values of a function and the second column of the array represents the y-values of a function. The function represented by the list of (x,y) pairs can be plotted and the integral of the function approximated using the trapezoidal rule which is described below in detail. The (x,y) pairs are provided to the program through an external file. The program must allow for as many pairs as provided in the file up to 200 pairs. The file name must be user specified (sample code supplied below). The program must use a 2-dimensional array that is initialized from data in an external file in order to earn points in the “Program Works” category of the rubric.
The order of work for the main program and the requirements are: 1. Explain to the user what the program does. 2. Query the user for the name of the external file. The data provided in the file must be organized so that each row contains a single (x, y) pair and each subsequent row contains a (x, y) pair where the x-value is a constant x larger than the value of x in the previous row. Also, each row should end with a line feed, including the last row of data. 3. Determine the number of rows in the external file. Report the value to the user as the number of data pairs provided by the file. This could be done with a function that returns the number of data pairs in the file to the main program. 4. Determine the value of x. Report the value to the user. This could be done within the function that counts the data pairs so that function would return the number of data pairs and x. 5. Fill the declared 2-dimensional array with the data from the external file. This could be done with a function such as fillArray function discussed in textbook.
6. Use the data in the array to approximate the value of the definite integral of the function from the first data point to the last data point using the trapezoidal rule. Also, find the index of the highest y-value and lowest y-value. For FULL CREDIT, this work must be done with a function that passes the array, the number of data pairs, and the value of x to the function and returns the calculated approximation of the definite integral along with the index that indicates where the highest y-value can be found and the index that indicates where the lowest y-value can be found. 7. After returning from the function, report to the user: a. the calculated approximation of the definite integral b. the place in the data set where the (x, y) data pair contains the highest y value along with the actual data (i.e. “The function reaches its highest value in row number 12 where the value of x is 8.0 and the value of y is 30.0”) c. the place in the data set where the (x, y) data pair contains the lowest y value along with the actual data (i.e. “The function reaches its lowest value in row number 5 where the value of x is 4.0 and the value of y is -4.25”)
Solution
#include <stdio.h>
void oneWay(void);
void anotherWay(void);
int main(void) {
printf(\"\ oneWay:\ \");
oneWay();
printf(\"\ antherWay:\ \");
anotherWay();
}
/*Array initialized with aggregate */
void oneWay(void) {
int vect[10] = {1,2,3,4,5,6,7,8,9,0};
int i;
for (i=0; i<10; i++){
printf(\"i = %2d vect[i] = %2d\ \", i, vect[i]);
}
}
/*Array initialized with loop */
void anotherWay(void) {
int vect[10];
int i;
for (i=0; i<10; i++)
vect[i] = i+1;
for (i=0; i<10; i++)
printf(\"i = %2d vect[i] = %2d\ \", i, vect[i]);
}
/* The output of this program is
oneWay:
i = 0 vect[i] = 1
i = 1 vect[i] = 2
i = 2 vect[i] = 3
i = 3 vect[i] = 4
i = 4 vect[i] = 5
i = 5 vect[i] = 6
i = 6 vect[i] = 7
i = 7 vect[i] = 8
i = 8 vect[i] = 9
i = 9 vect[i] = 0
antherWay:
i = 0 vect[i] = 1
i = 1 vect[i] = 2
i = 2 vect[i] = 3
i = 3 vect[i] = 4
i = 4 vect[i] = 5
i = 5 vect[i] = 6
i = 6 vect[i] = 7
i = 7 vect[i] = 8
i = 8 vect[i] = 9
i = 9 vect[i] = 10
*/
Here is a more complex program that will demonstrate how to read, write and traverse the integer arrays
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
void intSwap(int *x, int *y);
int getIntArray(int a[], int nmax, int sentinel);
void printIntArray(int a[], int n);
void reverseIntArray(int a[], int n);
int main(void) {
int x[10];
int hmny;
hmny = getIntArray(x, 10, 0);
printf(\"The array was: \ \");
printIntArray(x,hmny);
reverseIntArray(x,hmny);
printf(\"after reverse it is:\ \");
printIntArray(x,hmny);
}
void intSwap(int *x, int *y)
/* It swaps the content of x and y */
{
int temp = *x;
*x = *y;
*y = temp;
}
/* n is the number of elements in the array a.
* These values are printed out, five per line. */
void printIntArray(int a[], int n){
int i;
for (i=0; i<n; ){
printf(\"\\t%d \", a[i++]);
if (i%5==0)
printf(\"\ \");
}
printf(\"\ \");
}
/* It reads up to nmax integers and stores then in a; sentinel
* terminates input. */
int getIntArray(int a[], int nmax, int sentinel)
{
int n = 0;
int temp;
do {
printf(\"Enter integer [%d to terminate] : \", sentinel);
scanf(\"%d\", &temp);
if (temp==sentinel) break;
if (n==nmax)
printf(\"array is full\ \");
else
a[n++] = temp;
}while (1);
return n;
}
/* It reverse the order of the first n elements of array */
void reverseIntArray(int a[], int n)
{
int i;
for(i=0;i<n/2;i++){
intSwap(&a[i],&a[n-i-1]);
}
}
Copy one array into another
There is no such statement in C language which can directly copy an array into another array. So we have to copy each item separately into another array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
int main()
{
int iMarks[4];
short newMarks[4];
iMarks[0]=78;
iMarks[1]=64;
iMarks[2]=66;
iMarks[3]=74;
for(i=0; i<4; i++)
newMarks[i]=iMarks[i];
for(j=0; j<4; j++)
printf(\"%d\ \", newMarks[j]);
return 0;
}
| #include <stdio.h> void oneWay(void); void anotherWay(void); int main(void) { printf(\"\ oneWay:\ \"); oneWay(); printf(\"\ antherWay:\ \"); anotherWay(); } /*Array initialized with aggregate */ void oneWay(void) { int vect[10] = {1,2,3,4,5,6,7,8,9,0}; int i; for (i=0; i<10; i++){ printf(\"i = %2d vect[i] = %2d\ \", i, vect[i]); } } /*Array initialized with loop */ void anotherWay(void) { int vect[10]; int i; for (i=0; i<10; i++) vect[i] = i+1; for (i=0; i<10; i++) printf(\"i = %2d vect[i] = %2d\ \", i, vect[i]); } /* The output of this program is oneWay: i = 0 vect[i] = 1 i = 1 vect[i] = 2 i = 2 vect[i] = 3 i = 3 vect[i] = 4 i = 4 vect[i] = 5 i = 5 vect[i] = 6 i = 6 vect[i] = 7 i = 7 vect[i] = 8 i = 8 vect[i] = 9 i = 9 vect[i] = 0 antherWay: i = 0 vect[i] = 1 i = 1 vect[i] = 2 i = 2 vect[i] = 3 i = 3 vect[i] = 4 i = 4 vect[i] = 5 i = 5 vect[i] = 6 i = 6 vect[i] = 7 i = 7 vect[i] = 8 i = 8 vect[i] = 9 i = 9 vect[i] = 10 */ |








