You are on page 1of 6

Lab05

Please do not use array, self-defined function in this lab.

1. Homework Problem I
Write a menu-driven program that has the following options: (1~6)
In each option, please let user input data continuously and stop executing the selected
option when inputting CTRL+Z.
1. Factorial of a number
2. Prime or not
3. Odd or even
4. Coordinate
Write a program that prompts the user to input the x-y coordinate of a point in a
Cartesian plane. The program should then output a message indicating whether the
point is the origin, is located on the x/y axis, or appears in a particular quadrant.
For example:
(0, 0) is the origin.
(4, 0) is on the x-axis.
(0, -3) is on the y-axis.
(-2, 3) is in the second quadrant.
(-1, -9) is in the third quadrant.
No ternary operator in this case.
5. Arrange in descending order
Write a program that prompts the user to input three numbers. The program should
then output the numbers in descending order. (大到小). No ternary operator in this
case.
6. Exit
Once an option is selected, the appropriate action should be taken and once this action is
finished, the menu should appear again. Unless the user selects the “Exit” option, the
program should continue to work.
Please use a while loop and a switch statement to construct this menu.
Input / Output Example:
2. Homework Problem II
A. Write a program that mimics a calculator.
1. The calculator provides only 4 arithmetic operations: +, -, *, /. The program should
take as input one integer, the operator that the operation to be performed, and the
other integer.
2. It should then output the numbers, the operators, and the operation result.
3. For division, if the denominator is zero, output an error message.
4. If the input operator is not one of +, -, *, and /, please also output error message.
5. The user can continue executing the program until the user inputs ‘N’ or ‘n’ to stop
the program.
Please use the switch syntax to solve this problem.

B. The Integration
Using trapezoidal rule and rectangular rule to find the area bounded by:
From a = 5, b = 15, f(x) = 5x 3 + 2𝑥 2 + 7𝑥 + 3.
The interval [a, b] is divided into n subintervals.
Please let user input n continuously to calculate the results and stop the program when
inputting CTRL+Z. Please show the answer to the 8th decimal place.

C. Suppose you can buy a chocolate bar from the vending machine for $1 each. Inside
every chocolate bar is a coupon. You can redeem seven coupons for one chocolate bar
from the machine. You would like to know how many chocolate bars you can eat,
including those redeem via coupon, if you have n dollars.
For example, if you have 20 dollars then you can initially buy 20 chocolate bars. This
gives you 20 coupons. You can redeem 14 coupons for two additional chocolate bars.
This additional chocolate bars give you two more coupons, so you now have a total of
eight coupons. This gives you enough to redeem for one final chocolate bar. As result
you have 23 chocolate bars and two leftover coupons.
Write a program that inputs the number of dollars and outputs how many chocolate
bars you can collect after spending all your money and redeeming as many coupons as
possible. Also output the number of leftover coupons. The easiest way to solve this
problem is use a loop. Please let user input the number of dollars continuously to
calculate the results and stop the program when inputting CTRL+Z.
Input /Output Example:
3. Problem
A. Binary Palindrome
Write a program to convert binary numbers to decimal numbers and check if they are
palindromic. The program should run repeatedly until the user inputs CTRL+Z. Do not
use nested loop and no more than 2 loops in this problem. (The while loop for
continuously inputting will not be counted in the above 2-loop limitation.)

Palindromic: 回文
Ex: 12321, 123321, 8 → palindromic
1232, 10 → not palindromic

B. Write a program to approximate the value of arctan(x) using the formula:


∞ 𝑛
𝑥 𝑥 2𝑘𝑥 2
arctan(x) = +∑ ∏
1 + 𝑥2 (1 + 𝑥 2 )𝑛+1 2𝑘 + 1
𝑛=1 𝑘=1

Stop when the added/subtracted term is less than 10−12 and show the answer to the
14th decimal place. Additionally, you have to repeat your program to let user input x
and stop when user input CTRL+Z. Please use do-while or while to complete it and do
not use abs() and pow() functions in this problem.

Input /Output Example:


4. Problem
A. Write a program that reads integers and then outputs the maximum sum of
consecutive value. Assume that zero marks the end of the input. If all of the inputs are
negative, the maximum sum of consecutive values is defined to be 0. The program
should run repeatedly until the user inputs CTRL+Z.
Example:
The input is 27 6 -50 21 -3 14 16 -8 42 33 -21 9 0
The output is 115 (sum of 21, -3, 14, 16, -8, 42, 33)

B. Write a program that prompts the user to input a positive integer n and generate
corresponding diamond (1 for A, 2 for B, 3 for C, … H for 8, I for 9). You can only use at
most four loops (nested loop) to complete it. (The while loop for continuously inputting
will not be counted in the above 4-loop limitation.) Additionally, you have to repeat
your program to let user input n and stop when user input CTRL+D or CTRL+Z.

Input / Output Example:

You might also like