Recommended Reading: Chapters 1, 2, 3.7, 4.1-4.4,4.6 [IACU]
In the first lecture I gave you an example of a C program that computes
and prints the square of an integer.
It had a function "square" that returns an integer and a main function
that called this function.
a) Type this program and save it under the filename "square1.c". Add the necessary "#include" statements. Make sure to include some comments. Compile it and test it (run it) on different inputs.
b) Change your program ("square2.c") so it reads the value of the integer (y) from the user using the scanf function.
c) Rewrite the program ("square3.c") in the following way: Use the function square that takes an integer and returns its square. Use this function from within main() to produce a table that lists all the squares of the integers from a user specified upper limit down to 1. Your program output should look roughly like this:
% square
Please enter upper limit: 5
square(5) = 25
square(4) = 16
square(3) = 9
square(2) = 4
square(1) = 1
Write a program avg.c that computes the average of three integer
numbers provided by the user. When you run your program to test it,
the example run should look like this:
% a.out
Enter number 1: 1
Enter number 2: 2
Enter number 3: 5
Average of the numbers is 2.666667
Hint: See Section 2.10.1 in [IACU] and remember that the average should
not be computed using integer arithmetic
Write a program that does a "Metric to Customary" length
conversion for you. The program should take as input a certain number of centimeters
(as float) and produces the correct output in the units feet and inches (as float).
5 cm are 1.9685 inches
10 cm are 3.93701 inches
15 cm are 5.90551 inches
20 cm are 7.87402 inches
25 cm are 9.84252 inches
30 cm are 11.811 inches
35 cm are 1 foot and 1.77953 inches
40 cm are 1 foot and 3.74803 inches
45 cm are 1 foot and 5.71654 inches
50 cm are 1 foot and 7.68504 inches
55 cm are 1 foot and 9.65354 inches
60 cm are 1 foot and 11.622 inches
65 cm are 2 feet and 1.59055 inches
Hint 1: The conversion formula from centimeters to inches is:
inch = cm / 2.54
Hint 2: When declaring the variables use float instead of int whenever you think that floating point numbers are involved.
Hint 3: To print a floating point number use %g in the printf() statement (instead of which is used with integers). So, for example
printf("The value of pi is approx. %g\n", 3.1415);
will print the value of pi. You may also use %f instead of %g, but %g gives a marginally
nicer default formatting. We will discuss formatting issues at a later time.
Which of the following statements prints out the values 1 to 10 on seperate lines?
Rewrite the for loop as a while loop, so that it will generate the same output.
1. for (int count = 1; count <= 10; count= count + 1) printf("%d\n",count); 2. for (int count = 1; count < 10; count++) printf("%d\n",count); 3. for (int count = 1; count <= 9; count = count + 1) printf("%d ",count); 4. for (int count = 1; count <> 10; count++) printf("%d\n",count);