ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm having a bit of trouble figuring out if consecutive values in an array are the same; I don't know how to approach the problem.
Basically, I am provided with a file "temp.dat" with 30 high temperatures (integers) in it. The program is supposed to read them in and compute/print the average. Then it is supposed to print the temperature of each day and, in addition, display a + by each day that is over the average, but only if it is above the average high for three or more consecutive days. This is the part I am stuck on. I'd appreciate any tips that would point me in the right direction
Full disclosure: This is a school project. Code:
Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 31
int main() {
FILE *data;
int counter, days, i, month_l, sum_t, temperature;
float average_t = 0;
int array_t[SIZE];
char array_c[SIZE];
i = counter = days = sum_t = 0;
printHeading();
printf("Enter the number of days data was collected for:\t");
scanf("%d",&month_l);
if ((data = fopen("temp.dat","r")) == 0){
printf("No such file found. Please place temp.dat in the program's directory.\n");
exit(1);
}
while(fscanf(data, "%d\n", &temperature) != EOF){
array_t[i] = temperature;
sum_t += temperature;
i++;
}
average_t = sum_t / month_l;
printf("The average temperature is %.2f\n", average_t);
for(i = 0; i < month_l; i++){
if(array_t[i] > average_t)
array_c[i] = '+';
else
array_c[i] = ' ';
}
for(i = 2; i < month_l; i++){
if((array_c[i-2] == '+') && (array[i-1] == '+') && (array[i] == '+')){
//Not really sure what to do here to preserve values for >3 day consecutive above average high temperatures.
}
}
for(i = 0; i < month_l; i++){
printf("%d\t%d %c\n", ++days, array_t[i], array_c[i]);
}
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.