Assignment #8: The C Programming Language
Fall 1996
(To be completed on Nov. 28/29)
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 words and outputs the entered words in sorted order. The sequence is terminated when the word ``end'' is input, which is not to be part of the sequence. Assume that there are at most 20 words to be entered, including ``end''. Assume that the words will be no longer than 30 characters.
For the previous problem, remove the assumption on the maximum number of words. Write a C program that can accommodate any number of words. You may still assume that the words will be no longer than 30 characters.
If you want a further challenge, remove the assumption on the maximum number of characters. Write a C program that can handle any number of words of any length.