SlideShare a Scribd company logo
1 of 4
Departmentof ComputerScience
CSC-113 ComputerProgramming
Topic:Algorithms
1
EXAMPLE 1
In this example, we design an algorithm that calculates the sales tax and the price of an item sold
in a particular state.
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.
To calculate the sales tax, we must know the selling price of the item and whether the item is a
luxury item.
The stateSalesTax and citySalesTax can be calculated using the following formulas:
stateSalesTax = salePrice *0.04
citySalesTax = salePrice *0.015
Next, you can determine luxuryTax as follows:
if (item is a luxury item)
luxuryTax = salePrice _*0.1
otherwise
luxuryTax = 0
Next, you can determine salesTax as follows:
salesTax = stateSalesTax + citySalesTax + luxuryTax
Finally, you can calculate amountDue as follows:
amountDue = salePrice + salesTax
Departmentof ComputerScience
CSC-113 ComputerProgramming
Topic:Algorithms
2
ALGORITHM
The algorithm to determine salesTax and amountDue is, therefore:
1. Get the selling price of the item.
2. Determine whether the item is a luxury item.
3. Find the state’s portion of the sales tax using the formula:
stateSalesTax = salePrice *0.04
4. Find the city’s portion of the sales tax using the formula:
citySalesTax = salePrice * 0.015
5. Find the luxury tax using the following formula:
if (item is a luxury item)
luxuryTax = salePrice * 0.1
otherwise
luxuryTax = 0
6. Find salesTax using the formula:
salesTax = stateSalesTax + citySalesTax + luxuryTax
7. Find amountDue using the formula:
amountDue = salePrice + salesTax
EXAMPLE 2:
In this example, we 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.
The first step is to generate a random number. Suppose num stands for the random number and
guess stands for the number guessed by the player.
After the player enters the guess, you can compare the guess with the random number as follows:
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!"
Departmentof ComputerScience
CSC-113 ComputerProgramming
Topic:Algorithms
3
ALGORITHM:
1. Generate a random number and call it num.
2. Repeat the following steps until the player has guessed the correct number:
a. Prompt the player to enter guess.
b. Check the value of 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!"
EXAMPLE 3
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 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.
This is a problem that can be divided into subproblems as follows: There are five tests, so you
design an algorithm to find the average test score. Next, you design an algorithm to determine the
grade. The two subproblems are to determine the average test score and to determine the grade.
ALGORITHM
Let us first design an algorithm to determine the average test score. To find the average test score,
add the five test scores and then divide the sum by 5. Therefore, the algorithm is the following:
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:
if average is greater than or equal to 90
grade = A
otherwise
if average is greater than or equal to 80
grade = B
Departmentof ComputerScience
CSC-113 ComputerProgramming
Topic:Algorithms
4
otherwise
if average is greater than or equal to 70
grade = C
otherwise
if average is greater than or equal to 60
grade = D
otherwise
grade = F
You can use the solutions to these subproblems to design the main algorithm as follows:
(Suppose totalAverage stands for the sum of the averages of each student’s test average.)
1. totalAverage = 0;
2. Repeat the following steps for 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.
3. Determine the class average as follows:
classAverage = totalAverage / 10

More Related Content

Viewers also liked

Winu invtv lesson plan
Winu invtv lesson planWinu invtv lesson plan
Winu invtv lesson planreshmafmtc
 
Ict lesson plan
Ict lesson planIct lesson plan
Ict lesson planreshmafmtc
 
My Top 10 Favorite Ronnie Fieg ASICS Collabs
My Top 10 Favorite Ronnie Fieg ASICS CollabsMy Top 10 Favorite Ronnie Fieg ASICS Collabs
My Top 10 Favorite Ronnie Fieg ASICS CollabsIvan Gomez
 
Translate english to brazilian portuguese
Translate english to brazilian portugueseTranslate english to brazilian portuguese
Translate english to brazilian portugueseS4U Languages
 
Winu ict lessonplan
Winu ict lessonplanWinu ict lessonplan
Winu ict lessonplanreshmafmtc
 
Crisis: ¿Cómo superarla?
Crisis: ¿Cómo superarla?Crisis: ¿Cómo superarla?
Crisis: ¿Cómo superarla?Marco Tapia Mera
 
Winu innvtv work
Winu innvtv workWinu innvtv work
Winu innvtv workreshmafmtc
 

Viewers also liked (12)

Enfermedad de pompee
Enfermedad de pompeeEnfermedad de pompee
Enfermedad de pompee
 
Abdomen agudo
Abdomen agudoAbdomen agudo
Abdomen agudo
 
Winu invtv lesson plan
Winu invtv lesson planWinu invtv lesson plan
Winu invtv lesson plan
 
Ict lesson plan
Ict lesson planIct lesson plan
Ict lesson plan
 
My Top 10 Favorite Ronnie Fieg ASICS Collabs
My Top 10 Favorite Ronnie Fieg ASICS CollabsMy Top 10 Favorite Ronnie Fieg ASICS Collabs
My Top 10 Favorite Ronnie Fieg ASICS Collabs
 
Translate english to brazilian portuguese
Translate english to brazilian portugueseTranslate english to brazilian portuguese
Translate english to brazilian portuguese
 
2015 04-25 01-17-11120379
2015 04-25 01-17-111203792015 04-25 01-17-11120379
2015 04-25 01-17-11120379
 
Winu ict lessonplan
Winu ict lessonplanWinu ict lessonplan
Winu ict lessonplan
 
DCN dos Cursos de Engenharia - Brasil -2002
DCN dos Cursos de Engenharia - Brasil -2002DCN dos Cursos de Engenharia - Brasil -2002
DCN dos Cursos de Engenharia - Brasil -2002
 
Crisis: ¿Cómo superarla?
Crisis: ¿Cómo superarla?Crisis: ¿Cómo superarla?
Crisis: ¿Cómo superarla?
 
Winu innvtv work
Winu innvtv workWinu innvtv work
Winu innvtv work
 
How the automotive industry reshaped urban America
How the automotive industry reshaped urban AmericaHow the automotive industry reshaped urban America
How the automotive industry reshaped urban America
 

Similar to Casestudy 01 algorithms

Course project solutions 2018
Course project solutions 2018Course project solutions 2018
Course project solutions 2018Robert Geofroy
 
Skip To ContentOpen Quick LinksQuick LinksPage LandmarksCont.docx
Skip To ContentOpen Quick LinksQuick LinksPage LandmarksCont.docxSkip To ContentOpen Quick LinksQuick LinksPage LandmarksCont.docx
Skip To ContentOpen Quick LinksQuick LinksPage LandmarksCont.docxedgar6wallace88877
 
presentation_python_11_1569171345_375360.pptx
presentation_python_11_1569171345_375360.pptxpresentation_python_11_1569171345_375360.pptx
presentation_python_11_1569171345_375360.pptxGAURAVRATHORE86
 
Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1Techglyphs
 
A General Manger of Harley-Davidson has to decide on the size of a.docx
A General Manger of Harley-Davidson has to decide on the size of a.docxA General Manger of Harley-Davidson has to decide on the size of a.docx
A General Manger of Harley-Davidson has to decide on the size of a.docxevonnehoggarth79783
 
WEKA:Credibility Evaluating Whats Been Learned
WEKA:Credibility Evaluating Whats Been LearnedWEKA:Credibility Evaluating Whats Been Learned
WEKA:Credibility Evaluating Whats Been Learnedweka Content
 
WEKA: Credibility Evaluating Whats Been Learned
WEKA: Credibility Evaluating Whats Been LearnedWEKA: Credibility Evaluating Whats Been Learned
WEKA: Credibility Evaluating Whats Been LearnedDataminingTools Inc
 
SOP For Progressive Verification (Times New Roman)
SOP For Progressive Verification (Times New Roman)SOP For Progressive Verification (Times New Roman)
SOP For Progressive Verification (Times New Roman)Donald Console
 
Statement of Operations and Financial StatementsSubmit written r.docx
Statement of Operations and Financial StatementsSubmit written r.docxStatement of Operations and Financial StatementsSubmit written r.docx
Statement of Operations and Financial StatementsSubmit written r.docxwhitneyleman54422
 
ch1. .ppt
ch1. .pptch1. .ppt
ch1. .pptmazber1
 
selection.ppt
selection.pptselection.ppt
selection.pptalaguap
 
Empowerment Technology Lesson 4
Empowerment Technology Lesson 4Empowerment Technology Lesson 4
Empowerment Technology Lesson 4alicelagajino
 
Machine Learning Application: Credit Scoring
Machine Learning Application: Credit ScoringMachine Learning Application: Credit Scoring
Machine Learning Application: Credit Scoringeurosigdoc acm
 
CIS 115 Education for Service--cis115.com
CIS 115 Education for Service--cis115.com  CIS 115 Education for Service--cis115.com
CIS 115 Education for Service--cis115.com williamwordsworth10
 
CIS 115 Redefined Education--cis115.com
CIS 115 Redefined Education--cis115.comCIS 115 Redefined Education--cis115.com
CIS 115 Redefined Education--cis115.comagathachristie208
 

Similar to Casestudy 01 algorithms (20)

Course project solutions 2018
Course project solutions 2018Course project solutions 2018
Course project solutions 2018
 
Skip To ContentOpen Quick LinksQuick LinksPage LandmarksCont.docx
Skip To ContentOpen Quick LinksQuick LinksPage LandmarksCont.docxSkip To ContentOpen Quick LinksQuick LinksPage LandmarksCont.docx
Skip To ContentOpen Quick LinksQuick LinksPage LandmarksCont.docx
 
Simulations
SimulationsSimulations
Simulations
 
presentation_python_11_1569171345_375360.pptx
presentation_python_11_1569171345_375360.pptxpresentation_python_11_1569171345_375360.pptx
presentation_python_11_1569171345_375360.pptx
 
Data Transformation
Data TransformationData Transformation
Data Transformation
 
Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1
 
A General Manger of Harley-Davidson has to decide on the size of a.docx
A General Manger of Harley-Davidson has to decide on the size of a.docxA General Manger of Harley-Davidson has to decide on the size of a.docx
A General Manger of Harley-Davidson has to decide on the size of a.docx
 
WEKA:Credibility Evaluating Whats Been Learned
WEKA:Credibility Evaluating Whats Been LearnedWEKA:Credibility Evaluating Whats Been Learned
WEKA:Credibility Evaluating Whats Been Learned
 
WEKA: Credibility Evaluating Whats Been Learned
WEKA: Credibility Evaluating Whats Been LearnedWEKA: Credibility Evaluating Whats Been Learned
WEKA: Credibility Evaluating Whats Been Learned
 
SOP For Progressive Verification (Times New Roman)
SOP For Progressive Verification (Times New Roman)SOP For Progressive Verification (Times New Roman)
SOP For Progressive Verification (Times New Roman)
 
If switch structure
If switch structureIf switch structure
If switch structure
 
Statement of Operations and Financial StatementsSubmit written r.docx
Statement of Operations and Financial StatementsSubmit written r.docxStatement of Operations and Financial StatementsSubmit written r.docx
Statement of Operations and Financial StatementsSubmit written r.docx
 
ch1. .ppt
ch1. .pptch1. .ppt
ch1. .ppt
 
selection.ppt
selection.pptselection.ppt
selection.ppt
 
Python Math Concepts Book
Python Math Concepts BookPython Math Concepts Book
Python Math Concepts Book
 
Chap 4 c++
Chap 4 c++Chap 4 c++
Chap 4 c++
 
Empowerment Technology Lesson 4
Empowerment Technology Lesson 4Empowerment Technology Lesson 4
Empowerment Technology Lesson 4
 
Machine Learning Application: Credit Scoring
Machine Learning Application: Credit ScoringMachine Learning Application: Credit Scoring
Machine Learning Application: Credit Scoring
 
CIS 115 Education for Service--cis115.com
CIS 115 Education for Service--cis115.com  CIS 115 Education for Service--cis115.com
CIS 115 Education for Service--cis115.com
 
CIS 115 Redefined Education--cis115.com
CIS 115 Redefined Education--cis115.comCIS 115 Redefined Education--cis115.com
CIS 115 Redefined Education--cis115.com
 

Recently uploaded

Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...nirzagarg
 
Charbagh + Female Escorts Service in Lucknow | Starting ₹,5K To @25k with A/C...
Charbagh + Female Escorts Service in Lucknow | Starting ₹,5K To @25k with A/C...Charbagh + Female Escorts Service in Lucknow | Starting ₹,5K To @25k with A/C...
Charbagh + Female Escorts Service in Lucknow | Starting ₹,5K To @25k with A/C...HyderabadDolls
 
Fun all Day Call Girls in Jaipur 9332606886 High Profile Call Girls You Ca...
Fun all Day Call Girls in Jaipur   9332606886  High Profile Call Girls You Ca...Fun all Day Call Girls in Jaipur   9332606886  High Profile Call Girls You Ca...
Fun all Day Call Girls in Jaipur 9332606886 High Profile Call Girls You Ca...kumargunjan9515
 
High Profile Call Girls Service in Jalore { 9332606886 } VVIP NISHA Call Girl...
High Profile Call Girls Service in Jalore { 9332606886 } VVIP NISHA Call Girl...High Profile Call Girls Service in Jalore { 9332606886 } VVIP NISHA Call Girl...
High Profile Call Girls Service in Jalore { 9332606886 } VVIP NISHA Call Girl...kumargunjan9515
 
Aspirational Block Program Block Syaldey District - Almora
Aspirational Block Program Block Syaldey District - AlmoraAspirational Block Program Block Syaldey District - Almora
Aspirational Block Program Block Syaldey District - AlmoraGovindSinghDasila
 
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...nirzagarg
 
7. Epi of Chronic respiratory diseases.ppt
7. Epi of Chronic respiratory diseases.ppt7. Epi of Chronic respiratory diseases.ppt
7. Epi of Chronic respiratory diseases.pptibrahimabdi22
 
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...nirzagarg
 
Statistics notes ,it includes mean to index numbers
Statistics notes ,it includes mean to index numbersStatistics notes ,it includes mean to index numbers
Statistics notes ,it includes mean to index numberssuginr1
 
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...HyderabadDolls
 
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制vexqp
 
Discover Why Less is More in B2B Research
Discover Why Less is More in B2B ResearchDiscover Why Less is More in B2B Research
Discover Why Less is More in B2B Researchmichael115558
 
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...nirzagarg
 
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...gajnagarg
 
TrafficWave Generator Will Instantly drive targeted and engaging traffic back...
TrafficWave Generator Will Instantly drive targeted and engaging traffic back...TrafficWave Generator Will Instantly drive targeted and engaging traffic back...
TrafficWave Generator Will Instantly drive targeted and engaging traffic back...SOFTTECHHUB
 
Ranking and Scoring Exercises for Research
Ranking and Scoring Exercises for ResearchRanking and Scoring Exercises for Research
Ranking and Scoring Exercises for ResearchRajesh Mondal
 
Vadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book now
Vadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book nowVadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book now
Vadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book nowgargpaaro
 
Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...gajnagarg
 
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...gajnagarg
 

Recently uploaded (20)

Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
 
Charbagh + Female Escorts Service in Lucknow | Starting ₹,5K To @25k with A/C...
Charbagh + Female Escorts Service in Lucknow | Starting ₹,5K To @25k with A/C...Charbagh + Female Escorts Service in Lucknow | Starting ₹,5K To @25k with A/C...
Charbagh + Female Escorts Service in Lucknow | Starting ₹,5K To @25k with A/C...
 
Fun all Day Call Girls in Jaipur 9332606886 High Profile Call Girls You Ca...
Fun all Day Call Girls in Jaipur   9332606886  High Profile Call Girls You Ca...Fun all Day Call Girls in Jaipur   9332606886  High Profile Call Girls You Ca...
Fun all Day Call Girls in Jaipur 9332606886 High Profile Call Girls You Ca...
 
High Profile Call Girls Service in Jalore { 9332606886 } VVIP NISHA Call Girl...
High Profile Call Girls Service in Jalore { 9332606886 } VVIP NISHA Call Girl...High Profile Call Girls Service in Jalore { 9332606886 } VVIP NISHA Call Girl...
High Profile Call Girls Service in Jalore { 9332606886 } VVIP NISHA Call Girl...
 
Aspirational Block Program Block Syaldey District - Almora
Aspirational Block Program Block Syaldey District - AlmoraAspirational Block Program Block Syaldey District - Almora
Aspirational Block Program Block Syaldey District - Almora
 
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
 
7. Epi of Chronic respiratory diseases.ppt
7. Epi of Chronic respiratory diseases.ppt7. Epi of Chronic respiratory diseases.ppt
7. Epi of Chronic respiratory diseases.ppt
 
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
 
Statistics notes ,it includes mean to index numbers
Statistics notes ,it includes mean to index numbersStatistics notes ,it includes mean to index numbers
Statistics notes ,it includes mean to index numbers
 
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...
 
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
 
Discover Why Less is More in B2B Research
Discover Why Less is More in B2B ResearchDiscover Why Less is More in B2B Research
Discover Why Less is More in B2B Research
 
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
 
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
 
TrafficWave Generator Will Instantly drive targeted and engaging traffic back...
TrafficWave Generator Will Instantly drive targeted and engaging traffic back...TrafficWave Generator Will Instantly drive targeted and engaging traffic back...
TrafficWave Generator Will Instantly drive targeted and engaging traffic back...
 
Ranking and Scoring Exercises for Research
Ranking and Scoring Exercises for ResearchRanking and Scoring Exercises for Research
Ranking and Scoring Exercises for Research
 
Vadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book now
Vadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book nowVadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book now
Vadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book now
 
Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...
 
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
 

Casestudy 01 algorithms

  • 1. Departmentof ComputerScience CSC-113 ComputerProgramming Topic:Algorithms 1 EXAMPLE 1 In this example, we design an algorithm that calculates the sales tax and the price of an item sold in a particular state. 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. To calculate the sales tax, we must know the selling price of the item and whether the item is a luxury item. The stateSalesTax and citySalesTax can be calculated using the following formulas: stateSalesTax = salePrice *0.04 citySalesTax = salePrice *0.015 Next, you can determine luxuryTax as follows: if (item is a luxury item) luxuryTax = salePrice _*0.1 otherwise luxuryTax = 0 Next, you can determine salesTax as follows: salesTax = stateSalesTax + citySalesTax + luxuryTax Finally, you can calculate amountDue as follows: amountDue = salePrice + salesTax
  • 2. Departmentof ComputerScience CSC-113 ComputerProgramming Topic:Algorithms 2 ALGORITHM The algorithm to determine salesTax and amountDue is, therefore: 1. Get the selling price of the item. 2. Determine whether the item is a luxury item. 3. Find the state’s portion of the sales tax using the formula: stateSalesTax = salePrice *0.04 4. Find the city’s portion of the sales tax using the formula: citySalesTax = salePrice * 0.015 5. Find the luxury tax using the following formula: if (item is a luxury item) luxuryTax = salePrice * 0.1 otherwise luxuryTax = 0 6. Find salesTax using the formula: salesTax = stateSalesTax + citySalesTax + luxuryTax 7. Find amountDue using the formula: amountDue = salePrice + salesTax EXAMPLE 2: In this example, we 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. The first step is to generate a random number. Suppose num stands for the random number and guess stands for the number guessed by the player. After the player enters the guess, you can compare the guess with the random number as follows: 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!"
  • 3. Departmentof ComputerScience CSC-113 ComputerProgramming Topic:Algorithms 3 ALGORITHM: 1. Generate a random number and call it num. 2. Repeat the following steps until the player has guessed the correct number: a. Prompt the player to enter guess. b. Check the value of 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!" EXAMPLE 3 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 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. This is a problem that can be divided into subproblems as follows: There are five tests, so you design an algorithm to find the average test score. Next, you design an algorithm to determine the grade. The two subproblems are to determine the average test score and to determine the grade. ALGORITHM Let us first design an algorithm to determine the average test score. To find the average test score, add the five test scores and then divide the sum by 5. Therefore, the algorithm is the following: 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: if average is greater than or equal to 90 grade = A otherwise if average is greater than or equal to 80 grade = B
  • 4. Departmentof ComputerScience CSC-113 ComputerProgramming Topic:Algorithms 4 otherwise if average is greater than or equal to 70 grade = C otherwise if average is greater than or equal to 60 grade = D otherwise grade = F You can use the solutions to these subproblems to design the main algorithm as follows: (Suppose totalAverage stands for the sum of the averages of each student’s test average.) 1. totalAverage = 0; 2. Repeat the following steps for 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. 3. Determine the class average as follows: classAverage = totalAverage / 10