TDT4102, Spring 2009
Exercise 1
Deadline: 30.01.2009
The objective of this exercise:
- get familiar with the C++ environment
- learn the basics of C++ and procedural programming
- practice basic input and output
- recall basic elements of programming
General requirements:
- use Visual Studio or another IDE of your choice
- use exact name and specification when given in the exercise
Recommended reading:
- Chapter 1 & 2, Absolute C++ (Walter Savitch)
- It's Learning notes
Part 1: A simple input/output (15 pt.)
a) Write a C++ program which prompts the user for an integer which it will print back to the user.
Example output:
Write a number: 42
You wrote: 42
b) Write a program which prompts the user for two integers and prints the sum of the two integers.
Part 2: More operators (25 pt.)
a) Write a program which prompts the user for an integer which represents seconds and prints a formatted output of days, hours and minutes which the seconds make up (i.e. the input 10000 should yield the output "2 hours, 46 minutes and 40 seconds").
Hint: Using the modulo operator (%
) will ease the calculation.
b) Write a program that computes the total sales tax and tip on a restaurant bill for a patron with a $44.50 meal charge. The tax should be 6.75 percent of the meal cost, and the tip should be 15 percent of the total after adding the tax. Display the meal cost, tax amount, tip amount and total bill on the screen.
(Optional) Write program that computes the result of the equation y = x^2 + (6x-2)/3
and display the result. x should be provided by the user.
Part 3: Control Structures (20 pt.)
a) Write a program which prompts the user for an integer, and check if the integer is odd or even. The result should be printed for the user.
Example output:
Write a number: 47
The number is odd.
b) Write a program that prompts the user for two decimal numbers, and display the larger one to the user.
Part 4: Loops (40 pt.)
a) Write a program which converts NOK to Euro. The user should be able to convert any given decimal number within a reasonable range. If the user submits a negative value, he should be prompted for a new value. The output should contain only 2 decimals. One Euro is worth 9.22 NOK.
b) Write a program that uses a while structure to print the following table of values:
N 10*N 100*N 1000*N
1 10 100 1000
2 20 200 2000
3 30 300 3000
4 40 400 4000
5 50 500 5000
Hint: You can use the tab escape sequence \t in the output string to align the text.
c) Write a program which calculate each payment of a loan over 10 years, based upon the amount, interest specified by the user.
The output should be formatted properly so that it is easy to read.
To calculate the payment, you can use the following formula:
Payment = Amount/[number of payments] + (interest/100)*[remaining amount]