$29
In t ro d u c t i o n
Important: Please read Lab Guidelines before you continue.
This lab requires you to do 3 exercises on repetition control structure. To receive the attempt mark for this lab assignment, we will look at the best two programs out of three.
You may assume that the input data are according to specification, and hence there is no need to do input data validation, unless otherwise stated. The maximum number of submissions for each exercise is 15.
If you have any questions on the task statements, you may post your queries on the relevant IVLE discussion forum. However, do not post your programs (partial or complete) on the forum before the deadline!
Note that you are NOT allowed to use recursion or array for the exercises here. Using it would amount to violating the objective of this lab assignment, and you will be deemed to have failed.
1 E xerc i s e 1 : T h e 3 x+ 1 Pro b l em
1.1 Learning objectives
Using selection and repetition statement.
Writing function.
1.2 Task statement
The 3x+1 problem is also known as the Collatz problem (and a few other names), named after Lothar Collatz who first proposed it in 1937. The Collatz conjecture, still unsolved in mathematics, states the following:
Take any natural number n. If n is even, divide it by 2 to get n/2; if n is odd, triple it and add 1 to obtain 3n + 1 (see mathematical expression below). Repeat the process indefinitely. No matter what number your start with, you will always eventually reach 1.
You are not required to prove the Collatz conjecture, but to write a program collatz.c that reads in a positive integer and determines how many iterations it takes to reach 1.
For example, if the input is 3, then the answer would be 7, as 3 → 10 → 5 → 16 → 8 → 4 → 2
1.
1.3 Sample runs
Sample run using interactive input (user's input shown in blue; output shown in bold purple). Note that the first two lines (in green below) are commands issued to compile and run your program on UNIX.
Sample run #1:
gcc -Wall collatz.c -o collatz
collatz
Enter a natural number: 3 Number of iterations = 7
Sample run #2:
Enter a natural number: 1
Number of iterations = 0
1.4 Skeleton program and Test data
The skeleton program is provided here: collatz.c
Test data: Input files | Output files
1.5 Important notes
Write a function count_iterations(int n)to compute the number of iterations required for the value nto reach 1.
In writing functions, we would like you to include function prototypes before the main() function, and the function definitions after the main() function.
You are also to write the precondition in the comment above the function definition.
1.6 Estimated development time
The time here is an estimate of how much time we expect you to spend on this exercise. If you need to spend way more time than this, it is an indication that some help might be needed.
Devising and writing the algorithm (pseudo-code): 10 minutes
Translating pseudo-code into code: 5 minutes
Typing in the code: 5 minutes
Testing and debugging: 10 minutes
Total: 30 minutes
2 E xerc i s e 2 : Can d l es
2.1 Learning objectives
Using repetition statement.
Writing function.
Applying neat logic in problem solving.
2.2 Task statement
Alexandra has n candles. He burns them one at a time and carefully collects all unburnt residual wax. Out of the residual wax of exactly k (where k 1) candles, he can roll out a new candle.
Write a program candles.cto help Alexandra find out how many candles he can burn in total, given two positive integers n and k.
The output should print the total number of candles he can burn.
The diagram below illustrates the case of n = 5 and k = 3. After burning the first 3 candles, Alexandra has enough residual wax to roll out the 6th candle. After burning this new candle
with candles 4 and 5, he has enough residual wax to roll out the 7th candle. Burning the 7th candle would not result in enough residual wax to roll out anymore new candle. Therefore, in total he can burn 7 candles.
2.3 Sample runs
Sample run using interactive input (user's input shown in blue; output shown in bold purple). Note that the first two lines (in green below) are commands issued to compile and run your program on UNIX.
Sample run #1:
gcc -Wall candles.c -o candles
candles
Enter number of candles and
number of residuals to make a new candle: 5 3 Total candles burnt = 7
Sample run #2:
Enter number of candles and
number of residuals to make a new candle: 100 7
Total candles burnt = 116
2.4 Skeleton program and Test data
The skeleton program is provided here: candles.c
Test data: Input files | Output files
2.5 Important notes
Write a function count_candles()to compute the total number of candles burnt.
You need to determine the parameter(s) of the function.
You also need to write the precondition in the comment above the function definition. In writing functions, we would like you to include function prototypes before the main() function, and the function definitions after the main() function.
This is a problem-solving task where we look for neat logic in your program. Using descriptive variable names, and adding appropriate comments will help the readers (and yourself) to understand the logic better.
2.6 Estimated development time
The time here is an estimate of how much time we expect you to spend on this exercise. If you need to spend way more time than this, it is an indication that some help might be needed.
Devising and writing the algorithm (pseudo-code): 15 minutes
Checking/tracing the algorithm: 15 minutes
Translating pseudo-code into code: 5 minutes
Typing in the code: 5 minutes
Testing and debugging: 20 minutes
Total: 1 hour
3 E xerc i s e 3 : F o rt u n e Co o ki es
3.1 Learning objectives
Using nested loops. (See 3.5 Important notes below.)
Writing functions.
3.2 Task statement
Write a program cookies.cto read in a positive integer and add up its digits repeatedly until the sum is a single digit. For example, if the integer is 12345, then adding its digits (1 + 2 + 3 + 4 + 5) yields 15, and adding its digits again (1 + 5) yields 6. Hence the answer is 6.
Using this single digit result, print out the corresponding Fortune Cookie message according to the table below:
Digit
Fortune Cookie message
1
You will have a fine capacity for the enjoyment of life.
2 Now is the time to try something new.
3 Don't let doubt and suspicion bar your progress.
4 Your principles mean more to you than any money or success.
5 Accept the next proposition you hear.
6 A handful of patience is worth more than a bushel of brains.
7 You have an active mind and a keen imagination.
8 You are talented in many ways.
9 Treat everyone as a friend.
3.3 Sample runs
Sample run using interactive input (user's input shown in blue; output shown in bold purple). Note that the first two lines (in green below) are commands issued to compile and run your program on UNIX.
Sample run #1:
gcc -Wall cookies.c -o cookies
cookies
Enter a positive integer: 12345
A handful of patience is worth more than a bushel of brains.
Sample run #2:
Enter a positive integer: 67890
Don't let doubt and suspicion bar your progress.
Sample run #3:
Enter a positive integer: 11111111
You are talented in many ways.
3.4 Skeleton program and Test data
The skeleton program is provided here: cookies.c
Test data: Input files | Output files
3.5 Important notes
Write a function sum_digits(int num)to repeatedly sum the digits in num until it gets to a single digit answer, and return this answer.
Write a function print_cookie(int id)to print the corresponding cookie message depending on the value of id.
You also need to write the precondition in the comment above the function definition. Although the learning objectives indicate "using nested loops", this problem can be done with a single loop. See if you are able to come out with an algorithm using a single loop. It is okay if you cannot; a doubly nested loop is just as acceptable for this exercise. You may discuss this at your discussion session.
As the objective of this lab assignment is on using repetition statement, please use a loop for this exercise, even if there is a way to solve it without using loop.
In writing functions, we would like you to include function prototypes before the main() function, and the function definitions after the main() function.
3.6 Estimated development time
The time here is an estimate of how much time we expect you to spend on this exercise. If you need to spend way more time than this, it is an indication that some help might be needed.
Devising and writing the algorithm (pseudo-code): 20 minutes
Translating pseudo-code into code: 10 minutes
Typing in the code: 10 minutes
Testing and debugging: 30 minutes
Total: 1 hour 10 minutes
4 D ead l i n e
The deadline for submitting all programs is 14 September 2013, Saturday, 9am. Late submission will NOT be accepted.
0
Introduction
1
Exercise 1: The 3x+1 Problem
1.1
Learning objectives
1.2
Task statement
1.3
Sample runs
1.4
Skeleton program and Test data
1.5
Important notes
1.6
Estimated development time
2
Exercise 2: Candles
2.1
Learning objectives
2.2
Task statement
2.3
Sample runs
2.4
Skeleton program and Test data
2.5
Important notes
2.6
Estimated development time
3
Exercise 3: Fortune Cookies
3.1
Learning objectives
3.2
Task statement
3.3
Sample runs
3.4
Skeleton program and Test data
3.5
Important notes
3.6
Estimated development time
4
Deadline
Aaron Tan
Saturday, August 24, 2013 10:18:10 PM SGT