简体   繁体   中英

How can i add to this code to calculate the average temperature and the maximum temperature?

The program should compute and display the maximum temperature and average temperature recorded using the inputed temperatures taken 5 times repeatidly for 5 days. Help me please

    ` #include <stdio.h>
int main(){
const int DAYS=5;
const int TIMES=5;
int temperature[DAYS][TIMES], i, j;
for(i=0; i<=DAYS; i++)
{for(j=0; j<=TIMES; j++)
{printf("Please enter temperature [%d] for Day[%d]: ", i+1, j+1);
scanf("%d", &temperature[i][j] );}
printf("\n");}
for(i=0; i<DAYS; i++)
{for(j=0; j<TIMES; j++)
{printf("[%d], Day[%d]=%d\n", i+1, j+1, temperature[i][j]);}
printf("\n");} `

First of all: please format your code that it is easier to read. Secondly you should try to solve the problem by yourself and just post the question if you're stuck on a particular aspect.

But nevertheless here is an idea for you, how to go about this problem: To determine the maximum temperature, just track the highest entered value and compare every new value to the previous maximum and set the new maximum, if the entered value is higher. For the average temperature just add all temperatures up and divide them by DAYS * TIMES

You can do both things in the loop you already have (besides the division at the end obviously).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM