Skip to content

Exercise 0: Echo, Divide, Ones, BMI, Quadratic

This is your first programming exercise. An exercise is something that you do on your own. You can submit them but they will not be graded. Test cases are provided for the exercises so that you can test and check on your own if your code is correct. This is in contrast to an assignment, where you need to submit for grading and credits.

Deadline

This is an ungraded exercise. There is no deadline, but we encourage you to complete it before next Monday's lecture so that you do not fall behind.

Prerequisite

Before attempting this exercise, please make sure that you:

Learning Outcomes

  • Be comfortable writing simple C programs that involve arithmetic operations, long and double types, and standard I/O.

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.

Grading

This is an ungraded programming exercise.


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
ooiwt@pe111:~/ex00-ooiwt$ ./echo
123
123
ooiwt@pe111:~/ex00-ooiwt$ ./echo
-1
-1

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
ooiwt@pe111:~/ex00-ooiwt$ ./divide
10 2
5.0000
ooiwt@pe111:~/ex00-ooiwt$ ./divide
1 2
0.5000

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
ooiwt@pe111:~/ex00-ooiwt$ ./ones
10
0
1
ooiwt@pe111:~/ex00-ooiwt$ ./ones
95324
4
9532

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
ooiwt@pe111:~/ex00-ooiwt$ ./bmi
100.0 50
50.0000
ooiwt@pe111:~/ex00-ooiwt$ ./bmi
182.9 70.125
20.9626

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:

\[ \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} \]

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.

Sample run:

1
2
3
4
5
6
7
8
ooiwt@pe111:~/ex00-ooiwt$ ./quadratic
1.0 -2.0 1.0
1.0000
1.0000
ooiwt@pe111:~/ex00-ooiwt$ ./quadratic
2.0 -7.0 3.0
3.0000
0.5000