Assignment #9: The C Programming Language
Fall 1997
(To be completed on Nov. 12/13)
Objective: To learn the basics of C programming
Example C programs and a makefile can be found in the directory /usr/copy/aps105/Csamples on ECF.
A good way to start with a new language is to translate from one that you know. If you have been using C all along, then treat this as pseudo-code.
The following Turing program will read a sequence of grades (integers in the range of 0-100 inclusive), terminated with a negative number. It will then print out the number of grades in each of a number of ranges, and the class average (mean grade). You should study this program so that you understand its operation, and then rewrite it in C. You should create a simple makefile and demonstrate how to use it to compile your program. Test your program thoroughly. Note that for an empty input sequence, no output should be given.
var b1,b2,b3,b4,b5,b6: int:= 0 var grade: int var gradeCount: int:= 0 % number of grades var total: int:= 0 % accumulator put "Enter grade (end with -1) " .. get grade loop % ... while grade >= 0 do ... exit when grade < 0 gradeCount:= gradeCount + 1 total:= total + grade if grade < 50 then b1:= b1 + 1 % grades below 50 elsif grade < 60 then b2:= b2 + 1 % grades 50-59 elsif grade < 70 then b3:= b3 + 1 % grades 60-69 elsif grade < 80 then b4:= b4 + 1 % grades 70-79 elsif grade < 90 then b5:= b5 + 1 % grades 80-89 else b6:= b6 + 1 % grades 90-100 end if put "Enter grade (end with -1) " .. get grade end loop if gradeCount > 0 then put b1, " is/are below 50" put b2, " is/are in range 50-59" put b3, " is/are in range 60-69" put b4, " is/are in range 70-79" put b5, " is/are in range 80-89" put b6, " is/are in range 90-100" put "The average is ", total/gradeCount end if
Write a C program that reads in a sequence of positive integers and outputs them in sorted order using the bubble sort algorithm. The sequence is terminated with the number -1, which is not to be part of the sequence. Assume that there are at most 20 numbers to be entered, not including the -1.
Change your sorting program so that it inputs and sorts words instead of numbers. The list of words should terminate with the word ``end.''
Since C does not have builtin string handling, a standard library of
string handling functions has been developed.
To access it, you will have to have #include <string.h>
at the top of your program.
You can see what these commands are by typing man string.