Sunteți pe pagina 1din 10

University Of Central Punjab

Introduction to Computing - Course Code: CSCS1514

ASSIGNMENT#4
Announcement Date: 23rd December, 2013 Submission Date: 28th December, 2013

Submission Guidelines: Hard copy is to be submitted. Do not bind your assignment, only staple them. Test will be conducted in class from the assignment questions for evaluation. In case of plagiarism, both the students will get ZERO. Late submissions will NOT be accepted in ANY case. Attempt the following questions using switch statement: Q1. Write a C++ program that calculates and displays water bill. The water rates vary depending on the type of usage. A code of H means home use, A code of C means commercial use, A code of I means industrial use. Any other code value should be treated as an INVALID INPUT. Water rates are computed as follows: Code H: First 1 hundred gallons for $5.00 and $0.005 for each additional gallon used Code C: First 4 hundred gallons for$1000.00 and $0.025 for each additional gallon used Code I: First 4 hundred gallons for $1500 and $0.125 for each additional gallon used Your program should prompt the user to enter: Account number in integer Usage code in character Gallons of water consumed in real number The output from your program should include the: Account number Message indicating the type of usage Amount of money due from the user (e.g. Rs. 340.56) Display the amount to two decimal places. [Hint: Read the handout attached] Q2. A bank in your town updates its customers accounts at the end of each month. The bank offers two types of accounts: savings and checking. Every customer must maintain a minimum balance. If a customers balance falls below the minimum balance, there is a service charge of: $10.00 for savings accounts $25.00 for checking accounts

~1~

If the balance at the end of the month is at least the minimum balance, the account receives interest as follows: a. Savings accounts receive 4% interest. b. Checking accounts with balances of up to $5,000 more than the minimum balance, receive 3% interest; otherwise, the interest is 5%. Write a program that reads a customers account number (int type), account type (char; s for savings, c for checking), minimum balance (double type) that the account should maintain, and current balance (double type). The program should then output the account number, account type, current balance, and an appropriate message. Test your program by running it five times, using the following data: 46728 S 1000 2700 87324 C 1500 7689 79873 S 1000 800 89832 C 2000 3000 98322 C 1000 750

Q3. Write a program that calculates and prints the bill for a cellular telephone company. The company offers two types of service: regular and premium. Its rates vary, depending on the type of service. The rates are computed as follows: Regular service: $10.00 plus first 50 minutes are free. Charges for over 50 minutes are $0.20 per minute. Premium service: $25.00 plus: a. For calls made from 6:00 a.m. to 6:00 p.m., the first 75 minutes are free; charges for more than 75 minutes are $0.10 per minute. b. For calls made from 6:00 p.m. to 6:00 a.m., the first 100 minutes are free; charges for more than 100 minutes are $0.05 per minute. Your program should prompt the user to enter an account number, a service code (type char), and the number of minutes the service was used. A service code of r or R means regular service; a service code of p or P means premium service. Treat any other character as an error. Your program should output the account number, type of service, number of minutes the telephone service was used, and the amount due from the user. For the premium service, the customer may be using the service during the day and the night. Therefore, to calculate the bill, you must ask the user to input the number of minutes the service was used during the day and the number of minutes the service was used during the night. Q4. Write and run a program that plays the game of Rock, paper, scissors. In this game, two players simultaneously say (or display a hand symbol representing) either rock, paper, or scissors. The winner is the one whose choice dominates the other. The rules are: paper dominates (wraps) rock rock dominates (breaks) scissors scissors dominate (cut) paper Q5. Write and run a program that simulates a calculator. Your calculator should perform the following the functions: Addition Subtraction

~2~

Division result should be in two decimal places Modulus Percentage result should be in two decimal places Average result should be in two decimal places Factorial Sin result should be in two decimal places Cos result should be in two decimal places Tan result should be in two decimal places Sqrt square root Exp Power Log- logarithm result should be in two decimal places

Attempt the following question with reference to the handout attached:

Q6. Write a program that: a. Displays the movie name Journey to Mars b. Display price of one ticket that is $ 39.50 c. Asks the user to enter number of tickets to be bought d. Calculate and display total price e. Asks the user to enter amount paid f. Calculate and display balance Use the format given below to display the output. Refer to the handout given to attempt this question.

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* TICKET BILL -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* Movie Name: ....................... Journey to Mars Price per ticket:............. $ 39.50 Tickets to be bought: ............6 Total Price: ..$ 237.00 Amount Paid:.................... $ 300.00 Balance: $ 63.00
Attempt the following questions using loops:

Q7. Write a program to fill the screen with a smiling face. The smiling face has an ASCII value 1. Q8. Write a program to fill the screen with diamond and heart alternatively. The ASCII value for heart is 3 and that of diamond is 4. [Hint for Q7, Q8: assign a character with ASCII integer value] Q9. Write a program that calculates and displays the amount of money available in a bank account that initially has $1000 deposited in it and earns 8% interest a year. Your program should display the amount available at the end of each year for a period of ten years. The money

~3~

available at the end of each year equals the amount of money in the account at the start of the next year plus 0.08 times the amount available at the start of the year.

Assignment Guidelines:

Comment your code Use sensible variable names

~4~

Some new concepts to explore on your own:

1.

setprecision Manipulator

Setprecision is used to control the output of floating-point numbers. Usually, the default output of floatingpoint numbers is scientific notation. Some integrated development environments (IDEs) might use a maximum of six decimal places for the default output of floating-point numbers. To print floating-point output to two decimal places, we use the setprecision manipulator to set the precision to 2. The general syntax of the setprecision manipulator is:
setprecision(n)

where n is the number of decimal places required. We use the setprecision manipulator with cout and the insertion operator. For example, the statement:
cout << setprecision(2);

formats the output of decimal numbers to two decimal places until a similar statement changes the precision. To use the manipulator setprecision, the program must include the header file iomanip.h. Thus, the following include statement is required:
#include <iomanip.h>

Example: Run the following program on your system to better understand:


#include <iostream.h> #include <iomanip.h> int main() { float hours = 25.1423; float tolerance = 0.01000; cout<<"\nDefault display: \n"; cout<<"hours = " << hours << endl; cout<<"tolerance = " << tolerance << endl; cout << setprecision(2); cout<<"\nSet precision 2: \n"; cout<<"hours = " << hours << endl; cout<<"tolerance = " << tolerance << endl; cout.setf(ios::showpoint); cout<<"\nDifferent decimal places: \n"; cout<<"hours = "<<setprecision(6)<<hours<< endl; cout<<"tolerance = " <<setprecision(6)<< tolerance <<endl; return 0; }

2.

Fixed and Scientific display of floating values:

We can further control the output of floating-point numbers as following: a. To output floating-point numbers in a fixed decimal format, we use the manipulator

~5~

fixed. The following statement sets the output of floating-point numbers in a fixed decimal format:
cout.setf(ios::fixed);

b. To output floating-point numbers in a scientific format, we use the manipulator scientific. The following statement sets the output of floating-point numbers in a scientific format:
cout.setf(ios::scientific);

c. Without setting the scientific or fixed manipulators, the trailing zeros are not shown. After setting the manipulators, the values are printed to six decimal places. d. To unset a manipulator, we use:
cout.unsetf(ios::scientific); cout.unsetf(ios::fixed);

Example: For the following variables hours = 35.45; rate = 15.00; tolerance = 0.01000; Values displayed in scientific and fixed form are as given below:

Run the following program on your system to better understand:


#include <iostream.h> #include <iomanip.h> int main() { float hours = 35.45; float tolerance = 0.01000; cout<<"\nDefault display: \n"; cout<<"hours = " << hours <<endl; cout<<"tolerance = " << tolerance <<endl; cout<<"\nScientific display: \n"; cout.setf(ios::scientific); cout<<"hours = " << hours <<endl; //Scientific notation

~6~

cout<<"tolerance = " << tolerance <<endl; cout.unsetf(ios::scientific); //Unset scientific notation

cout<<"\nFixed display: \n"; cout.setf(ios::fixed); //setting fixed notation cout<<"hours = " << hours <<endl; cout<<"tolerance = " << tolerance <<endl; return 0; }

3. setw Manipulator
The manipulator setw is used to output the value in a specific number of columns on the screen.

The expression setw(n) outputs the value in nth column on the screen. The output is right justified.

If we specify the number of columns to be 8, for example, and the output requires only four columns, the first four columns are left blank. Furthermore, if the number of columns specified is less than the number of columns required by the output, the output automatically expands to the required number of columns; the output is not truncated. For example, if x is an int variable, the following statement outputs the value of x in five columns on the standard output device:
cout << setw(5) << x << endl;

To use the manipulator setw, the program must include the header file iomanip.h. Thus, the following include statement is required:
#include <iomanip.h>

Unlike setprecision, which controls the output of all floating-point numbers until it is reset, setw controls the output of only the next expression. Example: Run the following program on your system to better understand:
#include <iostream.h> #include <iomanip.h> int main() { float hours = 35; float tolerance = 0.01000; cout<<"\nDefault display: \n"; cout<<"hours = "<<hours<<setw(24)<<"tolerance = " << tolerance <<endl; cout<<"\nScientific display: \n"; cout.setf(ios::scientific); //Scientific notation cout<<"hours = "<<hours<<setw(20)<<"tolerance = " << tolerance <<endl; cout.unsetf(ios::scientific); cout<<"\nFixed display: \n"; cout.setf(ios::fixed); //Unset scientific notation //setting fixed notation

~7~

cout<<"hours = "<<hours<<setw(24)<<"tolerance = " << tolerance <<endl; return 0; }

4.

setfill Manipulator Recall that in the manipulator setw, if the number of columns specified exceeds the number of columns required by the expression, the output of the expression is right-justified and the unused columns to the left are filled with spaces. The output stream variables can use the manipulator setfill to fill the unused columns with a character other than a space. The syntax to use the manipulator setfill is:
cout << setfill('#');

sets the fill character to '#' on the standard output device. To use the manipulator setfill, the program must include the header file iomanip.h. Example: Run the following program on your system to better understand:
#include <iostream.h> #include <iomanip.h> int main() { int a =2, b=3; cout<<setw(5)<< a << setw(7) << b << endl<<endl; cout<<setfill('*'); cout<<setw(5)<< a << setw(7) << b << endl<<endl; cout<<setw(5)<< a << setw(7) << setfill('#')<< b << endl<<endl; cout<<setw(5)<<setfill('@')<<a<<setw(7)<<setfill('#')<<b<<endl<<endl; cout<<setfill(' '); cout<<setw(5)<< a << setw(7) << b << endl<<endl; return 0; }

~8~

5.

left and right Manipulators

Recall that if the number of columns specified in the setw manipulator exceeds the number of columns required by the next expression, the default output is right-justified. Sometimes, you might want the output to be left-justified. To left-justify the output, you use the manipulator left. Example: Run the following program on your system to better understand:
#include <iostream.h> #include <iomanip.h> int main() { int a =2, b=3; cout.setf(ios::left); //setting the setw to left

cout << setw(5)<< a << setw(7) << b << endl<<endl; cout.unsetf(ios::left); cout.setf(ios::right); cout << setfill('*'); cout << setw(5)<< a << setw(7) << b << endl<<endl; cout << setw(5)<< a << setw(7) << setfill('#')<< b << endl<<endl; cout.unsetf(ios::right); cout.setf(ios::left); //unsetting the setw to right //setting the setw to left //unsetting the setw to left //setting the setw to right

cout << setw(5) << setfill('@') << a << setw(7) << setfill('#') << b << endl<<endl; cout << setfill(' '); //Line 9 cout << setw(5)<< a << setw(7) << b << endl<<endl; return 0; }

~9~

6.

Generating Random Numbers We can generate random numbers using the following function: rand(); rand() function returns a random number which we need to store in a variable, so in our programs we use rand() function as given below: a = rand(); where a is an integer variable. rand() returns an int value between 0 and 32767. In order to generate a random number between a 0 and a specific number n, use the following: random(n) It generates a random number between 0 to n-1

In order to use these functions include stdlib.h in your program. Example: Run the following program on your system to better understand:
#include <iostream.h> #include <stdlib.h> int main() { int a=0, n=60; a = rand(); cout<<a<<endl; a = random(n); cout<<a<<endl; return 0; }

7.

Mathematical Functions: Some of the important mathematical functions in header file math.h are Function sin(x) cos(x) tan(x) asin(x) acos(x) exp(x) log(x) log 10(x) sqrt(x) pow(x, y) abs(x) fabs(x) Meaning Sine of an angle x (measured in radians) Cosine of an angle x (measured in radians) Tangent of an angle x (measured in radians) Sin-1 (x) where x (measured in radians) Cos-1 (x) where x (measured in radians) Exponential function of x (ex) logarithm of x Logarithm of number x to the base 10 Square root of x x raised to the power y Absolute value of integer number x Absolute value of real number x

All of the above functions will return a value so in order to store that value use a variable as given below: a = sin(x); In the above example, sin of x is calculated and stored in variable a, which is already declared.

~ 10 ~

S-ar putea să vă placă și