Hi please help. Any help would be appreciated :D

I have to write a program to read data from a large text file, analyze it, and output the results of the analysis to a data file. there are two data files to process.
Problem: Using airborne sensors that record GPS position and time, you have collected data on the airflow patterns of a tornado. The time (t) and altitude (Z) data are space-separated in a text file (twister.dat).
-As a first-step in data analysis, read-in the (unknown number of) data points and compute when the sensor changes direction. This occurs when the velocity, ΔZ/Δt, changes sign, passing through zero.
-Output the time and altitude of each occurrence of a change in direction to a new data file, change.dat.

So far this is what i have and i am stuck because i do not know how to find when the derivative goes from negative to positive or from positive to negative.

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

 main () {
    ifstream fin ("prog10.text");
    float x[10000], y[10000], deltaT[10000], deltaZ[10000], derivative[10000];
    int nPoints;
    int lcv = 0;
    cout<< "The velocity changes direction at: " << endl;
    
    for (lcv  = 0; !fin.eof(); lcv++)
    { 
        fin >> deltaT[lcv] >> deltaZ[lcv];
        
    }
    deltaT[lcv] = deltaT[lcv] - deltaT[lcv -1]; // finds how much T changes for each lcv
    deltaZ[lcv] = deltaZ[lcv] - deltaZ[lcv-1]; // finds how much Z chnages for each lcv
    derivative[lcv] = deltaT[lcv]/ deltaZ[lcv]; // equation for derivative
    if(derivative[lcv]>0 && derivative[lcv-1] <0)
    {
    // find when the derivative goes from negative to positive or from positive to negative
}

Dani AI

Generated

Notes and a compact, practical solution (building on points from , , and ):

  • The velocity is ΔZ/Δt (not Δt/ΔZ). Use the stream extraction operator (>>) — it already skips whitespace, so no extra spacing token is needed. Also make sure the filename in code matches the data file (the post mentions twister.dat but the sample uses prog10.text).
  • A simple, robust test for a change of direction is to compute the interval velocities v[i] = (Z[i] - Z[i-1]) / (T[i] - T[i-1]) and report a direction change when consecutive velocities have opposite signs (v[i-1] * v[i] < 0). That indicates a local max/min at the shared sample (T[i], Z[i]).

Example (streaming, memory-light) implementation:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>

int sgn(double x){ return (x>0) - (x<0); }

int main(){
    std::ifstream in("twister.dat");
    std::ofstream out("change.dat");
    if(!in || !out) return 1;

    double t0,z0, t1,z1;
    if(!(in >> t0 >> z0)) return 0;
    if(!(in >> t1 >> z1)) return 0;

    double v_prev = (z1 - z0) / (t1 - t0);
    int sign_prev = sgn(v_prev);
    out << std::fixed << std::setprecision(6);

    double tn, zn;
    while(in >> tn >> zn){
        double dt = tn - t1;
        if(std::abs(dt) < 1e-12){ t1 = tn; z1 = zn; continue; } // skip/diagnose duplicate times
        double v = (zn - z1) / dt;
        int s = sgn(v);
        if(sign_prev != 0 && s != 0 && sign_prev != s){
            out << t1 << ' ' << z1 << '\n'; // turning point at the shared sample
        }
        if(s != 0) sign_prev = s;
        t0 = t1; z0 = z1; t1 = tn; z1 = zn;
    }
    return 0;
}

Troubleshooting notes:

  • Watch for dt == 0 (duplicate/non-monotonic timestamps) and skip or report them.
  • Flat segments (v == 0) and long plateaus need a policy: either ignore exact zeros or scan outward for the nearest nonzero signs and then mark the plateau midpoint or first zero sample as the turning point.
  • For sub-sample accuracy, fit a parabola through three points around the sign change or interpolate velocities to estimate the exact zero-crossing time and altitude.
  • For very large files use the streaming approach above (no full-file buffers).

Recommended Answers

All 4 Replies

So far this is what i have and i am stuck because i do not know how to find when the derivative goes from negative to positive or from positive to negative.

You need to store some data(altitude and time) in change.dat when velocity becomes negative. I htink this is what you are trying to do?
correct me if i am wrong.

The second thing is , in line 20 you have given derivative[lcv] = deltaT[lcv]/ deltaZ[lcv]; which contradicts the expression you posted (v= z/t)

The easiest way to see if the derivative changes sign when considering time derivatives would of course be to see if the difference between the last and 2nd to last Z changes sign.
This would mean something like if((deltaZ[lcv] - deltaZ[lcv - 1]) * (deltaZ[lcv - 1] - deltaZ[lcv - 2]) < 0). (Note that this fails when Z is identical for subsequent values of t)
You don't even need to compute the derivative for each set of values.

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

 main () {
    ifstream fin ("prog10.text");
    float x[10000], y[10000], T[10000], Z[10000], derivative[10000];
    float direction = 0;
    int nPoints;
    int lcv = 0;
    cout<< "The velocity changes direction at: " << endl;
    
    for (lcv  = 0; !fin.eof(); lcv++)
    { 
        fin >> T[lcv] >> Z[lcv];
        
        // Make sure two subsequent identical Z's don't mess everything up
        // by keeping track of the latest non-zero direction
        if (lcv > 0 && Z[lcv] != Z[lcv - 1])
        {
            // Check if the direction changes sign for this (T,Z)
            if (direction * (Z[lcv] - Z[lcv - 1]) < 0)
            {
                // Got one
            }

            direction = Z[lcv] - Z[lcv - 1];
        }
    }
}

fin >> T[lcv] >> Z[lcv];

According to the OP:

The time (t) and altitude (Z) data are space-separated in a text file (twister.dat).

Why not declare a string empty val and input it in between t[lcv] and Z[lcv] to achieve the spacing?

string spacing= " ";
//Other code
for (lcv = 0; !fin.eof(); lcv++)
{
fin >> T[lcv] >> spacing >> Z[lcv];
 //Rest of the code..

Why not declare a string empty val and input it in between t[lcv] and Z[lcv] to achieve the spacing?

The extraction operator (>>) takes input delimited on spaces. The way the OP has it is fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.