Average High Temperatures
Learning Goals
- Practice working with
structs - Practice applying sorting 算法

背景
We seem to be breaking records every year 的 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.
七月 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 排序 the average high temperature values for 10 cities, in decending order.
- 提示
- When copying one
structto another, no need to assign individual elements. The entirestructcan be assigned in one statement. - Even though a
voidfunction cannot return any values, areturnstatement 可用于 to terminate the function.
- When copying one
演示
入门
- Log into cs50.dev using your GitHub account.
- Click inside the terminal window and execute
cd. - Execute
wget https://cdn.cs50.net/2022/fall/实验s/3/temps.zipfollowed by Enter in order to 下载 a zip calledtemps.zipin your codespace. Take care not to overlook the space betweenwgetand the following URL, or any other character for that matter! - Now execute
unzip temps.zipto create 的文件夹中名为temps. - You 不再 need the ZIP file, so you can execute
rm temps.zipand respond with “y” followed by Enter at the prompt.
实现细节
The 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 冒泡排序, 选择排序, or insertion 排序) to 排序 the array by temperature, in descending order.
Thought 问题
- Which of the sorting 算法 did you choose and why?
如何测试 Your Code
你的程序应该 behave per the例子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/ $
No check50 for this one!
To evaluate that the style of your code, type in the following at the $ prompt.
style50 temps.c
如何提交
No need to 提交! This is an 可选 练习题.