Please help how do i do this program??

Write a program to read data from a large text file. Within the file, the data are arranged in two columns, separated by white space characters, and comprised of a variable number of rows of data, the last of which contains nine nines (999999999) in each column. An example data file is found on the Blackboard site. The rows of data are x and y pairs of data where x is an angle in degrees and y is a function of that angle. The angle data are spaced irregularly.
Input the data and compute a new set of x and y values at equal integer increments (0,1,2,…) of angle from 0 to 360 degrees. Compute the new y values using linear interpolation. Output new, equally spaced data to a file; include the name of the original data file within the new data file.

This is what i have so far:

#include<iostream.h>
#include<fstream.h>

int main(){
    ifstream fin("prog8.dat");
   float y[1000], x[1000]; //read the two columns
   for (int lcv = 0; lcv < 1000; lcv++)
   fin >> y[lcv]>> x[lcv];
   
   // y fucntion
   y[lcv] = y[lcv-1]+ ((y[lcv+1]-y[lcv-1])/(x[lcv+1]-x[lcv-1])*(x-x[lcv-1]))

Well, keep going... It's early to give up yet. :-) Just one comment. Test for the EOF indicator (999999999 999999999 values read in the file), which is the sign for "done reading". The question is whether you want to generate output as your read input, or wait until all input has been read, storing the values for later output. There are pros and cons for either approach, depending upon what you are trying to accomplish.

Well, keep going... It's early to give up yet. :-) Just one comment. Test for the EOF indicator (999999999 999999999 values read in the file), which is the sign for "done reading". The question is whether you want to generate output as your read input, or wait until all input has been read, storing the values for later output. There are pros and cons for either approach, depending upon what you are trying to accomplish.

Thanks but since i am new to c++ i don't know where to go from here. I think i am trying to output after i have read all of the input. :)

for (int lcv = 0; lcv < 1000; lcv++)
   fin >> y[lcv]>> x[lcv];

Lines 7 and 8 above won't work. That's for a known-ahead-of-time data size. You DO NOT know ahead of time how much data you'll have, so you need to read something in, then check it. That's probably a while loop...

const int MAX = 1000; // don't want to overflow the array!
const int SENTINEL = 999999999; // "sentinel" means "end" (I think :))
int numPairs = 0; // number of pairs you care about (i.e. real data)
bool foundSentinel = false; // haven't found it yet

do
{
    // read data from file
    // see if the data is the sentinel value
    // if it is not, increment numPairs
    // if it is, set the foundSentinel flag to true
} while (numPairs < MAX && !foundSentinel);

// now you have your data in your arrays and you know how many pairs you have.  Do whatever calculations you want.
int main () {
   const int MAX = 1000; 
   const int SENTINEL = 999999999;
   int numPairs = 0; 
   bool foundSentinel = false; // 

do
{
      ifstream fin("prog8.dat");
   float y[1000], x[1000]; //read the two columns
   for (int lcv = 0; lcv < 1000; lcv++)
   fin >> y[lcv]>> x[lcv];  // read data from file
}

i think this is what you were telling me to do? but how do i see if the data is the sentinel value and if not how do i increment numPairs? and how do i set the foundSentinel flag to true. I know for the first one i have to use a boolean expression, but how about the first two? Thanks for the help

Edited 5 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

Better still, use a vector containing elements that are a structure that has the two (left and right) values. The vector can, depending upon your method of adding the data, can automatically resize itself. Then you can just iterate through the vector when you are ready to compute/transform/output etc.

ohhh can you give me an example of what you mean? it can be an example not related to my topic. Please i just want to understand and i appreciate the help

>> i think this is what you were telling me to do?

No. Let me expand my example.

const int MAX = 1000; // don't want to overflow the array!
const int SENTINEL = 999999999; // "sentinel" means "end" (I think :))
int numPairs = 0; // number of pairs you care about (i.e. real data)
bool foundSentinel = false; // haven't found it yet

// open your file BEFORE the do-while loop
ifstream fin("prog8.dat"); // verbatim from your original code
// declare your arrays BEFORE the do-while loop
float y[1000], x[1000]; // verbatim from your original code

do
{
    // read data from file
    fin >> y[numPairs]>> x[numPairs]; // slight modification from your original code to accommodate a new variable name

    // see if the data is the sentinel value
    if(y[numPairs] == SENTINEL)
    {
        // set the foundSentinel flag to true
    }
    else
    {
        // increment numPairs
    }
} while (numPairs < MAX && !foundSentinel);

// close the file

// now you have your data in your arrays and you know how many pairs you have.  Do whatever calculations you want.

>> how do i increment numPairs? numPairs++; >> how do i set the foundSentinel flag to true. foundSentinel = true; Now stick these lines on lines 19 and 23.

Okey i have done that and it all works. Now i am trying to do the calculation. I have an idea of how to go about doing it; yet, i am not able to come up with anything. First i am trying to set up a new x and then compute a new y from x, y, new x . Could you please help me .

Edited 5 Years Ago by hawita: n/a

Thanks for all the help. This is my code so far but the program does not run. It just compiles and crash and i believe this is because it is computing an infinite loop; however, i do not know how to fix the problem. Plz help

#include<iostream.h>
#include<fstream.h>
#include<string>
#include <C:\dislin\dislin.h>

using namespace std;
int main () {
    ifstream fin ("prog8.dat");
    float x[1000], y[1000],  xlin[1000], ylin[1000];
    
    int lcv = 0;
    
    for (lcv  = 0; !fin.eof(); lcv++)
    { 
        fin >> x[lcv] >> y[lcv];
        cout << x[lcv] << y[lcv]<< endl;
        
    }
      
int Npoints;
        Npoints = lcv - 1;
qplot (x,y,Npoints);  

for (int i = 0; i < 360; i++)
{
    int z = 0;
    xlin[i] = i;
    while (x[z] > xlin[i])
    {
        z++;
}

double slope = ((y[z+1]-y[z])/(x[z+1]-x[z])); 
ylin[i] = y[i]+slope * (xlin[i]-x[i]);
cout << xlin[i] <<'\t' << ylin[i] << endl;
}
qplot(xlin,ylin,361);
system("pause");
}

//x[z] > xnew[i]

Run your code in the debugger and see where it has a problem. Also, when you have 360 elements in xlin[] and ylin[], why do you pass 361 as the 3rd argument to qplot()?

This article has been dead for over six months. Start a new discussion instead.