Exercise 1: Odd, Sum, Triangle, Burger
Deadline
This is an ungraded exercise. There is no deadline, but we encourage you to complete it before Thursday's lab 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
. - You are familiar with basic C syntax and arithmetic operations, and have completed at least 3 of 5 questions from Exercise 0.
Learning Outcomes
- Be comfortable writing simple C programs that involve conditional statements and logical expressions.
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
ex01-<username>
in your current working directory, whereusername
is your GitHub username. -
We will call this directory your exercise directory or assignment directory.
-
Inside that directory, you should see a bunch of files:
odd.c
,sum.c
,triangle.c
, andburger.c
are the most important files. They are the skeleton C code that you should edit to solve the exercise.inputs
andoutputs
are subdirectories that contain test inputs and test outputs.Makefile
: The configuration for the toolmake
that we use to automate the compilation and testing of the programs. You do not have to understand how to write aMakefile
for CS1010. If you are interested to learn how to write aMakefile
, talk to either Wei Tsang or Google.test.sh
: Abash
script for testing your code. You do not have to edit this file. If you are interested to learn how to writebash
script, talk to either Wei Tsang or Google.compiler_flags.txt
and.clang-tidy
are two files used to configureclang
andclang-tidy
respectively. 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
odd.c
,sum.c
,triangle.c
,burger.c
to 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 odd.c
, sum.c
, triangle.c
, and burger.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: Odd
Write a program odd
(source file odd.c
) that reads in an integer \(n\) and prints that smallest odd number that is larger than \(n\) to the standard output.
Sample run:
1 2 3 4 5 6 |
|
Note: There is a less straightforward way to solve this question without using conditional statements. See if you can solve it this way! (Credit: Hu Jialun (cohort 20/21))
Question 2: Sum
Write a program sum
(source file sum.c
) that reads in two integers, \(x\) and \(y\), ignore any negative numbers, and prints the sum of the remaining (non-negative) numbers. If both numbers are negatives, the sum is 0.
Sample run:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Question 3: Triangle
Triangle is a three-sided shape and is a very common shape everywhere. In a triangle, the sum of the length of any two sides is always longer than of the third side. The area of the triangle with sides \(a\), \(b\), \(c\), can be computed with Heron's formula:
where \(s\) is half of the perimeter:
Write a program triangle, that reads from standard input three positive integers representing the three sides of a triangle Print, to the standard output,
Possible
if it is possible to create such triangle, followed by the area of the triangle;Impossible
if it is impossible to create such a triangle.
For instance, suppose that you are given the three sides of a triangle 3 4 5
. Then you will have to print:
1 2 |
|
One more example, suppose that you are given the three sides 4 4 10
. Then you will have to print:
1 |
|
Sample run:
1 2 3 4 5 6 7 |
|
Question Credit: Albert Sutiono (cohort 20/21)
Question 4: Burger
Who doesn't love burger? With approximately 50 billion burger eaten each year in AMERICA alone, it has become one of the most iconic and popular food in the world.
We will follow the story of Andy, a CS1010 student who loves burger. Andy has a burger chain that he started since 2020. He is, however, a bit careless when ordering the stock of ingredients for each month. He does not really plan on how many burgers he is planning to sell, but instead buy the ingredients by feeling. To help him, it will be useful to know how many burgers his restaurant can make during the month. Fortunately, he notes all the ingredients that he bought e.g. how many patties, how many tomatoes, how many cheese, etc.
Let's help Andy with your skills in C programming. In this question, you are to calculate the maximum number of proper burgers that can be made in respect to each ingredients count. A proper burger needs 3 buns (more like a Big Mac), 2 patties, 1 cheese, 15 sesame seeds, and 5 pickles. The input to your program will be the number of buns, patties, cheese, sesame seeds, and pickles respectively.
For instance, suppose that the input is 20 20 20 20 20
. Then you will have to print:
1 |
|
Explanation:
Ingredients | Count | Proper Burger |
---|---|---|
Buns | 20 | 6 |
Patties | 20 | 10 |
Cheese | 20 | 20 |
Sesame Seeds | 20 | 1 |
Pickles | 20 | 4 |
Here, as the least possible number of proper burger is 1, the correct answer of the number of proper burgers (with respect to all ingredients) is 1.
One more example, suppose that the input is 15 6 3 45 13
. Then you will have to print:
1 |
|
Explanation:
Ingredients | Count | Proper Burger |
---|---|---|
Buns | 15 | 5 |
Patties | 6 | 3 |
Cheese | 3 | 3 |
Sesame Seeds | 45 | 3 |
Pickles | 13 | 2 |
Here, as the least possible number of proper burger is 2, the correct answer of the number of proper burgers (with respect to all ingredients) is 2.
Write a program burger
, that reads from standard input:
- Five positive integers, which correspond to buns, patties, cheese, sesame seeds, and pickles respectively.
Print, to the standard output, on one line, the integer that corresponds to the number of proper burgers that can be made, followed by the correct plural form word burger
(for one burger) or burgers
(otherwise)
Sample Run
1 2 3 4 5 6 |
|
Question Credit: Albert Sutiono (cohort 20/21)