Skip to content

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

Learning Outcomes

  • Be comfortable writing simple C programs that involve conditional statements and logical expressions.

Accepting and Retrieving Assignments

1
~cs1010/get-ex01
  • You should see a new subdirectory ex01-<username> in your current working directory, where username 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, and burger.c are the most important files. They are the skeleton C code that you should edit to solve the exercise.
    • inputs and outputs are subdirectories that contain test inputs and test outputs.
    • Makefile: The configuration for the tool make that we use to automate the compilation and testing of the programs. You do not have to understand how to write a Makefile for CS1010. If you are interested to learn how to write a Makefile, talk to either Wei Tsang or Google.
    • test.sh: A bash script for testing your code. You do not have to edit this file. If you are interested to learn how to write bash script, talk to either Wei Tsang or Google.
    • compiler_flags.txt and .clang-tidy are two files used to configure clang and clang-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
@author XXXX (Group YYYY)

and change it to something like:

1
@author King T'Challa (Group A10)

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
make

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
cat inputs/echo.3.in

To see the expected output:

1
cat outputs/echo.3.out

To see what output your program gives,

1
./echo < inputs/echo.3.in

To test the program for a particular question (say, echo), instead of all programs,

1
./test.sh echo

Submission

When you are ready, run the following command while you are in the exercise directory:

1
~cs1010/submit-ex01

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
ooiwt@pe112:~/ex01-ooiwt$ ./odd
1234
1235
ooiwt@pe112:~/ex01-ooiwt$ ./echo
-1
1

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
ooiwt@pe111:~/ex01-ooiwt$ ./sum
123456
-100
123456
ooiwt@pe111:~/ex01-ooiwt$ ./sum
-1
-2
0
ooiwt@pe111:~/ex01-ooiwt$ ./sum
1
2
3

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:

\[\sqrt{s(s-a)(s-b)(s-c)}\]

where \(s\) is half of the perimeter:

\[s = \frac{a + b + c}{2}\]

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
Possible
6.0000

One more example, suppose that you are given the three sides 4 4 10. Then you will have to print:

1
Impossible

Sample run:

1
2
3
4
5
6
7
ooiwt@pe111:~/ex01-ooiwt$ ./triangle
3 4 5
Possible
6.0000
ooiwt@pe111:~/ex01-ooiwt$ ./triangle
4 4 10
Impossible

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
1 burger

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
2 burgers

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
albertsutz@pe119:~/ex01-albertsutz$ ./burger
20 20 20 20 20
1 burger
albertsutz@pe119:~/ex01-albertsutz$ ./burger
15 6 3 45 13
2 burgers

Question Credit: Albert Sutiono (cohort 20/21)