Skip to content

Exercise 1: Odd, Sum, Candy, 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 have completed Exercise 0.

Learning Outcomes

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

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.

Your program must find the answer within a function that takes in an integer and returns an integer called find_next_odd.

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\), ignores any negative numbers, and prints the sum of the remaining (non-negative) numbers. If both numbers are negatives, the sum is 0.

Your program must find the answer within a function named compute_sum_if_positive.

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: Candy

Beng needs to pack a bunch of candies into as few boxes as possible. Each box can hold only a certain number of candies.

Write a program candy that reads from the standard input two positive integers representing the number of candies that Beng has and the maximum number of candies that a box can hold.

Your program must contain a function called compute_num_of_boxes that computes the minimum number of boxes that Beng needs to pack all the candies into. The function should take in the number of candies and the maximum number of candies a box can hold as arguments, and return the number of boxes needed.

Your program then prints, to the standard output, the number of boxes needed.

For example, if there are 8 candies and each box can hold a maximum of 2 candies, then Beng needs 4 boxes. If each box can hold a maximum of 3 candies, then only 3 boxes are needed.

Sample run:

1
2
3
4
5
6
ooiwt@pe111:~/ex01-ooiwt$ ./candy
8 2
4
ooiwt@pe111:~/ex01-ooiwt$ ./candy
8 3
3

Question 4: 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. The program then either - prints, to the standard output, Possible if it is possible to create such a triangle, followed by the area of the triangle; or - prints 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 the program prints:

1
2
Possible
6.0000

Suppose that you are given the three sides 4 4 10. Then the program prints:

1
Impossible

Your solution must contain two functions is_possible_triangle, which returns whether the input is a possible triangle, and compute_area, which computes the area of the triangle.

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 5: Burger

Who doesn't love burgers? With approximately 50 billion burgers 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 burgers. Andy has a burger chain that he started in 2020. He is, however, a bit careless when ordering the stock of ingredients for each month. He does not plan how many burgers he will sell, but, instead, he buys the ingredients based on his gut 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 buns, how many cheese slices, 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 using the ingredients. A proper burger needs 3 buns (ala Big Mac), 2 patties, and 1 slice of cheese. The input to your program will be the number of buns, patties, and cheese slices respectively.

For instance, suppose that the input is 3 4 5. Then you will have to print:

1
1 burger

Explanation:

Ingredients Count Proper Burger
Buns 3 1
Patties 4 2
Cheese 5 5

The least possible number of proper burgers that can be made given the ingredients is 1 (we do not have enough sesame seeds to make 2 burgers!), so the correct answer is 1.

Suppose that the input is 10 4 3. Then you will have to print:

1
2 burgers

Explanation:

Ingredients Count Proper Burger
Buns 10 3
Patties 4 2
Cheese 3 3

The least possible number of proper burgers that can be made given the ingredients is 2 (we do not have enough pickles to make 3 burgers), so the correct answer is 2.

Write a program burger, that reads, from standard input, three positive integers correspond to the number of buns, patties, and cheese respectively. Your program must contain a function compute_num_of_burgers that takes in the number of each ingredient and return the number of proper burgers.

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
3 4 5
1 burger
albertsutz@pe119:~/ex01-albertsutz$ ./burger
10 4 3
2 burgers

Question Credit: Albert Sutiono (cohort 20/21)