Skip to content

Instantly share code, notes, and snippets.

# andrija-zikovic/temps.c

Created May 26, 2023 11:39
Show Gist options
• Save andrija-zikovic/e090044be719cb3784110e7d79f1f109 to your computer and use it in GitHub Desktop.
CS50/temps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 // Practice working with structs // Practice applying sorting algorithms #include #include #define NUM_CITIES 10 typedef struct { string city; int temp; } avg_temp; avg_temp temps[NUM_CITIES]; void sort_cities(void); int main(void) { temps[0].city = "Austin"; temps[0].temp = 97; temps[1].city = "Boston"; temps[1].temp = 82; temps[2].city = "Chicago"; temps[2].temp = 85; temps[3].city = "Denver"; temps[3].temp = 90; temps[4].city = "Las Vegas"; temps[4].temp = 105; temps[5].city = "Los Angeles"; temps[5].temp = 82; temps[6].city = "Miami"; temps[6].temp = 97; temps[7].city = "New York"; temps[7].temp = 85; temps[8].city = "Phoenix"; temps[8].temp = 107; temps[9].city = "San Francisco"; temps[9].temp = 66; sort_cities(); printf("\nAverage July Temperatures by City\n\n"); for (int i = 0; i < NUM_CITIES; i++) { printf("%s: %i\n", temps[i].city, temps[i].temp); } } // TODO: Sort cities by temperature in descending order void sort_cities(void) { // Add your code here int i , j , n = NUM_CITIES, swap; for ( i = 0; i < n-1; ++i) { for ( j = 0; j < n-i-1 ; j++) { if(temps[j].temp < temps[j+1].temp) { swap = temps[j].temp; temps[j].temp = temps[j+1].temp; temps[j+1].temp = swap; } } } }
to join this conversation on GitHub. Already have an account? Sign in to comment