LAB CP Assignment 1
LAB CP Assignment 1
Taxila
2020
[Computer Programming]
Assignment No 1
SUBMITTED BY:
SHEIKH ABUZAR AMJAD
REG NO:
19-CP-6
SUBMITTED TO:
SIR SHEHRYAR KHAN
Task No 01:-
We want to design an algorithm that calculates the sales tax and the price of an
item sold in a particular state. We have been given following terms information.
The sales tax is calculated as follows: The state’s portion of the sales tax is 4%,
and the city’s portion of the sales tax is 1.5%. If the item is a luxury item, such
as a car more than $50,000, then there is a 10% luxury tax.
To calculate the price of the item, we need to calculate the state’s portion of the
sales tax, the city’s portion of the sales tax, and, if it is a luxury item, the luxury
tax.
Suppose salePrice denotes the selling price of the item, stateSalesTax denotes the
state’s sales tax, citySalesTax denotes the city’s sales tax, luxuryTax denotes the
luxury tax, salesTax denotes the total sales tax, and amountDue denotes the final
price of the item.
Algorithm
To Calculate the sales tax, we must know the selling price of the item and whether the item is a
luxury item. The state Sales Tax and city Sales Tax can be calculated as:
1|Page
Task No 02:-
We want to design an algorithm that calculates the monthly paycheck of a
salesperson at a local department store.
We have been given following constraints:-
Every salesperson has a base salary. The salesperson also receives a bonus at
the end of each month, based on the following criteria: If the salesperson has
been with the store for five years or less, the bonus is $10 for each year that he
or she has worked there. If the salesperson has been with the store for more
than five years, the bonus is $20 for each year that he or she has worked there.
The salesperson can earn an additional bonus as follows: If the total sales made
by the salesperson for the month are at least $5,000 but less than $10,000, he or
she receives a 3% commission on the sale. If the total sales made by the
salesperson for the month are at least $10,000, he or she receives a 6%
commission on the sale.
To calculate a salesperson’s monthly paycheck, you need to know the base
salary, the number of years that the salesperson has been with the company,
and the total sales made by the salesperson for that month. Suppose baseSalary
denotes the base salary, no Of Service Years denotes the number of years that
the salesperson has been with the store, bonus denotes the bonus, totalSales
denotes the total sales made by the salesperson for the month, and additional
Bonus denotes the additional bonus.
ALGORITHM
1. Get base salary.
2. Get no of service Years.
3. Calculate bonus using the following formula:
If (no of service Years is less than or equal to five)
Bonus = 10. no of service Years
Otherwise
Bonus = 20. No of service Years
4. Get total sales.
5. Calculate additional bonus using the formula:
Additional bonus =0
Otherwise
2|Page
If (total Sales is greater than or equal to 5000 and total Sales is less than 10000)
Otherwise
Task No 03:-
We want to design an algorithm to play a number-guessing game.
The objective is to randomly generate an integer greater than or equal to 0 and
less than 100. Then prompt the player (user) to guess the number. If the player
guesses the number correctly, output an appropriate message. Otherwise,
check whether the guessed number is less than the random number. If the
guessed number is less than the random number generated, output the message,
‘‘Your guess is lower than the number. Guess again!’’; otherwise, output the
message, ‘‘Your guess is higher than the number. Guess again!’’. Then prompt
the player to enter another number. The player is prompted to guess the
random number until the player enters the correct number.
Algorithm
1. Generate a random number and call it num.
2. Repeat the following steps until the player has guessed the correct number.
3. Prompt the player to enter guess.
If (guess is equal to num)
Print “You Guessed the Correct Number”
Otherwise
If (guess is less than num)
Print “Your Guess is lower than the number. Guess again!”
Otherwise
Print “Your Guess is Higher than the number. Guess again!”
Task No 04:-
There are 10 students in a class. Each student has taken five tests, and each test
is worth 100 points. We want to design an algorithm to calculate the grade for
each student, as well as the class average. The grade is assigned as follows: If
3|Page
the average test score is greater than or equal to 90, the grade is A; if the
average test score is greater than or equal to 80 and less than 90, the grade is
B; if the average test score is greater than or equal to 70 and less than 80, the
grade is C; if the average test score is greater than or equal to 60 and less than
70, the grade is D; otherwise, the grade is F. Note that the data consists of
students’ names and their test scores.
ALGORITHM
First we design the algorithm to test the average test score.
1. Get the five test scores
2. Add the five test scores. Suppose sum stands for the sum of the test
scores.
3. Suppose average stands for the average test score. Then
Average = Sum/5;
Next, you design an algorithm to determine the grade. Suppose grade stands for
the grade assigned to a student. The following algorithm determines the grade:
1. if (average is greater than or equal to 90)
Print “grade = A”
Otherwise
2. if (average is greater than or equal to 80 and less than 90)
Print “grade = B”
Otherwise
3. if (average is greater than or equal to 70 and less than 80)
Print “grade = C”
Otherwise
4. if (average is greater than or equal to 60 and less than 70)
Print “grade = D”
Otherwise
Print “grade = F”
We can use the solutions to these subproblems to design the main algorithm as follows:
1. total Average = o;
2. Repeat the following steps for the each student in the class:
a) Get student's name.
b) Use the algorithm as discussed above to find the average test score.
c) Use the algorithm as discussed above to find the grade.
d) Update totalAverage by adding the current student's average test score.
4|Page
Home Assignment
1. To make a profit, the prices of the items sold in a
furniture store are marked up by 80%. After marking up
the prices each item is put on sale at a discount of
10%. Design an algorithm to find the selling price of
an item sold at the furniture store. What information
do you need to find the selling price?
INFORMATION
The Algorithm to find the selling price of an item sold at furniture store is:
a) Input:
• Original Price of Product
b) Output:
• Selling Price of Product
ALGORITHM
1. Start
2. Get the Original Price of Product in Furniture shop.
3. Find the Market Price using the following formula:
൫𝑶𝒓𝒊𝒈𝒊𝒏𝒂𝒍 𝑷𝒓𝒊𝒄𝒆×𝟖𝟎൯
Market Price =
𝟏𝟎𝟎
4. Then set the new price of the product as:
New Price = Market Price + Original Price
5. Find the Discounted Price as follow:
ሺ𝑵𝒆𝒘 𝑷𝒓𝒊𝒄𝒆×𝟏𝟎ሻ
Discount =
𝟏𝟎𝟎
6. Find the Selling Price using the following equation as:
Selling Price = New Price – Discount
7. Print selling price on standard output.
8. Stop.
5|Page
2. Suppose a, b, and c denote the lengths of the sides
of a triangle. Then the area of the triangle can be
calculated using the formula:
ඥ𝒔ሺ𝒔 − 𝒂ሻሺ𝒔 − 𝒃ሻሺ𝒔 − 𝒄ሻ
Where
S = (1/2)(a+b+c)
Design an algorithm that uses this formula to find the
area of a triangle. What information do you need to
find the area?
INFORMATION
The information that we need to find area is the perimeter and the sides lengths of
triangle a,b,c.
Suppose perimeter = a+b+c
1. Start
2. Get the side lengths
a = value given, b = value given, c = value given
3. Determine perimeter
perimeter = a + b +c
4. Determine value of s
s = perimeter / 2
5. Calculate the side values under the radical of the formula
aCalc = s - a
bCalc = s – b
cCalc = s – c
6. Calculate value under the radical of the formula
calcVal = s * aCalc * bCalc * cCalc
7. Determine the area of the triangle
area = call square root function from library (calcVal)
8. Display area
9. Stop
6|Page
3. Jason typically uses the Internet to buy various
items. If the total cost of the items ordered, at one
time, is $200 or more, then the shipping and handling
is free; otherwise, the shipping and handling is $10
per item. Design an algorithm that prompts Jason to
enter the number of items ordered and the price of
each item. The algorithm then outputs the total
billing amount.
ALGORITHM
Suppose total is the sum of the cost of items purchased. Suppose total due
amount is the sum of total amount and shipping fee.
1. Prompt user for number of items purchased
No of tems = n
2. For each item use loop for 1 to n
Prompt user for cost of each item
Add cost to total
3. Determine shipping cost
If (total is greater than or equal to $200)
shipping = 0
otherwise
shipping = $10 * No of items
4. Determine total amt due
totalAmtDue = total + shipping
5. Display total amt due
We know if the number of pages to be faxed is greater than 10, then the baseCharge is $2 (10 pg
@.20 per page is $2.00). Suppose svcCharge is the $3 service charge. Suppose numPages is the
number of pages entered by user. Suppose cost is the total cost of the pages to be faxed. Suppose
total Amount Due is svc Charge + cost.
ALGORITHM
7|Page
1. Start
2. Prompt user for number of pages to be faxed
3. Determine cost
if (numPages is less than or equal to 10)
cost = numPages * 0.20
otherwise
cost = baseCharge + (numPages – 10) * 0.10
THE end
8|Page