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 <iostream>
#include <fstream>
using namespace std;
int data[200][2];
//Reads data from file and stores it in 2D array
int fillArray()
{
int r = 0, c = 0;
char fname[20];

ifstream fin;
//Accepts the file name to read
cout<<\"\ Enter file name: \";
cin>>fname;
//Opens the specified file
fin.open(fname);
//Reads till end of file
while (!fin.eof())
{
fin >> data[r][c];
c++;
if(c == 2)
{
r++; //Increase the row position if column count is 2
c = 0; //Resets the column counter if column count is 2
}
}
//Close the file
fin.close();
//Returns number of rows
return r;
}
//Find out the highest and lowest value
void highLowY(int nr)
{
int big, small, r, c, posB = 0, posS = 0;
//Assumes the first row and first column data is the biggest one
big = data[0][1];
//Assumes the first row and first column data is the smallest one
small = data[0][1];
//Loops till number of rows
for(r = 0; r < nr; r++)
{
for(c = 0; c < 2; c++)
{
//Only read y value so if column is 1
if(c == 1)
{
//If current position of data is bigger than the assumed big then change the big to current value
if(big < data[r][c])
{
big = data[r][c];
posB = r;
}
//If current position of data is less than the assumed big then change the big to current value
if(small > data[r][c])
{
small = data[r][c];
posS = r;
}
}
}
}
//Displays the row number with the value
cout<<\"\ The function reaches its highest value in row number \"<<posB<<\" where the value of x is \"<<data[posB][0]<<\" and the value of y is \"<<data[posB][1];
cout<<\"\ The function reaches its lowest value in row number \"<<posS<<\" where the value of x is \"<<data[posS][0]<<\" and the value of y is \"<<data[posS][1];
}
int main()
{
int r = 0, c = 0, sizef;
sizef = fillArray();
cout<<\"\ Number of Record in the file = \"<<sizef<<endl;
for(r = 0; r < sizef; r++)
{
for(c = 0; c < 2; c++)
cout<<data[r][c]<<\" \";
cout<<endl;
}
highLowY(sizef);
return 0;
}

Output:

Enter file name: data.txt

Number of Record in the file = 10
10 16
13 15
20 22
24 28
30 32
34 36
38 40
42 56
50 52
54 62

The function reaches its highest value in row number 9 where the value of x is 54 and the value of y is 62
The function reaches its lowest value in row number 1 where the value of x is 13 and the value of y is 15

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 c
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 c
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 c

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site