SlideShare a Scribd company logo
1 of 15
CSCI 101
Algorithms II
Elsayed Hemayed
Overview
• Multiplication table
• Matrix multiplication
• Sorting
• Practice Problems
Displaying the multiplication table
for i=1:5
for j=1:5
fprintf(‘%8d’,i*j);
end
fprintf(‘n’);
end 1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Displaying a modified multiplication table
for i=1:5
for j=i:5
fprintf(‘%8d’,i*j);
end
fprintf(‘n’);
end 1 2 3 4 5
4 6 8 10
9 12 15
16 20
25
Matrices Multiplication
• Given matrix A [n][k] and matrix B[k][m] find
their multiplication matrix C[n][m].
i
j
i
a
a
j
=x
A [n][k] B[k][m] C[n][m]
Matrices Multiplication
for i =1 to n
for j=1 to m
C[i][j]=0
for a=1 to k
C[i][j]=C[i][j]+A[i][a]*B[a][j]
i
j
i
a
a
j
=x
Sorting
• Given a list of N integer values, sort them in an
ascending order.
for i=1 to N
for j=i+1 to N
If A[i]>A[j]
swap A[i] and A[j]
5
4
2
1
6
Array Sorting
X=input(‘enter an array:’);
for i=1:length(X)
for j=i+1:length(X)
if X(i)>X(j)
t=X(i);
X(i)=X(j);
X(j)=t;
end
end
end
disp(X);
5
4
2
1
6
4
5
2
1
6
2
5
4
1
6
1
5
4
2
6
i=1
j= 2 3 4 5
1
5
4
2
6
enter an array: [5 4 2 1 6]
Swapping values
What is the output of this program?
N=input(‘enter a number:’);
for i=1:N
for j=1:i
fprintf(‘*’);
end
fprintf(‘n’);
end
Enter a number: 5
*
**
***
****
*****
Practice Problems
Problem 1
Write a program that reads a number N from the
user and prints an array containing the numbers
(from 1 to N) that are multiples of 3 or 5
Also print their sum
Input/Output Sample:
Enter number: 10
The array that has multiples of 3 or 5 = 3 5 6 9 10
The sum of the multiples of 3 or 5 = 33
Problem 2
N children at a party eat scoops of ice cream, write a matlab program
to calculate how many scoops of ice cream do they eat altogether and
who eats more, if the user enters a negative number, the program asks
the user to re-enter the number.
Input/Output Sample:
Enter the number of children: 4
Enter number of scoops/ child: 1
Enter number of scoops/ child: -2
Error! Enter Positive Number: 4
Enter number of scoops/ child: 5
Enter number of scoops/ child: 3
Number of ice cream scoops all children ate: 13
Child ate more ice scoops: 3
Problem 3
In train station, the price of ticket depends on the destination
distance. There is a 20% discount for prices>160, 5% for
prices<100 and 10% for any other prices.
Write a matlab script to print final price for each station.
The program can be executed 3 times.
Input/Output Sample:
Enter tickets price/station: [150 210 50 75 80 170]
Discounted prices are: 135.00 168.00 47.50 71.25 76.00 136.00
Problem 4
Write MATLAB code that reads an array from user and
counts the number of elements that have values between
50 and 90 and calculates the max, and average of these
numbers. If the array does not contain numbers in the
required range the program should display “there are no
numbers between 50, 90 in the array”
Input/Output Sample:
Enter your array of numbers: [2 3 55 88 65 57 99 78]
Count of numbers between 50, 90 is: 5
Max is: 88
Avg is: 68.6
Problem 5
A customer want to buy a TV at least price. Write a program to help
him to know the average price of the TV and the store at which the
least price. The program should ask the customer about the number of
stores that have the required TV, and he should then enter the TV
price at each store. Then the program should print the average price of
TV in all stores, and where the least price.
Input/Output Sample:
Enter the number of stores have the required TV: 4
Enter the price of the TV in this store: 2800
Enter the price of the TV in this store: 2780
Enter the price of the TV in this store: 2930
Enter the price of the TV in this store: 2855
The average price of TV in all stores: 2841.25
The store have the least TV price: 2

More Related Content

Similar to Csci101 lect07 algorithms_ii

CIS 115 Inspiring Innovation/tutorialrank.com
 CIS 115 Inspiring Innovation/tutorialrank.com CIS 115 Inspiring Innovation/tutorialrank.com
CIS 115 Inspiring Innovation/tutorialrank.comjonhson110
 
05 control structures 2
05 control structures 205 control structures 2
05 control structures 2Jomel Penalba
 
CIS 115 Effective Communication - tutorialrank.com
CIS 115  Effective Communication - tutorialrank.comCIS 115  Effective Communication - tutorialrank.com
CIS 115 Effective Communication - tutorialrank.comBartholomew18
 
COMP 122 Entire Course NEW
COMP 122 Entire Course NEWCOMP 122 Entire Course NEW
COMP 122 Entire Course NEWshyamuopeight
 
CPSC 120Spring 2014Lab 5Name _____________________.docx
CPSC 120Spring 2014Lab 5Name _____________________.docxCPSC 120Spring 2014Lab 5Name _____________________.docx
CPSC 120Spring 2014Lab 5Name _____________________.docxfaithxdunce63732
 
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
 
Percentage and its applications /COMMERCIAL MATHEMATICS
Percentage and its applications /COMMERCIAL MATHEMATICSPercentage and its applications /COMMERCIAL MATHEMATICS
Percentage and its applications /COMMERCIAL MATHEMATICSindianeducation
 
Mat 540 week 11 final exam newly taken 2016
Mat 540 week 11 final exam newly taken 2016Mat 540 week 11 final exam newly taken 2016
Mat 540 week 11 final exam newly taken 2016bestwriter
 
Mat 540 week 11 final exam newly taken 2016
Mat 540 week 11 final exam newly taken 2016Mat 540 week 11 final exam newly taken 2016
Mat 540 week 11 final exam newly taken 2016Patrickrasacs
 
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab neweyavagal
 
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newLast7693
 
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newscottbrownnn
 
Sample Program file class 11.pdf
Sample Program file class 11.pdfSample Program file class 11.pdf
Sample Program file class 11.pdfYashMirge2
 
As mentioned earlier, the mid-term will have conceptual and quanti.docx
As mentioned earlier, the mid-term will have conceptual and quanti.docxAs mentioned earlier, the mid-term will have conceptual and quanti.docx
As mentioned earlier, the mid-term will have conceptual and quanti.docxfredharris32
 
Cis 115 Education Organization -- snaptutorial.com
Cis 115   Education Organization -- snaptutorial.comCis 115   Education Organization -- snaptutorial.com
Cis 115 Education Organization -- snaptutorial.comDavisMurphyB99
 
Csphtp1 05
Csphtp1 05Csphtp1 05
Csphtp1 05HUST
 
ECO 520 Case Study One Production and Cost Guidelines and Rubric.docx
ECO 520 Case Study One Production and Cost Guidelines and Rubric.docxECO 520 Case Study One Production and Cost Guidelines and Rubric.docx
ECO 520 Case Study One Production and Cost Guidelines and Rubric.docxjack60216
 

Similar to Csci101 lect07 algorithms_ii (20)

CIS 115 Inspiring Innovation/tutorialrank.com
 CIS 115 Inspiring Innovation/tutorialrank.com CIS 115 Inspiring Innovation/tutorialrank.com
CIS 115 Inspiring Innovation/tutorialrank.com
 
05 control structures 2
05 control structures 205 control structures 2
05 control structures 2
 
06
0606
06
 
CIS 115 Effective Communication - tutorialrank.com
CIS 115  Effective Communication - tutorialrank.comCIS 115  Effective Communication - tutorialrank.com
CIS 115 Effective Communication - tutorialrank.com
 
COMP 122 Entire Course NEW
COMP 122 Entire Course NEWCOMP 122 Entire Course NEW
COMP 122 Entire Course NEW
 
CPSC 120Spring 2014Lab 5Name _____________________.docx
CPSC 120Spring 2014Lab 5Name _____________________.docxCPSC 120Spring 2014Lab 5Name _____________________.docx
CPSC 120Spring 2014Lab 5Name _____________________.docx
 
Cpph exam a_2019_s1
Cpph exam a_2019_s1Cpph exam a_2019_s1
Cpph exam a_2019_s1
 
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
 
Percentage and its applications /COMMERCIAL MATHEMATICS
Percentage and its applications /COMMERCIAL MATHEMATICSPercentage and its applications /COMMERCIAL MATHEMATICS
Percentage and its applications /COMMERCIAL MATHEMATICS
 
Mat 540 week 11 final exam newly taken 2016
Mat 540 week 11 final exam newly taken 2016Mat 540 week 11 final exam newly taken 2016
Mat 540 week 11 final exam newly taken 2016
 
Mat 540 week 11 final exam newly taken 2016
Mat 540 week 11 final exam newly taken 2016Mat 540 week 11 final exam newly taken 2016
Mat 540 week 11 final exam newly taken 2016
 
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab new
 
MAT 540 Entire Course NEW
MAT 540 Entire Course NEWMAT 540 Entire Course NEW
MAT 540 Entire Course NEW
 
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab new
 
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab new
 
Sample Program file class 11.pdf
Sample Program file class 11.pdfSample Program file class 11.pdf
Sample Program file class 11.pdf
 
As mentioned earlier, the mid-term will have conceptual and quanti.docx
As mentioned earlier, the mid-term will have conceptual and quanti.docxAs mentioned earlier, the mid-term will have conceptual and quanti.docx
As mentioned earlier, the mid-term will have conceptual and quanti.docx
 
Cis 115 Education Organization -- snaptutorial.com
Cis 115   Education Organization -- snaptutorial.comCis 115   Education Organization -- snaptutorial.com
Cis 115 Education Organization -- snaptutorial.com
 
Csphtp1 05
Csphtp1 05Csphtp1 05
Csphtp1 05
 
ECO 520 Case Study One Production and Cost Guidelines and Rubric.docx
ECO 520 Case Study One Production and Cost Guidelines and Rubric.docxECO 520 Case Study One Production and Cost Guidelines and Rubric.docx
ECO 520 Case Study One Production and Cost Guidelines and Rubric.docx
 

More from Elsayed Hemayed

14 cie552 camera_calibration
14 cie552 camera_calibration14 cie552 camera_calibration
14 cie552 camera_calibrationElsayed Hemayed
 
12 cie552 object_recognition
12 cie552 object_recognition12 cie552 object_recognition
12 cie552 object_recognitionElsayed Hemayed
 
11 cie552 image_featuresii_sift
11 cie552 image_featuresii_sift11 cie552 image_featuresii_sift
11 cie552 image_featuresii_siftElsayed Hemayed
 
10 cie552 image_featuresii_corner
10 cie552 image_featuresii_corner10 cie552 image_featuresii_corner
10 cie552 image_featuresii_cornerElsayed Hemayed
 
09 cie552 image_featuresi
09 cie552 image_featuresi09 cie552 image_featuresi
09 cie552 image_featuresiElsayed Hemayed
 
08 cie552 image_segmentation
08 cie552 image_segmentation08 cie552 image_segmentation
08 cie552 image_segmentationElsayed Hemayed
 
07 cie552 image_mosaicing
07 cie552 image_mosaicing07 cie552 image_mosaicing
07 cie552 image_mosaicingElsayed Hemayed
 
06 cie552 image_manipulation
06 cie552 image_manipulation06 cie552 image_manipulation
06 cie552 image_manipulationElsayed Hemayed
 
05 cie552 image_enhancement
05 cie552 image_enhancement05 cie552 image_enhancement
05 cie552 image_enhancementElsayed Hemayed
 
04 cie552 image_filtering_frequency
04 cie552 image_filtering_frequency04 cie552 image_filtering_frequency
04 cie552 image_filtering_frequencyElsayed Hemayed
 
03 cie552 image_filtering_spatial
03 cie552 image_filtering_spatial03 cie552 image_filtering_spatial
03 cie552 image_filtering_spatialElsayed Hemayed
 
02 cie552 image_andcamera
02 cie552 image_andcamera02 cie552 image_andcamera
02 cie552 image_andcameraElsayed Hemayed
 
Csci101 lect04 advanced_selection
Csci101 lect04 advanced_selectionCsci101 lect04 advanced_selection
Csci101 lect04 advanced_selectionElsayed Hemayed
 
Csci101 lect10 algorithms_iii
Csci101 lect10 algorithms_iiiCsci101 lect10 algorithms_iii
Csci101 lect10 algorithms_iiiElsayed Hemayed
 
Csci101 lect09 vectorized_code
Csci101 lect09 vectorized_codeCsci101 lect09 vectorized_code
Csci101 lect09 vectorized_codeElsayed Hemayed
 
Csci101 lect08b matlab_programs
Csci101 lect08b matlab_programsCsci101 lect08b matlab_programs
Csci101 lect08b matlab_programsElsayed Hemayed
 
Csci101 lect08a matlab_programs
Csci101 lect08a matlab_programsCsci101 lect08a matlab_programs
Csci101 lect08a matlab_programsElsayed Hemayed
 
Csci101 lect06 advanced_looping
Csci101 lect06 advanced_loopingCsci101 lect06 advanced_looping
Csci101 lect06 advanced_loopingElsayed Hemayed
 
Csci101 lect05 formatted_output
Csci101 lect05 formatted_outputCsci101 lect05 formatted_output
Csci101 lect05 formatted_outputElsayed Hemayed
 

More from Elsayed Hemayed (20)

14 cie552 camera_calibration
14 cie552 camera_calibration14 cie552 camera_calibration
14 cie552 camera_calibration
 
12 cie552 object_recognition
12 cie552 object_recognition12 cie552 object_recognition
12 cie552 object_recognition
 
11 cie552 image_featuresii_sift
11 cie552 image_featuresii_sift11 cie552 image_featuresii_sift
11 cie552 image_featuresii_sift
 
10 cie552 image_featuresii_corner
10 cie552 image_featuresii_corner10 cie552 image_featuresii_corner
10 cie552 image_featuresii_corner
 
09 cie552 image_featuresi
09 cie552 image_featuresi09 cie552 image_featuresi
09 cie552 image_featuresi
 
08 cie552 image_segmentation
08 cie552 image_segmentation08 cie552 image_segmentation
08 cie552 image_segmentation
 
07 cie552 image_mosaicing
07 cie552 image_mosaicing07 cie552 image_mosaicing
07 cie552 image_mosaicing
 
06 cie552 image_manipulation
06 cie552 image_manipulation06 cie552 image_manipulation
06 cie552 image_manipulation
 
05 cie552 image_enhancement
05 cie552 image_enhancement05 cie552 image_enhancement
05 cie552 image_enhancement
 
04 cie552 image_filtering_frequency
04 cie552 image_filtering_frequency04 cie552 image_filtering_frequency
04 cie552 image_filtering_frequency
 
03 cie552 image_filtering_spatial
03 cie552 image_filtering_spatial03 cie552 image_filtering_spatial
03 cie552 image_filtering_spatial
 
02 cie552 image_andcamera
02 cie552 image_andcamera02 cie552 image_andcamera
02 cie552 image_andcamera
 
01 cie552 introduction
01 cie552 introduction01 cie552 introduction
01 cie552 introduction
 
Csci101 lect04 advanced_selection
Csci101 lect04 advanced_selectionCsci101 lect04 advanced_selection
Csci101 lect04 advanced_selection
 
Csci101 lect10 algorithms_iii
Csci101 lect10 algorithms_iiiCsci101 lect10 algorithms_iii
Csci101 lect10 algorithms_iii
 
Csci101 lect09 vectorized_code
Csci101 lect09 vectorized_codeCsci101 lect09 vectorized_code
Csci101 lect09 vectorized_code
 
Csci101 lect08b matlab_programs
Csci101 lect08b matlab_programsCsci101 lect08b matlab_programs
Csci101 lect08b matlab_programs
 
Csci101 lect08a matlab_programs
Csci101 lect08a matlab_programsCsci101 lect08a matlab_programs
Csci101 lect08a matlab_programs
 
Csci101 lect06 advanced_looping
Csci101 lect06 advanced_loopingCsci101 lect06 advanced_looping
Csci101 lect06 advanced_looping
 
Csci101 lect05 formatted_output
Csci101 lect05 formatted_outputCsci101 lect05 formatted_output
Csci101 lect05 formatted_output
 

Recently uploaded

Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 

Recently uploaded (20)

Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 

Csci101 lect07 algorithms_ii

  • 2. Overview • Multiplication table • Matrix multiplication • Sorting • Practice Problems
  • 3. Displaying the multiplication table for i=1:5 for j=1:5 fprintf(‘%8d’,i*j); end fprintf(‘n’); end 1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25
  • 4. Displaying a modified multiplication table for i=1:5 for j=i:5 fprintf(‘%8d’,i*j); end fprintf(‘n’); end 1 2 3 4 5 4 6 8 10 9 12 15 16 20 25
  • 5. Matrices Multiplication • Given matrix A [n][k] and matrix B[k][m] find their multiplication matrix C[n][m]. i j i a a j =x A [n][k] B[k][m] C[n][m]
  • 6. Matrices Multiplication for i =1 to n for j=1 to m C[i][j]=0 for a=1 to k C[i][j]=C[i][j]+A[i][a]*B[a][j] i j i a a j =x
  • 7. Sorting • Given a list of N integer values, sort them in an ascending order. for i=1 to N for j=i+1 to N If A[i]>A[j] swap A[i] and A[j] 5 4 2 1 6
  • 8. Array Sorting X=input(‘enter an array:’); for i=1:length(X) for j=i+1:length(X) if X(i)>X(j) t=X(i); X(i)=X(j); X(j)=t; end end end disp(X); 5 4 2 1 6 4 5 2 1 6 2 5 4 1 6 1 5 4 2 6 i=1 j= 2 3 4 5 1 5 4 2 6 enter an array: [5 4 2 1 6] Swapping values
  • 9. What is the output of this program? N=input(‘enter a number:’); for i=1:N for j=1:i fprintf(‘*’); end fprintf(‘n’); end Enter a number: 5 * ** *** **** *****
  • 11. Problem 1 Write a program that reads a number N from the user and prints an array containing the numbers (from 1 to N) that are multiples of 3 or 5 Also print their sum Input/Output Sample: Enter number: 10 The array that has multiples of 3 or 5 = 3 5 6 9 10 The sum of the multiples of 3 or 5 = 33
  • 12. Problem 2 N children at a party eat scoops of ice cream, write a matlab program to calculate how many scoops of ice cream do they eat altogether and who eats more, if the user enters a negative number, the program asks the user to re-enter the number. Input/Output Sample: Enter the number of children: 4 Enter number of scoops/ child: 1 Enter number of scoops/ child: -2 Error! Enter Positive Number: 4 Enter number of scoops/ child: 5 Enter number of scoops/ child: 3 Number of ice cream scoops all children ate: 13 Child ate more ice scoops: 3
  • 13. Problem 3 In train station, the price of ticket depends on the destination distance. There is a 20% discount for prices>160, 5% for prices<100 and 10% for any other prices. Write a matlab script to print final price for each station. The program can be executed 3 times. Input/Output Sample: Enter tickets price/station: [150 210 50 75 80 170] Discounted prices are: 135.00 168.00 47.50 71.25 76.00 136.00
  • 14. Problem 4 Write MATLAB code that reads an array from user and counts the number of elements that have values between 50 and 90 and calculates the max, and average of these numbers. If the array does not contain numbers in the required range the program should display “there are no numbers between 50, 90 in the array” Input/Output Sample: Enter your array of numbers: [2 3 55 88 65 57 99 78] Count of numbers between 50, 90 is: 5 Max is: 88 Avg is: 68.6
  • 15. Problem 5 A customer want to buy a TV at least price. Write a program to help him to know the average price of the TV and the store at which the least price. The program should ask the customer about the number of stores that have the required TV, and he should then enter the TV price at each store. Then the program should print the average price of TV in all stores, and where the least price. Input/Output Sample: Enter the number of stores have the required TV: 4 Enter the price of the TV in this store: 2800 Enter the price of the TV in this store: 2780 Enter the price of the TV in this store: 2930 Enter the price of the TV in this store: 2855 The average price of TV in all stores: 2841.25 The store have the least TV price: 2