Average High Temperatures
- Practice working with
- Practice applying sorting algorithms
We seem to be breaking records every year for the hottest weather ever recorded. Climate scientists keep track of what are called “new normals” over multiple years so that we can better predict and prepare for conditions in the near future. The official normals are calculated for a uniform 30 year period, and consist of annual/seasonal, monthly, daily, and hourly averages and statistics of temperature, precipitation, and other climatological variables from almost 15,000 U.S. weather stations.
July is the hottest month of the year for most large US cities. Daytime temperatures above 80 degrees Fahrenheit regularly occur nearly everywhere. The exceptions are some cities along the Pacific coast.
In this problem, you will sort the average high temperature values for 10 cities, in decending order.
- When copying one
structto another, no need to assign individual elements. The entire
structcan be assigned in one statement.
- Even though a
voidfunction cannot return any values, a
returnstatement can be used to terminate the function.
- When copying one
- Log into code.cs50.io using your GitHub account.
- Click inside the terminal window and execute
wget https://cdn.cs50.net/2022/fall/labs/3/temps.zipfollowed by Enter in order to download a zip called
temps.zipin your codespace. Take care not to overlook the space between
wgetand the following URL, or any other character for that matter!
- Now execute
unzip temps.zipto create a folder called
- You no longer need the ZIP file, so you can execute
rm temps.zipand respond with “y” followed by Enter at the prompt.
- Finally, right-click or control-click on the
tempsfolder and click “Open in CS50 Lab”. You should see the specification for this problem on the left-hand side and its distribution code on the right-hand side.
main function initializes the
temps array, calls the
sort_cities function and prints out the array in sorted order. You will use an O(n2) sorting algorithm of your choice (possibly bubble sort, selection sort, or insertion sort) to sort the array by temperature, in descending order.
- Which of the sorting algorithms did you choose and why?
How to Test Your Code
Your program should behave per the examples below.
temps/ $ ./temps Average July Temperatures by City Phoenix: 107 Las Vegas: 105 Austin: 97 Miami: 97 Denver: 90 Chicago: 85 New York: 85 Boston: 82 Los Angeles: 82 San Francisco: 66 temps/ $
check50 for this one!
To evaluate that the style of your code, type in the following at the
How to Submit
No need to submit! This is an optional practice problem.