# Thread: How do i printf the city with the highest temperature

1. ## How do i printf the city with the highest temperature

Code:
```#include <stdio.h>

typedef struct{

char city[50];
float celcius;
float fahrenheit;
}Temperature;

int main()
{
Temperature array[5];
int i;
float average1, average2, sum;

for(i=0;i<5;i++)
{
printf("Insert a city: ");
gets(array[i].city);
fflush(stdin);
}

for(i=0;i<5;i++)
{
printf("Insert the respective temperatures in celsius degrees: ");
scanf("%f", &array[i].celcius);

array[i].fahrenheit=array[i].celcius*1.8+32;
sum+=array[i].celcius;
}

average1=sum/5;
average2=avarage1*1.8+32;

for(i=0;i<5;i++)
{
printf("%s -> %.1f celsius degrees -> %.1f fahrenheit degrees \n", array[i].city, array[i].celcius, array[i].fahrenheit);
}
printf("The average temperature is %.1f celcius degrees -> %.1f fahrenheit degrees", average1, average2);
return 0;
}```

2. Just a little code criticism:
Code:
`gets(array[i].city);`
Use fgets so you don't try to assign too many characters into the char array.
Code:
`fflush(stdin);`
fflush(stdin) is undefined behavior. fflush is designed for output or update streams, not input streams. The best replacement for it I've read is:
Code:
`while ((c = getchar()) != '\n' && c != EOF) { }`
To get the highest temp, you would just make a variable that will hold the highest temp as you cycle through the struct array and compare it to the target array member. To get the name of that city, you would cycle through again until the target value matches the highest temp in the variable. This is, of course, assuming you are not sorting, which would make both searches easier.

3. Code:
```
float highest = 0.0;
int d = 0;
highest = array[0].celcius;

for (d = 1; d < 5; d++)
if (highest < array[d].celcius)
highest = array[d].celcius;```
side bar:
Code:
```  for(i=0;i<5;i++)
{
printf("Insert a city: ");
fgets(array[i].city,50,stdin);
// fflush(stdin);
}```

4. Thanks for the correction but can you please make the cycle that gives me the name of the city, i dont know how to do it.

5. just associate the same number off the loop to the other data

6. Just like you iterate through the array and calculate the average temperature, you need to have a condition that checks for the highest temperature. They way you do this is to initially save the first temperature and then check it against each other to see if it's the highest. If it isn't, then replace it with the one that is. Repeat that until the end and you have the one you want.