reading file and performing linear interpolation

hey guys i have a text file having 7 columns "x","z","p","v","w","av","aw" of numeric values at different time steps.So i want a c++ code to store these values in different
arrays on which i can operate.My function is of form U(t,z) and returning the "v" and "w" values corresponding to given z and t value.
here is my text file:
TIME= 0.00
9.00000 -0.67000 6.57270 0.00000 0.00000 0.00000 0.00000
9.00000 -0.65029 6.37938 0.00000 0.00000 0.00000 0.00000
9.00000 -0.63059 6.18606 0.00000 0.00000 0.00000 0.00000
9.00000 -0.61088 5.99274 0.00000 0.00000 0.00000 0.00000
9.00000 -0.59117 5.79942 0.00000 0.00000 0.00000 0.00000
9.00000 -0.57147 5.60610 0.00000 0.00000 0.00000 0.00000
9.00000 -0.55176 5.41278 0.00000 0.00000 0.00000 0.00000
9.00000 -0.53206 5.21946 0.00000 0.00000 0.00000 0.00000
9.00000 -0.51235 5.02614 0.00000 0.00000 0.00000 0.00000
9.00000 -0.49264 4.83282 0.00000 0.00000 0.00000 0.00000

TIME= 2.00
9.00000 -0.67000 6.57270 0.00000 0.00000 0.00000 0.00000
9.00000 -0.65029 6.37938 0.00000 0.00000 0.00000 0.00000
9.00000 -0.63059 6.18606 0.00000 0.00000 0.00000 0.00000
9.00000 -0.61088 5.99274 0.00000 0.00000 0.00000 0.00000
9.00000 -0.59117 5.79942 0.00000 0.00000 0.00000 0.00000
9.00000 -0.57147 5.60610 0.00000 0.00000 0.00000 0.00000
9.00000 -0.55176 5.41278 0.00000 0.00000 0.00000 0.00000
9.00000 -0.53206 5.21946 0.00000 0.00000 0.00000 0.00000
9.00000 -0.51235 5.02614 0.00000 0.00000 0.00000 0.00000
9.00000 -0.49264 4.83282 0.00000 0.00000 0.00000 0.00000
9.00000 -0.47294 4.63951 0.00000 0.00000 0.00000 0.00000


TIME= 4.00
9.00000 -0.67000 6.57270 0.00000 0.00000 0.00000 0.00000
9.00000 -0.65029 6.37938 0.00000 0.00000 0.00000 0.00000
9.00000 -0.63059 6.18606 0.00000 0.00000 0.00000 0.00000
9.00000 -0.61088 5.99274 0.00000 0.00000 0.00000 0.00000
9.00000 -0.59117 5.79942 0.00000 0.00000 0.00000 0.00000
9.00000 -0.57147 5.60610 0.00000 0.00000 0.00000 0.00000
9.00000 -0.55176 5.41278 0.00000 0.00000 0.00000 0.00000
9.00000 -0.53206 5.21946 0.00000 0.00000 0.00000 0.00000
9.00000 -0.51235 5.02614 0.00000 0.00000 0.00000 0.00000
9.00000 -0.49264 4.83282 0.00000 0.00000 0.00000 0.00000
9.00000 -0.47294 4.63951 0.00000 0.00000 0.00000 0.00000
9.00000 -0.45323 4.44619 0.00000 0.00000 0.00000 0.00000
9.00000 -0.43352 4.25287 0.00000 0.00000 0.00000 0.00000


TIME= 6.00
9.00000 -0.67000 6.57270 0.00000 0.00000 0.00000 0.00000
9.00000 -0.65029 6.37938 0.00000 0.00000 0.00000 0.00000
9.00000 -0.63059 6.18606 0.00000 0.00000 0.00000 0.00000
9.00000 -0.61088 5.99274 0.00000 0.00000 0.00000 0.00000
9.00000 -0.59117 5.79942 0.00000 0.00000 0.00000 0.00000
9.00000 -0.57147 5.60610 0.00000 0.00000 0.00000 0.00000
9.00000 -0.55176 5.41278 0.00000 0.00000 0.00000 0.00000
9.00000 -0.53206 5.21946 0.00000 0.00000 0.00000 0.00000
9.00000 -0.51235 5.02614 0.00000 0.00000 0.00000 0.00000
9.00000 -0.49264 4.83282 0.00000 0.00000 0.00000 0.00000
9.00000 -0.47294 4.63951 0.00000 0.00000 0.00000 0.00000
9.00000 -0.45323 4.44619 0.00000 0.00000 0.00000 0.00000
9.00000 -0.43352 4.25287 0.00000 0.00000 0.00000 0.00000

TIME= 8.000000000000000E-002
9.00000 -0.67000 6.57270 0.00000 0.00000 0.00000 0.00000
9.00000 -0.65029 6.37938 0.00000 0.00000 0.00000 0.00000
9.00000 -0.63059 6.18606 0.00000 0.00000 0.00000 0.00000
9.00000 -0.61088 5.99274 0.00000 0.00000 0.00000 0.00000
9.00000 -0.59117 5.79942 0.00000 0.00000 0.00000 0.00000
9.00000 -0.57147 5.60610 0.00000 0.00000 0.00000 0.00000
9.00000 -0.55176 5.41278 0.00000 0.00000 0.00000 0.00000
9.00000 -0.53206 5.21946 0.00000 0.00000 0.00000 0.00000
9.00000 -0.51235 5.02614 0.00000 0.00000 0.00000 0.00000
9.00000 -0.49264 4.83282 0.00000 0.00000 0.00000 0.00000


Further if the given z value is not in the "z" list for a particular time step t,i am doing linear interpolation.

........how can i read for the different time step from the file.



here is my code for reading the text file for only one given time step i.e.
-0.67001 6.54438 -0.01078 0.00001 -0.00987 0.00000
-0.65043 6.35235 -0.01081 -0.00014 -0.00981 0.00329
-0.63086 6.16032 -0.01083 -0.00028 -0.00976 0.00657
-0.61129 5.96829 -0.01086 -0.00034 -0.00969 0.00967
-0.59172 5.77623 -0.01089 -0.00038 -0.00961 0.01154
-0.57215 5.58411 -0.01096 -0.00035 -0.00945 0.01071
-0.55258 5.39199 -0.01103 -0.00034 -0.00929 0.00992


#include <fstream>
#include <string>
#include <vector>
#include <iostream>
#include <sstream>

using namespace std;
double interpolation (double x1, double x2, double y1, double y2, double xInt)
{

double yInt = y1 + (y2-y1)/(x2-x1)*(xInt-x1);

return yInt;
}
double U (double z)
{
ifstream theFile("vel.txt");
double depth;
int n=0;
int f=0;
double y[100]={0};

double v[100]={0};


double velocity;

double pressure;
double velocityx;
double velocityy;
double acclx;
double accly;
while (theFile>>depth>>pressure>>velocityx>>velocityy>>acclx>>accly)
{
y[n]=depth;

v[n]=velocityx;

n=n+1;
}

for(int i=0;i<n;i++)
{
if(y[i]==z)
{
velocity=v[i];

}
else if(y[i]<z)
{
f=f+1;
}
else
{
velocity=interpolation (y[f-1], y[f], v[f-1], v[f], z);

break;
}
}
return velocity;
}
int main()
{
double z;
cin>>z;
cout<<"velocity in x direction is "<<U(z);
return 0;


}
closed account (48T7M4Gy)
Multiple posts like this are bad. Not using code tags is even worse.

http://www.cplusplus.com/forum/general/179725/
Last edited on
this post is different from the previous one.........here i need to read the different columns at different time steps...........pls help
as i m a new to this forum i was not kowning about code tags.sorry for ur inconvenience.
Topic archived. No new replies allowed.