I need help fixing my code. I don't know what else to do, I've been trying to sort all the temps array in descending order. But I don't know what I'm doing wrong. Can you help me saying what things I need to implement to my code to fix this problem? And What am I doing wrong? Thanks.

The compiler prompts me with this result of my current code: Phoenix: 107 Miami: 97 Denver: 90 San Francisco: 66 Chicago: 85 Las Vegas: 105 Los Angeles: 82 Boston: 82 New York: 85 Austin: 97

``` C
// Practice working with structs
// Practice applying sorting algorithms

#include <cs50.h>
#include <stdio.h>

#define NUM_CITIES 10

typedef struct
    string city;
    int temp;

avg_temp temps[NUM_CITIES];
// Get a copy of the original temps array
avg_temp temp_array[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;


    printf("\nAverage July Temperatures by City\n\n");

    for (int i = 0; i < NUM_CITIES; i++)
        printf("%s: %i\n", temp_array[i].city, temp_array[i].temp);

// TODO: Sort cities by temperature in descending order
void sort_cities(void)
    // Add your code here
    // Using Selection Sort:
    // Get the biggest temperature
    int biggest_temperature;
    // Get the real biggest temperature
    avg_temp real_biggest_temperature;
    // Get a current index number
    int current_index_temperature;
    int j;

    // Copying the temps array
    for (int i = 0; i < NUM_CITIES;i++)
        temp_array[i] = temps[i];
    // Iterating every each temperature
    for (int i = 0; i < NUM_CITIES; i++)
        // Find out the current index element
        int current_index = i;
        // Iterate every each temperature to figure out what is the biggest temperature
        for (j = current_index + 1; j < NUM_CITIES; j++)
            if (temp_array[current_index].temp < temp_array[j].temp)
                // Figuring out what's the biggest temperature
                biggest_temperature = temp_array[j].temp;
            // If the current biggest temperature is less than the before index and greater than everyone else set it as the real biggest temperature
            for (int l = j + 1; l < NUM_CITIES; l++)
                if (current_index == 0)
                    if (biggest_temperature > temp_array[current_index].temp && biggest_temperature > temp_array[l].temp)
                        real_biggest_temperature = temp_array[j];
                        current_index_temperature = j;
                    if (biggest_temperature < temp_array[current_index - 1].temp && biggest_temperature > temp_array[l].temp)
                        for (int f = l + 1; f < NUM_CITIES; f++)
                            if (biggest_temperature > temp_array[l].temp && biggest_temperature > temp_array[f].temp)
                                real_biggest_temperature = temp_array[j];
                                current_index_temperature = j;
        avg_temp copy_temp_array = temp_array[current_index];
        // Swap the current biggest temperature and city with the current element of the temp_array
        temp_array[current_index] = temp_array[current_index_temperature];
        // Swap the current element of the temp_array with the biggest temperature and city
        temp_array[current_index_temperature] = copy_temp_array;

1 Answer 1


that's a lot of code for a selection sort. It should be able to be sorted without need for a second array and with only two nested for loops.

The setup is pretty straightforward. I'll give a verbal description and the for loop setup rather than actual full code, so you have the opportunity to implement it. If you can't, leave a comment and I'll edit the answer.

for(int i = 0; i < NUM_CITIES - 1; i++)
    for(int j = i+1; j < NUM_CITIES; i++)
        if( temps[i].temp > temps[j].temp


THat's the for loop setup. Now, you need to add code as follows.

First, set a variable to hold the high temp for the inner loop pass, starting with whatever the temp is for temps[i].temp. Also, set a variable to hold the index of the high temp, starting with i and replacing with j as each higher temp is found. You're only looking for the high temp right now, not doing any swaps yet.

Next, inside the inner loop, add code to replace the vals in the two vars above as each higher temp is found.

Then, after the inner loop finishes, if the value in the temp index changed from i to a j value, swap the nodes.

Swapping nodes is easy:

avg_temp tmp = temps[i];
temps[i] = temps[hi_index];
temps[hi_index] = tmp;

This swaps the entire struct, you don't have to worry about swapping individual elements.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • Note: I had a critical typo in the for loop that has since been fixed.
    – Cliff B
    Mar 13, 2023 at 20:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .