Write a function that will read data from the attached \"XY.dat\". This data fil
ID: 3768951 • Letter: W
Question
Write a function that will read data from the attached "XY.dat". This data file has one header line The format of every line in the file is the letter ‘x’, a space, the x value, space, the letter ‘y’, space, and the y value. (x 0 y 1 x 1.3 y 2.2) The function has one input argument: the file path of the data file. The function will attempt to open the data file and errorcheck to make sure it was opened. If so, it reads in all x values and y values. Then the function closes the file. The function shall return the x and y vectors as the output arguments.
(XY.dat) data:
x coordinate y coordinate
x 0 y 1 x 1.3 y 2.2 x 2.2 y 6 x 3.4 y 7.4 x 4.2 y 5.5 x 4.4 y 4.5 x 6.2 y 7.8 x 7.7 y 11.1 x 8.2 y 11.5 x 9.9 y 15.2 x 7.2 y 9.5 x 8.9 y 12.5Explanation / Answer
Program:
data file:xy.dat
output:
code to copy:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
//declare variables
size_t size = 20;
vector<char> x(size);
vector<char> y(size);
vector<float> x_value(size);
vector<float> y_value(size);
//Function to read data from the text file (inputFile)
int readData(vector<char> &x, vector<float> &x_value,
vector<char> &y, vector<float> &y_value)
{
char dataFile[30];
//Read the name of the text file from user
cout<<"Enter the name of the data file "<<endl;
cin>>dataFile;
ifstream input(dataFile);
int i=0;
//read 10 records into an array from a data file
while(!input.eof())
{
input>>x[i];
input>>x_value[i];
input>>y[i];
input>>y_value[i];
i++;
}
//close the file
input.close();
return i-1;
}
int main()
{
int count;
count=readData(x,x_value, y,y_value);
cout<<" x values and y values "<<count<<endl;
for (int i=0;i<count;i++)
{
cout<<x_value[i]<<" "<<y_value[i]<<endl;
}
system ("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.