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
- You are able to access the CS1010 programming environment.
- You are familiar with basic UNIX CLI and using terminal-based editor
vim.
Learning Outcomes
- Be comfortable writing simple C programs that involve arithmetic operations,
longanddoubletypes, 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.
Accepting and Retrieving Assignments
- Click on this link to accept the exercise.
- Log in to one of the hosts of CS1010 programming environment (PE)
- Run the following on the command line on one of the PE hosts:
1 | |
- You should see a new subdirectory
ex00-<username>in your current working directory, whereusernameis your GitHub username. -
We will call this directory your exercise directory or assignment directory.
-
Inside that directory, you should see a bunch of files:
echo.c,divide.c,bmi.c,ones.candquadratic.care the most important files. They are the skeleton C code that you should edit to solve the exercise.inputsandoutputsare subdirectories that contain test inputs and test outputs. We use the conventionproblem-name.test-id.in for input test data, andproblem-name.test-id.out for output test data. So, you will seeecho.1.in,divide.1.out, etc. The expected output forecho.1.inis inecho.1.out. You can look at the content of these files if you wish (which UNIX command should you use to do this?). You can edit these files to change the test input and output.Makefile: The configuration for the toolmakethat we use to automate the compilation and testing of the programs. You do not have to understand how to write aMakefilefor CS1010. If you are interested to learn how to write aMakefile, talk to either Wei Tsang or Google.test.sh: Abashscript for testing your code. You do not have to edit this file nor call it directly. It is invoked bymake. If you are interested to learn how to writebashscript, talk to either Wei Tsang or Google.compiler_flags.txtand.clang-tidyare two files used to configureclangandclang-tidyrespectively. You do not need to edit this.
Identifying Yourself
In every C file that you submit to CS1010, you need to identify yourself by writing your name and tutorial group. Marks will be deducted if you fail to do so. You need to edit the line:
1 | |
and change it to something like:
1 | |
Solving The Assignments
- Edit the files
echo.c,divide.c,bmi.c,ones.candquadratic.cto solve the corresponding question as described below. - To compile and run the given tests with the sample inputs and outputs, run on the command line,
1 | |
This command will compile the C files. If there is no compilation error, it will run the test scripts. make is smart enough that if you did not change the C file, it will not recompile the files again. You can read more about how you can become a power user of make in CS1010.
This list of common clang warnings and error messages might be helpful.
Clean Compilation
This is a reminder that, for your lab assignments and practical exams, any submission that cannot compile will receive 0. Further, each compilation warning will lead to a -1 mark deduction. So, please make it a habit to ensure that you can compile cleanly, starting with this ungraded exercise.
Examining Individual Input/Output Files
If your code prints the wrong output for some of the test inputs, familiarity with UNIX CLI would be helpful. Suppose that your code for problem echo fails on test case 3. To see the input of this test case, run:
1 | |
To see the expected output:
1 | |
To see what output your program gives,
1 | |
To test the program for a particular question (say, echo), instead of all programs,
1 | |
Submission
When you are ready, run the following command while you are in the exercise directory:
1 | |
The files echo.c, bmi.c, divide.c, ones.c, and quadratic.c will be uploaded to GitHub. You can submit multiple times.
You are not allowed to interact with your CS1010 GitHub repositories using git commands or edit your files directly on GitHub's website. Doing so would interfere with the automation that we use for grading and would result in penalties (for graded assignments).
Grading
This assignment is not graded.
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 integers, \(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.
Sample run:
1 2 3 4 5 6 7 8 | |