Write a program for each of the following problems. Be sure to
save every separate program. All programs must be properly commented
and indented (see Assignment Standards on the class website).
CODE! If you are having difficulty writing programs,
consider programming more often. Working on C every day makes a big
difference, and leaving it alone, even just for a weekend, can
result in backsliding. You are learning a language and a way of thinking.
Every function gets commented with a brief description. Discuss
this with your TA to learn what is appropriate.
Name each program lab04.n.c, where n is the number in the list
below. For example, the name of the file for the first will be
lab04.1.c. Put the files in your lab04 directory.
Programs
Write a function that computes and prints the value
of a number raised to an exponent. It will take two parameters (the
double base number and the integer exponent) and use a loop to raise
the number to the appropriate power. Call the function from
main(). Test appropriately.
Put the function call from problem 1 inside a loop so that the
user can calculate as many exponentiations as they want. Design the
loop to halt when the user enters a sentinel exponent of -1.
Write a function called indent. It will be a void
function and takes a single integer parameter called
length. If the number is negative, the function prints
"error" and returns. Otherwise, it prints length spaces
(nothing else) and then returns.
Write a program that uses indent to create
the indents for a diagonal line. Your program will use a
for loop that calls indent to print the following
(based on user input):
% ./a.out
Enter height: 5
*
*
*
*
*
%
Create a file called data1.txt. Put ten assorted
hobbit heights1 (in meters) in it, separated by spaces, and save it. Now write a program
that reads the ten numbers with a loop and prints them out.
Modify 5 so that the same ten heights are read
into a type double array of ten elements. After the array is full, use a
separate loop to print the contents of the array on a single line.
Modify 6 so that after you print the
array, you call a function that takes the array and its size as a
parameter, and computes and prints the average height.
As you program, practice all of your Unix commands that have
been introduced in lab and class: ls, pwd, cd, cp, rm, mv, and mkdir
because they will be on the exams.
You should have a total of 7 programs named lab04.1.c to
lab04.7.c. Make a single script file (see lab00 for the
instructions) where you cat, compile, and run each one in its final form.
Submit all C files and your script on WebCT, and give the paper version of the
complete script file only to your TA
at the beginning of your next lab (all Friday labs) or in lecture Friday
(Wednesday labs only). Note: Cat, compile, and run each
program in order! Do not cat all programs, then compile, etc.
Footnotes:
1If you don't really know the height of a
hobbit, check the article at wikipedia.org before writing your data
file.
File translated from
TEX
by
TTH,
version 3.38. On 26 Sep 2006, 22:58.