Prokom-Tugas 3
Prokom-Tugas 3
Assignment 3
2. A store sells carpets for IDR120.0000 per meter. If a customer buys more than 10 m of carpet, they
get a discount of 15% on every additional meter of carpet they purchase. Write a program that inputs
the carpet length that a user wishes to buy, stores the value in a double variable, and then calculates
and outputs the total cost of the carpet [20]
3. The following flowchart contains a series of questions to determine what kind of haircut to get. Write
a program that asks the questions to the user and outputs the recommended haircut. [20]
4. It is difficult to make a budget that spans several years, because prices are not stable. If your
company needs 500 pencils per year, you cannot simply use this year’s price as the cost of pencils
two years from now. Because of inflation the cost is likely to be higher than it is today. Write a
program to gauge the expected cost of an item in a specified number of years. The program asks
for the cost of the item, the number of years from now that the item will be purchased, and the rate
of inflation. The program then outputs the estimated cost of the item after the specified period. Have
the user enter the inflation rate as a percentage, such as 2.6%. Your program should then convert
the percentage to a decimal fraction, such as 0.056, and should use a loop to estimate the price
adjusted for inflation. ( Hint: Use a loop.) [20]
5. Sound travels through air as a result of collisions between the molecules in the air. The temperature
of the air affects the speed of the molecules, which in turn affects the speed of sound. The velocity
of sound in dry air can be approximated by the formula:
where Tc is the temperature of the air in degrees Celsius and the velocity is in m/s. Write a program
that allows the user to input a starting and an ending temperature. Within this temperature range,
the program should output the temperature and the corresponding velocity in 1° increments. For
example, if the user entered 0 as the start temperature and 2 as the end temperature, then the
program should output
At 0 degrees Celsius the velocity of sound is 331.3 m/s
At 1 degrees Celsius the velocity of sound is 331.9 m/s
At 2 degrees Celsius the velocity of sound is 332.5 m/s. [20]
6. Write a program that accepts a year written as a four-digit Arabic (ordinary) numeral and outputs
the year written in Roman numerals. Important Roman numerals are V for 5, X for 10, L for 50, C
for 100, D for 500, and M for 1,000. Recall that some numbers are formed by using a kind of
subtraction of one Roman “digit”; for example, IV is 4 produced as V minus I, XL is 40, CM is 900,
and so on. A few sample years: MCM is 1900, MCML is 1950, MCMLX is 1960, MCMXL is 1940,
MCMLXXXIX is 1989. Assume the year is between 1000 and 3000. Your program should include a
loop that lets the user repeat this calculation until the user says she or he is done [20]
Write a C++ program to calculate the approximate value of pi using this series. The program takes
an input n that determines the number of terms in the approximation of the value of pi and outputs
the approximation. Include a loop that allows the user to repeat this calculation for new values n
until the user says she or he wants to end the program [20]
8. Write a C++ program that solves a quadratic equation to find its roots. The roots of a quadratic
equation
ax2 + bx + c = 0 (where a is not zero)
The value of the discriminant (b 2 − 4ac) determines the nature of roots. If the value of the
discriminant is zero, then the equation has a single real root. If the value of the discriminant is
positive then the equation has two real roots. If the value of the discriminant is negative, then the
equation has two complex roots. The program takes values of a, b, and c as input and outputs the
roots. Be creative in how you output complex roots. Include a loop that allows the user to repeat this
calculation for new input values until the user says she or he wants to end the program. [20]
9. Write a program that scores a blackjack hand. In blackjack, a player receives from two to five cards.
The cards 2 through 10 are scored as 2 through 10 points each. The face cards—jack, queen, and
king—are scored as 10 points. The goal is to come as close to a score of 21 as possible without
going over 21. Hence, any score over 21 is called “busted.” The ace can count as either 1 or 11,
whichever is better for the user. For example, an ace and a 10 can be scored as either 11 or 21.
Since 21 is a better score, this hand is scored as 21. An ace and two 8s can be scored as either 17
or 27. Since 27 is a “busted” score, this hand is scored as 17. The user is asked how many cards
she or he has, and the user responds with one of the integers 2, 3, 4, or 5. The user is then asked
for the card values. Card values are 2 through 10, jack, queen, king, and ace. A good way to handle
input is to use the type char so that the card input 2, for example, is read as the character '2', rather
than as the number 2. Input the values 2 through 9 as the characters '2' through '9'. Input the values
10, jack, queen, king, and ace as the characters 't', 'j', 'q', 'k', and 'a'. (Of course, the user does not
type in the single quotes.) Be sure to allow upper- as well as lowercase letters as input. After reading
in the values, the program should convert them from character values to numeric card scores, taking
special care for aces. The output is either a number between 2 and 21 (inclusive) or the word Busted.
You are likely to have one or more long multiway branches that use a switch statement or nested
if-else statement. Your program should include a loop that lets the user repeat this calculation until
the user says she or he is done. [30]
Instruction:
1. ODD NRP: 1, 3, 5, 7, 9 and EVEN NRP: 2, 4, 6, 8, 9
2. Write a report in Bahasa or English and submit it into myITS Classroom!
3. The file to submit is only NRP.pdf
4. Please use the report template. (available on MyITSC)