Skip to content

Exercise 1: Freezer

This is your first programming exercise. An exercise is something that you do on your own. You can submit them but it will not be graded. Test cases are provided for the exercises so that you can test and check on your own if your code is correct. This is in contrast to an assignment, where you need to submit for grading and for credits.

Prerequisite

Learning Outcomes

  • Be comfortable writing simple C programs that involves arithmetic operations, long and double types.

One-Time Setup

Before going into their first programming exercise, you need to do a one time setup of your account on PE. You need to create a file called .gitconfig in your home directory and with the following content:

[user]
  name = Your Name
  email = Your Email
[github]  
  user = Your GitHub ID

Your email should be whatever you used to sign up Github.

For example, a sample .gitconfig looks like this:

[user]
  name = Jon Snow
  email = king@north.gov
[github]  
  user = jonsnow

After saving this file, run:

git config --get github.user

It should return your GitHub user id.

It should print your GitHub user id as set. If there is a typo, you need to edit .gitconfig again and reload it by repeating the command above.

Setup

~cs1010/get-ex01
  • You should see a new subdirectory ex01-<githubid> in your current working directory, where githubid is your GitHub ID.
  • We will call this directory your exercise directory or assignment directory.

  • Inside that directory, you should see a bunch of files:

    • freezer1.c and freezer2.c are the most important files. They are the skeleton C code that you should edit to solve the exercise.
    • inputs and outputs are sub directories that contain test inputs and test outputs. We use the convention problem-name.test-id.in for input test data, and problem-name.test-id.out for output test data. So, you will see freezer1.1.in, freezer1.1.out, etc. The expected output for freezer1.1.in is in freezer1.1.out. You can look at the content of these files if you wish (which UNIX command should you use to do this?). You can edit these files to change the test input and output.
    • Makefile: This is the configuration for the tool make that we use to automate the compilation and testing of the programs. You do not have to understand how to write a Makefile for CS1010. If you are interested to learn how to write a Makefile, talk to either Wei Tsang or Google.
    • test.sh: This is a bash script for testing your code. You do not have to edit this file nor call it directly. It is called by make. If you are interested to learn how to write bash script, talk to either Wei Tsang or Google.

Identifying Yourself

In every C file that you submit to CS1010, you need to identify yourself by writing your name and tutorial group. Marks will be deducted if you fail to do so. You need to edit the line:

@author XXXX (Group YYYY)

and change it to something like:

@author Jon Snow (Group 10)

Solving The Assignments

  • Edit the files freezer1.c and freezer2.c to solve the corresponding question as described below.
  • To compile and run the given tests with the sample inputs and outputs, run on the command line,
make

This will compile both C files and if there is no error, run the test scripts.

Submission

When you are ready, run the following command while you are in the exercise directory:

~cs1010/submit-ex01

The files freezer1.c and freezer2.c will be uploaded to GitHub. You can submit multiple times.

Grading

This assignment is not graded.


Question 1: Freezer

(a)

Write a program freezer1 (source file freezer1.c) that estimates the temperature (in degree Celsius) in a freezer given the elapsed time (in hours) since a power failure.

Assume this temperature T is given by1:

T = \frac{4t^2}{t + 2} - 20;

where t is the time (in hours) since the power failure.

Your program reads in an integer: the number of hours since the start of the power failure.

Sample run:

ooiwt@pe111:~/ex01-ooiwt$ ./freezer1
0
-20.0000
ooiwt@pe111:~/ex01-ooiwt$ ./freezer1
1
-18.6667

The text ooiwt@pe111:~/ex01-ooiwt$ is the command prompt. Yours will look different, of course. freezer1 is the executable you created. The next line, 0, is the input you provide. Press enter after the input. -20.0000 is the output printed by freezer1.

(b)

freezer1 is rather restrictive, as we can only calculate the temperature after one hour, two hours, etc. The equation given above works for fractional hours as well. Modify your program, call it freezer2 (source file freezer2.c), so that it now reads in two integers: the number of hours and the number of additional minutes since the start of the power failure. For example, to calculate the temperature after 2 hours and 45 minutes of power failure:

ooiwt@pe111:~/ex01-ooiwt$ ./freezer2
2 45
-13.6316

  1. I do not know where this formula comes from or if it is correct. Please ignore the fact that, according to this formula, 32 hours after the power is turned off, the freezer will reach the boiling point of 100 Celsius!