Reputation: 713
I got a data file which consists of a single column with the header name with temperature and the following rows are just a series of recorded temperature. I can read it successfully(perhaps) into the C program using the following command:
#include <stdio.h>
#include <cstdlib>
int main()
{
FILE *fpt; /*define a pointer to predefined structure type FILE*/
fpt = fopen("temperature.dat","r");
char temp[10];
float t[7];
int i;
fscanf(fpt, "%s",temp);
printf("%s",temp);
for(i=0;i<7;++i)
{
fscanf(fpt, "%f",&t[i]);
printf("%.2f",t[i]);
}
printf("%f",t[3]); /*just testing whether the program is reading correctly*/
fclose(fpt);
system("pause");
}
But the problem is how could I detect when there is a series of temperature, for instance 6 temperature values are increasing continuously. I need something like IF total of 6 values of temperature is increased continuously, then it will generate some error message using printf
function. Assume that the total input number of data is not fixed, how could I program it.
Upvotes: 0
Views: 459
Reputation: 1560
You will need some kind of counter to see how many times you have seen incrementing temperatures. Also, read the file in a while loop:
#include <stdio.h>
int main()
{
FILE *fpt; /*define a pointer to predefined structure type FILE*/
fpt = fopen("temperature.dat","r");
char temp[10];
int count = 0;
int i;
float prev_temp = -999.00;
float current_temp;
int threshold = 6;
fscanf(fpt, "%s",temp); // header?
printf("Header: %s\n",temp);
while(!feof(fpt)) {
fscanf(fpt, "%f", ¤t_temp);
if (current_temp > prev_temp) count++;
else count = 0;
prev_temp = current_temp;
if (count > threshold) printf("Saw %d consecutive increases\n", count);
}
fclose(fpt);
}
Upvotes: 0
Reputation: 2721
Add a variable (say, inctemp
) to count seeing increases in a row, and increment it in your loop if there is an increase. Reset it to 0 if there is not an increase. At the end of your loop, you know how many in a row there were (at least at the end of the data set)
Modified for arbitrary number of reads
int inctemp = 0;
float curtemp, prevtemp;
...
if ( fscanf(fpt, "%f",&prevtemp) == 1)
printf("%.2f",prevtemp);
while( fscanf(fpt, "%f",&curtemp) == 1)
{
printf("%.2f",curtemp);
if( curtemp > prevtemp ) {
inctemp++;
}
else {
inctemp = 0;
}
if( inctemp == 6 ) {
printf("Six increases in a row!\n");
}
prevtemp = curtemp;
}
}
Upvotes: 0
Reputation: 15501
To detect whether a file of floats has at least 6 increasing values in a row, you could do something like this:
#include <stdio.h>
#define IN_A_ROW 6
int main() {
FILE *f = fopen("temps.txt", "r");
float x, last_x;
int inc = 0;
fscanf(f, "%f", &last_x);
while (fscanf(f, "%f", &x) == 1) {
if (x > last_x) { // or maybe >=
if (++inc >= IN_A_ROW) {
printf("Found %d increases in a row\n", IN_A_ROW);
return -1;
}
}else
inc = 0;
last_x = x;
}
fclose(f);
return 0;
}
Upvotes: 1
Reputation: 475
Finding a delta between the temperatures will help you.
#include <stdio.h>
#include <cstdlib>
int main()
{
FILE *fpt; /*define a pointer to predefined structure type FILE*/
fpt = fopen("temperature.dat","r");
char temp[10];
float t[7];
int i, loweringdelta;
fscanf(fpt, "%s",temp);
printf("%s",temp);
loweringdelta = 1;
for (i=0; i<7; ++i)
{
fscanf(fpt, "%f", &t[i]);
printf("%.2f", t[i]);
if (i > 0 && (t[i]-t[i-1]<= 0))
{
loweringdelta = t[i]-t[i-1];
}
}
if (loweringdelta > 0)
{
// Your error message here
}
printf("%f", t[3]); /*just testing whether the program is reading correctly*/
fclose(fpt);
system("pause");
}
Upvotes: 0
Reputation: 5383
There is no need to use an extra loop. You can just do
totalInc = 0;
for(i=0;i<7;++i) {
fscanf(fpt, "%f",&t[i]);
printf("%.2f",t[i]);
if (i > 0) {
if (t[i] > t[i-1]) totalInc += 1;
else totalInc -= 1;
}
}
The totalInc
will tell you the number of times the current value is greater than the previous value. For your case, you can then just check for totalInc == 6
but really, you can just check for any number of increments. A positive number will indicate a general incremental trend, while a negative number will indicate a general decreasing trend.
Upvotes: 1