Assignment #8: The C Programming Language
Winter 1997
(To be completed on Apr 7/Apr 8)
(This assignment is now ready)
Objective: To learn the basics of C programming by translating Turing programs to C.
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. Compile and 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
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
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.
Change your sorting program so that it inputs and sorts words instead of numbers. The list of words should terminate with the word ``end.''