Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.4 kB
2
Indexable
Never
#include <iostream>
#include <fstream> // needed for ifstream / fin
#include <cmath>
#include <cstdio>
#include <cstdlib>

using namespace std;

int main() {
    
//DECLARE VARIABLE
int i,j;
double A[10][3], ave_y1, ave_y2, 
double sum, delta_x, min_x, min_i, x_searched;
str[10];

//OPEN THE FILE
ifstream fin_A("file.txt"); 
if (!fin_A) {
cout << "error!"; 
exit(1);
}

//READ THE FILE
fin_A >> str;
fin_A >> str;
fin_A >> str;
for(i=1; i<=10; i++){
    for(j=1; j<=3; j++){
        fin_A >> A[i][j];
    }
}

//CALCULATE THE AVERAGE VALUES OF Y1 AND Y2
sum = 0.0;
for(i=0;i<=10;i++) sum += A[i][1];
ave_y1 = sum/10;
cout << "\naverage of y1=" << ave_y1;
sum= 0.0;
for(i=0;i<=10;i++) sum += A[i][2];
ave_y1 = sum/10;
cout << "\naverage of y2=" << ave_y2;

// THE PART I DON'T UNDERSTAND
x_searched = 1.7; //Understood
min_x = 1.0e10; //Why did you give such a big value to the minimum ?
for(i=0; i<10; i++)
{
    delta_x=abs(A[i][0]-x_searched); // reexplain please
    if(delta_x < min_x)  //reexplain please
    {
        min_x = delta_x; //reexplain please
        min_i = i;
    }
}
cout << "\nx lookup is =" << A[min_i][0];
cout << "\ny1 lookup is =" << A[min_i][1];
cout << "\ny2 lookup is =" << A[min_i][2];

//GET THE INFORMATION FROM THE FILE
ofstream fout("output.csv");
if(!fout){
    exit(1);
}

//PLOTTING
fout << "x, y\n";
for(i=0;i<10; i++){
    fout << A[i][0] << ",";
    fout << A[i][1] << ",";
    fout << "\n";
}
return 0;
}
Leave a Comment