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

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    2

    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. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    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.
    Last edited by jack jordan; 11-17-2017 at 02:48 PM.

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    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);
        }
    Last edited by userxbw; 11-17-2017 at 03:38 PM.

  4. #4
    Registered User
    Join Date
    Nov 2017
    Posts
    2
    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. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    just associate the same number off the loop to the other data

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    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.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Name my Minecraft city
    By Babkockdood in forum General Discussions
    Replies: 7
    Last Post: 06-16-2012, 05:09 PM
  2. Name ideas of City of Heros
    By skorman00 in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 07-06-2004, 02:20 PM
  3. Distance to City
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 08-17-2003, 10:58 PM
  4. Palindrome city
    By kippwinger in forum C++ Programming
    Replies: 19
    Last Post: 07-30-2003, 11:35 AM
  5. DNA Supernanocomputers and the city problem.
    By sean in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 06-05-2002, 01:12 PM

Tags for this Thread