Average High Temperatures

Learning Goals

  • Practice working with structs
  • Practice applying sorting 算法

Sun

背景

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 struct to another, no need to assign individual elements. The entire struct can be assigned in one statement.
    • Even though a void function cannot return any values, a return statement 可用于 to terminate the function.

演示

入门

  1. Log into cs50.dev using your GitHub account.
  2. Click inside the terminal window and execute cd.
  3. Execute wget https://cdn.cs50.net/2022/fall/实验s/3/temps.zip followed by Enter in order to 下载 a zip called temps.zip in your codespace. Take care not to overlook the space between wget and the following URL, or any other character for that matter!
  4. Now execute unzip temps.zip to create 的文件夹中名为 temps.
  5. You 不再 need the ZIP file, so you can execute rm temps.zip and 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 可选 练习题.