Exercise 0: I/O, Types, Arithmetic, Functions
Prerequisite
Before attempting this exercise, please make sure that you:
- can access the CS1010 programming environment.
- are familiar with basic UNIX CLI and using the terminal-based editor
vim
. - have set up your
vim
. - have read the guide and instructions for programming exercises.
Learning Outcomes
Be comfortable writing simple C programs that involve I/O, long
and double
types, arithmetic operations, and function decomposition/reuse.
One-Time Setup
Before going into your first programming exercise, you need to do a one-time setup of your account on PE. Follow the instructions here.
Concepts and Difficulty
Question | I/O | Types | Arithmetic | Function | Difficulty | |
---|---|---|---|---|---|---|
1 | Echo | |||||
2 | Divide | |||||
3 | Ones | |||||
4 | BMI | |||||
5 | Quadratic | |||||
6 | Cuboid |
Question 1: Echo
Write a program echo
(source file echo.c
) that reads in an integer and prints that integer to the standard output.
Sample run:
1 2 3 4 5 6 |
|
The text ooiwt@pe111:~/ex00-ooiwt$
is the command prompt. Yours will look different, of course. echo
is the executable you created. The next line, 123
, is the input you provide. Press enter after the input. 123
is the output printed by echo
.
Question 2: Divide
Write a program divide
(source file divide.c
) that reads in two integers, \(x\) and \(y\), and print the value of \(x\) divided by \(y\). You can assume that \(y\) is never 0.
Sample run:
1 2 3 4 5 6 |
|
Question Credit: Hu Jialun (cohort 20/21)
Question 3: Ones
Write a program ones
(source file ones.c
) that reads in a positive integer \(n\) that is at least 10. Print two lines to the standard output: the first line contains the last digit of \(n\). The next line contains all the other digits of \(n\) excluding the last one.
Sample run:
1 2 3 4 5 6 7 8 |
|
Question 4: BMI
Your body mass index (BMI) can be calculated by your body mass divided by the square of the body height. BMI is expressed in units of \(kg/m^2\).
Write a program bmi
(source file bmi.c
) that reads in two real numbers \(h\) and \(w\) from the standard input. \(h\) is the height of a person in centimeters (cm), and weight is the weight of the person in kilograms (kg).
Your program must include a function called compute_bmi
that takes in two parameters, the weight in kilograms (kg), and height in meters (m), and return the corresponding BMI of the given weight and height.
Question Credit: Hu Jialun (cohort 20/21)
Sample run:
1 2 3 4 5 6 |
|
Question 5: Quadratic
Write a program quadratic
(source file quadratic.c
) that reads in three floating point numbers, \(a\), \(b\), and \(c\), that represent the quadratic equation \(ax^2 + bx + c = 0\), and prints its two roots. Assume that \(a \not = 0\) and \(b^2 > 4ac\). Recall that the roots are:
Print the root \(\frac{-b + \sqrt{b^2 - 4ac}}{2a}\) first on one line, followed by the other root \(\frac{-b - \sqrt{b^2 - 4ac}}{2a}\) on the next line.
Try and decompose the computation of the roots into smaller functions for reusability.
Sample run:
1 2 3 4 5 6 7 8 |
|
Question 6: Cuboid
A cuboid is a 3D shape with six rectangular faces. We can specify such a cuboid with three parameters, the width \(w\), the length \(l\), and the height \(h\).
Our task is to calculate (i) the surface area of a cuboid, and (ii) the length of the diagonal of the cuboid (the red dash line in the figure above).
Complete the task by decomposing the calculations into two functions. To complete this task, in cuboid.c
,
- Write a function
hypotenuse_of
to compute the hypotenuse of a right-angled triangle, given the length of the legs (the sides adjacent to the right angle). - Write a function
area_of_rectangle
that computes the area of a rectangle given its width and length.
Finally, complete the program cuboid.c
so that it reads, from the standard input, three positive integers representing the width, length, and height of the cuboid (in that order) and prints, to the standard output, its surface area, followed by the length of its diagonal.
Solve this question by using composing and reusing the two functions above. You may add additional functions if needed.
Pay attention to the types (long
or double
) used in the inputs, calculations, and outputs. Keep your variable as integer type (long
) as long as possible.
Sample Run
1 2 3 4 5 6 7 8 |
|