Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have the code snippet below:

C++
#include "TimeUtils.hpp"

FILE *output;                   // internal filename

int main(int argc, char *argv[])
{

//…………………………….
//More code
//…………………………….

time01 = time_now(); // Returns the time now
    while(1)
    {
        time02 = time_now(); // Returns the time now
        static long count  = 0;
        count  = (time02-time01)/1000; // Number of seconds since "time01"

        Rangek.runge4(count, y, dist); // Method executed in real time [Is this the way to do this?]

/*
My problem:
	I want to print to file "output" only once per second.
	How do I do that?
*/
        if((count%2) != 0)
        {
            fprintf(output, "%f\t%f\n", count, y[0]);
            continue;
        }

        cout << "Counting: " << count << " seconds\n";

        if( ((time02-time01)/1000) >= 10) break; // Stop after 10 second
    }


    fclose(output);

return 0;

}

I am supposed to see only 10 lines on my file, but I have a few hundred lines.
I can I make it print only once per second?

Thank you for your help.

Regards,

Herve
Posted

It looks like you write to the file with every iteration that (count%2) is true.

Try adding a variable called lastcount and set it equal to -1.

Then if count and lastcount are different, print to the file and make lastcount = count.

C++
if(lastcount != count)
{
    fprintf(output, "%f\t%f\n", count, y[0]);
    lastcount = count;
    continue;
}
 
Share this answer
 
Comments
Chuck O'Toole 7-Feb-12 10:47am    
This is correct, "time02 = time_now();" could return the exact same value for the time a thousand times before the clock ticks to the next second. You are not doing "timing based output". As noted, you are doing "iteration based output".
The_Real_Chubaka 7-Feb-12 10:56am    
Do you mean like this:

while(1)
{
time02 = time_now(); // Returns the time now
static long count = 0;
count = (time02-time01)/1000; // Number of seconds since “time01”
Rangek.runge4(count, y, dist); // Method executed in real time [Is this the way to do this?]

static long lastCount = -1; // 'static' so it does not get re-initialised
cout << "Counting: " << lastCount << " seconds\n";
//if((count%2) == 0)
if(lastCount == count)
{
fprintf(output, "%f\t%f\n", count, y[0]);
lastCount = count;
continue;
}

cout << "Counting: " << count << " seconds\n";

if( ((time02-time01)/1000) >= MAX) break;// Stop after 10 second

}

I have two problems:

I made "lastCount" static, but it is always getting re-initialized!!! Why is that?

The "countinue" statement will make this line "Rangek.runge4(count, y, dist);" not be executed in real-time.
Richard MacCutchan 7-Feb-12 12:29pm    
I found the following comment on your profile page: I am very proficient at C, C++, Python and MATLAB.
?
The_Real_Chubaka 7-Feb-12 12:52pm    
Yes but, the last line of code i wrote was a few years ago.
The_Real_Chubaka 7-Feb-12 10:59am    
Since, it is always getting re-initialized, it will always be equal to -1 after the first iteration.
That mean lastCount will always be != from count after the first iteration.
That is also why it is only printing the first line.
C++
Rangek.runge4(count, y, dist); // Method executed in real time [Is this the way to do this?]

What is it supposed to be doing, and what are the types and values of y and dist?
C++
if((count%2) != 0)
{
    fprintf(output, "%f\t%f\n", count, y[0]);
    continue;
}

cout << "Counting: " << count << " seconds\n";

count % 2 will yield non-zero for odd numbers so that will be after 1 second, 3 seconds etc; so every two seconds.
The fprintf() statement uses the %f format control character to print a floating point value, but the variable count is a long integer. I don't know what y[0] might be.
The continue statement goes back to the top of the loop and gets the time again. This will (probably) be fairly close to the last iteration so it will call the fprintf() statement a second time within the current second.

You need to rethink your logic and figure out exactly what you are trying to do. It would be better to save the difference between your times in a second variable and wait till that changes before doing the print; something like:
C++
timeDifference = 0;
lastDifference = 0;
while (true)
    timeDifference = time2 - time1
    if (timeDifference != lastDifference)
        lastDifference = timeDifference
        print details
    end if
    if (lastDifference > 10)
        return
end while
 
Share this answer
 
Comments
The_Real_Chubaka 7-Feb-12 12:46pm    
Point 1)

Rangek.runge4(count, y, dist); // Method executed in real time [Is this the way to do this?]

y and dist are both is of type double.

This is Runge Kutta method to solve a set of first order differential equations.
“count” is the x-value, time in this case. “y[0]” is the y-value of the first differential equation, “y[1]” the y-value of the second differential equation …

So, this method is supposed to find y-values at every count+dist point.
I set “dist” to 0.1 but, it can be changed.

The problem I have is using it in real time. I am assuming that it is finding y-values in real time because count is real-time.

Point 2)

I changed that line to:

fprintf(output, "%l\t%f\n", count, y[0]);

I modified my code so many time that I forgot to modify that line. Thank you :)

Point 3)

Thank you very much.
It works like a charm.

Last point)

I think I need to go back to my Runge Kutta code and write my logic to implement it in real time with a pen and paper.
Richard MacCutchan 8-Feb-12 4:25am    
I'm afraid I know nothing about Runge Kutta (not being a mathematician) so I cannot offer any suggestions there.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900