Overview
This is a short C programming exercise that will introduce you to some core
C programming constructs: comments, variable declaration, arithmetic
expressions, loops and formatted output. Reference the introductory
slides shown in class and/or the C resources included on the course website.
Activity
Part 1
Write a C program that produces a chart of temperature conversions from
Fahrenheit to Celsius. Your program should output the Celsius conversion of Fahrenheit
temperatures in the range of 0-300 degrees F in increments of 20 degrees.
The output should look exactly like this:
Fahrenheit-Celsius
0 -17
20 -6
40 4
60 15
. .
< other values>
. .
280 137
300 148
Use the formula of C = (5/9)( F -32 ) to perform the
conversion.
- You cannot convert 5/9 into a decimal in your formula.
- You must use the fractional formula.
- You can rearrange the formula if needed
Note that all arithmetic is to be done using
integer
variables. In C, the division of two integer numbers truncates the fractional remainder.
-
Name your C program chart1.c in a directory named TempConvert
-
Create an ActivityJournal.txt. First estimate the time you will need to complete the program and complete your plan.
Then fill in all remaining sections as you work on the activity.
-
Keep track of the number of times you attempt to compile your program and
generate a compiler error of any type. Note that number in the ActivityJournal.txt
along with the actual time to complete the program.
-
On hamilton, compile your program using the GNU C Compiler (gcc) as follows:
gcc -o chart1 chart1.c
-
When you get a clean compile, the executable program will be named chart1
(that's what the -o (output) option is for). To test your program, execute the following command:
./chart1
-
The command runs your program; the
./
forces the command language interpreter (bash, or the Bourne-Again Shell)
to look in the current directory rather than the directories for standard system commands.
-
Submit your source file chart1.c and ActivityJournal.txt
in a directory named TempConvert to your Git repo.
-
Complete this Part before beginning Part 2.
Part 2
First estimate your completion time in the Activity Journal and then complete the Plan.
Copy chart1.c to a new file called chart2.c. In this new file
perfom the conversion calculation using floating point variables.
The output should look exactly like this (note the change in justification from chart1.c):
Fahrenheit-Celsius
0 -17.8
20 -6.7
40 4.4
...
Submission
Submit your source file chart2.c and updated
ActivityJournal.txt
in a directory named TempConvert to your Git repo.
Grading Criteria
To receive full credit (10 pts) for this activity you do the following:
- Submit your work in a correctly named directory. This must be one of the top level directories in your repository.
- All three files chart1.c, chart2.c, and ActivityJournal.txt must have exactly these filenames.
- The program must compile without any warnings.
- The output must match the expected output including the exact formatting.
- Good software style including consistent indentation and appropriate variable names.
- Completed Activity Journal for both parts including estimates, plans, actual times, and observations.