0
void sort_cities(void)
{
    // Add your code here
    for (int i = 0; i <= NUM_CITIES - 1; i++)
    {
        if (temps[i].temp < temps[NUM_CITIES - 1].temp)
        {
            int temp = temps[i].temp;
            temps[i].temp = temps[NUM_CITIES - 1].temp;
            temps[NUM_CITIES - 1].temp = temp;
        }
    }
}

This is my solution but I don't know what's wrong about it. When the temperature is sorted but the name of city is not come with the temperature.

1 Answer 1

0

the code is swapping only the temp elements of temps[] not the city elements. Structs can be handled as a whole. try declaring a holding var as the same type struct as temps and...

hold = temps[a]; ... you can figure out the rest. ;-)

Programming tip: You're using var names that are way too similar with temp and temps. In fact, you're using temp in two different contexts here. Imagine if you had to come back in a year and decipher what was happening? Using dissimilar terms for var names is the best practice to avoid confusion.

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

You must log in to answer this question.

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