SlideShare a Scribd company logo
CSCI 101
Modular Programming
Outline
• Modularity
• Best Practice
• Examples
3
Modularity
• How do you solve a big/complex problem?
• Divide it into small tasks and solve each task.
Then combine these solutions.
Divide and Conquer
Structure Chart
Shows how the program separated into
tasks and which tasks reference other
tasks.
NOTE: It does NOT indicate the
sequence of steps in the program!
Best Practice
• A module should be dedicated to one task
– Flexibility is provided by input/output parameters
• General purpose modules need
– Description of input/output parameters
– Meaningful error messages so that user
understands the problem
• Organization takes experience
– Goal is not to maximize the number of m-files
– Organization will evolve on complex projects
Example 1 - stat
function [mean, stdev] = stat(x)
% Mean and Standard deviation of an array
% Given the input argument array, this function calculates
% the mean (first output argument) and
% the standard deviation (second argument)
% of the array
% developed by XYZ on April 30, 2014
n=length(x);
mean=sumArray(x)/n;
stdev = sqrt(ssd(x,mean)/n);
end
stat
sumArray ssd
Example 1 - sumArray
function s = sumArray(x)
% summation of all array x elements
% Given the input argument array, this function calculates
% the sum of the array elements
s=0;
for i = 1:length(x)
s=s+x(i);
end
end
Example 1 - ssd
function SSD = ssd(x,k)
% Sum of squared difference
% Given the input argument array x and scalar k this function
% calculates the sum of the squared difference between
% each elements of x and k
SSD=0;
for i = 1:length(x)
SSD=SSD+(x(i)-k)^2;
end
end
Example 2 - main
• Read an array from the user and calculates the
sum of non-duplicate items
X=input(‘enter array:’);
Y=removeDuplicate(X);
S=sumArray(Y);
‘ or S=sumArray(removeDuplicate(X));
fprintf(‘Sum of non-duplicates = %dn,S);
Example 2 - removeDuplicate
function Y = removeDuplicate(X)
% copy non duplicate elements from X to Y
k=1; % index for the array Y
for i=1:length(X)
c=0;
for j=i+1:length(X)
if X(i) == X(j) % compare each element and the next one
c=c+1;
break; % exit inner most loop
end
end
if c==0 % if no duplicate, then store it in Y
Y(k)=X(i);
k=k+1; % increment the index to point to next location in Y
end
end
disp(Y);
Exercise 3
Write program that takes the student grade (out of 100) in Math,
Science, and English and prints ‘Excellent’ if greater than or equal 90%,
‘Very Good’ if greater than or equal 80% and smaller than 90%, ‘Good’
if greater than or equal 70 and smaller than 80%, ‘Fair’ if greater than
or equal 60% and smaller than 70%,’Fail’ if lower than 60%.
Sample Input/Output:
Enter Math grade from 0 to 100:94
Enter Science grade from 0 to 100:87
Enter English grade from 0 to 100:77
You got Excellent in Math
You got Very Good in Science
You got Good in English
Exercise 3 - Solution
Write program that takes the student grade (out of 100) in Math, Science, and English
m=correctInput('Enter math grade from 0 to 100:',0,100);
s=correctInput('Enter science grade from 0 to 100:',0,100);
e=correctInput('Enter english grade from 0 to 100:',0,10);
and prints ‘Excellent’ if greater than or equal 90%, ‘Very Good’ if greater than or equal
80% and smaller than 90%, ‘Good’ if greater than or equal 70 and smaller than 80%,
‘Fair’ if greater than or equal 60% and smaller than 70%,’Fail’ if lower than 60%.
mr=grade2rank(m); % A function to convert grade numeric to alphanumeric
sr=grade2rank(s);
er=grade2rank(e);
fprintf('You got %s in mathn',mr);
fprintf('You got %s in sciencen',sr);
fprintf('You got %s in englishn',er);
Use Functions for Clean and Organized
Code (Modular Programs)
m=correctInput('Enter math grade from 0 to 100:',0,100);
s=correctInput('Enter science grade from 0 to 100:',0,100);
e=correctInput('Enter english grade from 0 to 100:',0,100);
mr=grade2rank(m);
sr=grade2rank(s);
er=grade2rank(e);
fprintf('You got %s in mathn',mr);
fprintf('You got %s in sciencen',sr);
fprintf('You got %s in englishn',er);
Main
program
correctInput grade2rank
Example 3 - grade2rank
function r=grade2rank(x)
% calculate the corresponding rank of the grade x
if x>=90
r='Ecellent';
elseif x>=80
r='Very Good';
elseif x>=70
r='Good';
elseif x>=60
r='Fair';
else
r='Fail';
end
end
See MATLAB 
function – DisplayTime, DisplayTimeArray
function DisplayTime(h,m)
fprintf(‘%02d:%02dn’,h,m);
end
>>DisplayTime(5,10);
05:10
function DisplayTimeArray(t)
for i=1:length(t)
fprintf(‘%02d:%02dn’,t(i,1),t(i,2));
end
end
>>DisplayTimeArray([5 10; 11 30]);
05:10
11:30

More Related Content

What's hot (20)

Clark S Final Project
Clark S Final ProjectClark S Final Project
Clark S Final Project
 
ML - Multiple Linear Regression
ML - Multiple Linear RegressionML - Multiple Linear Regression
ML - Multiple Linear Regression
 
Machine Learning - Simple Linear Regression
Machine Learning - Simple Linear RegressionMachine Learning - Simple Linear Regression
Machine Learning - Simple Linear Regression
 
Machine learning session4(linear regression)
Machine learning   session4(linear regression)Machine learning   session4(linear regression)
Machine learning session4(linear regression)
 
The fundamentals of regression
The fundamentals of regressionThe fundamentals of regression
The fundamentals of regression
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
Curve fitting
Curve fittingCurve fitting
Curve fitting
 
COM1407: Arrays
COM1407: ArraysCOM1407: Arrays
COM1407: Arrays
 
Machine learning Introduction
Machine learning IntroductionMachine learning Introduction
Machine learning Introduction
 
Monte carlo simulation
Monte carlo simulationMonte carlo simulation
Monte carlo simulation
 
Logistic regression in Machine Learning
Logistic regression in Machine LearningLogistic regression in Machine Learning
Logistic regression in Machine Learning
 
Derivative rules.docx
Derivative rules.docxDerivative rules.docx
Derivative rules.docx
 
NUMERICAL METHOD
NUMERICAL METHODNUMERICAL METHOD
NUMERICAL METHOD
 
VARIANCE
VARIANCEVARIANCE
VARIANCE
 
Functions lect
Functions lectFunctions lect
Functions lect
 
Dummy variables
Dummy variablesDummy variables
Dummy variables
 
Polynomials lecture
Polynomials lecturePolynomials lecture
Polynomials lecture
 
Multiple reg presentation
Multiple reg presentationMultiple reg presentation
Multiple reg presentation
 
Rational functions lecture
Rational functions lectureRational functions lecture
Rational functions lecture
 
January 15, 2015
January 15, 2015January 15, 2015
January 15, 2015
 

Similar to Csci101 lect08b matlab_programs

Similar to Csci101 lect08b matlab_programs (20)

Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
Csci101 lect08a matlab_programs
Csci101 lect08a matlab_programsCsci101 lect08a matlab_programs
Csci101 lect08a matlab_programs
 
CSE 103 Project Presentation.pptx
CSE 103 Project Presentation.pptxCSE 103 Project Presentation.pptx
CSE 103 Project Presentation.pptx
 
Fst ch3 notes
Fst ch3 notesFst ch3 notes
Fst ch3 notes
 
Data Analysis Assignment Help
Data Analysis Assignment Help Data Analysis Assignment Help
Data Analysis Assignment Help
 
Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]
 
Numerical analysis
Numerical analysisNumerical analysis
Numerical analysis
 
Final Design Project - Memo (with GUI)
Final Design Project - Memo (with GUI)Final Design Project - Memo (with GUI)
Final Design Project - Memo (with GUI)
 
Cs291 assignment solution
Cs291 assignment solutionCs291 assignment solution
Cs291 assignment solution
 
3. chapter ii
3. chapter ii3. chapter ii
3. chapter ii
 
The Basics of MATLAB
The Basics of MATLABThe Basics of MATLAB
The Basics of MATLAB
 
R Programming Intro
R Programming IntroR Programming Intro
R Programming Intro
 
NPTEL QUIZ.docx
NPTEL QUIZ.docxNPTEL QUIZ.docx
NPTEL QUIZ.docx
 
C Programming Example
C Programming Example C Programming Example
C Programming Example
 
3ml.pdf
3ml.pdf3ml.pdf
3ml.pdf
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
 
array
arrayarray
array
 
L5 array
L5 arrayL5 array
L5 array
 
introduction to c programming and C History.pptx
introduction to c programming and C History.pptxintroduction to c programming and C History.pptx
introduction to c programming and C History.pptx
 
bobok
bobokbobok
bobok
 

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 lect07 algorithms_ii
Csci101 lect07 algorithms_iiCsci101 lect07 algorithms_ii
Csci101 lect07 algorithms_iiElsayed 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
 
Csci101 lect03 algorithms_i
Csci101 lect03 algorithms_iCsci101 lect03 algorithms_i
Csci101 lect03 algorithms_iElsayed 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 lect07 algorithms_ii
Csci101 lect07 algorithms_iiCsci101 lect07 algorithms_ii
Csci101 lect07 algorithms_ii
 
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
 
Csci101 lect03 algorithms_i
Csci101 lect03 algorithms_iCsci101 lect03 algorithms_i
Csci101 lect03 algorithms_i
 

Recently uploaded

How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPCeline George
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfTamralipta Mahavidyalaya
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonSteve Thomason
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXMIRIAMSALINAS13
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleCeline George
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsCol Mukteshwar Prasad
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...Sayali Powar
 
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...Denish Jangid
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersPedroFerreira53928
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasiemaillard
 
[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online PresentationGDSCYCCE
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxJenilouCasareno
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxPavel ( NSTU)
 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxssuserbdd3e8
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasiemaillard
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfbu07226
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportAvinash Rai
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfVivekanand Anglo Vedic Academy
 

Recently uploaded (20)

Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
 
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
Operations Management - Book1.p  - Dr. Abdulfatah A. SalemOperations Management - Book1.p  - Dr. Abdulfatah A. Salem
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 

Csci101 lect08b matlab_programs

  • 2. Outline • Modularity • Best Practice • Examples
  • 3. 3 Modularity • How do you solve a big/complex problem? • Divide it into small tasks and solve each task. Then combine these solutions. Divide and Conquer
  • 4. Structure Chart Shows how the program separated into tasks and which tasks reference other tasks. NOTE: It does NOT indicate the sequence of steps in the program!
  • 5. Best Practice • A module should be dedicated to one task – Flexibility is provided by input/output parameters • General purpose modules need – Description of input/output parameters – Meaningful error messages so that user understands the problem • Organization takes experience – Goal is not to maximize the number of m-files – Organization will evolve on complex projects
  • 6. Example 1 - stat function [mean, stdev] = stat(x) % Mean and Standard deviation of an array % Given the input argument array, this function calculates % the mean (first output argument) and % the standard deviation (second argument) % of the array % developed by XYZ on April 30, 2014 n=length(x); mean=sumArray(x)/n; stdev = sqrt(ssd(x,mean)/n); end stat sumArray ssd
  • 7. Example 1 - sumArray function s = sumArray(x) % summation of all array x elements % Given the input argument array, this function calculates % the sum of the array elements s=0; for i = 1:length(x) s=s+x(i); end end
  • 8. Example 1 - ssd function SSD = ssd(x,k) % Sum of squared difference % Given the input argument array x and scalar k this function % calculates the sum of the squared difference between % each elements of x and k SSD=0; for i = 1:length(x) SSD=SSD+(x(i)-k)^2; end end
  • 9. Example 2 - main • Read an array from the user and calculates the sum of non-duplicate items X=input(‘enter array:’); Y=removeDuplicate(X); S=sumArray(Y); ‘ or S=sumArray(removeDuplicate(X)); fprintf(‘Sum of non-duplicates = %dn,S);
  • 10. Example 2 - removeDuplicate function Y = removeDuplicate(X) % copy non duplicate elements from X to Y k=1; % index for the array Y for i=1:length(X) c=0; for j=i+1:length(X) if X(i) == X(j) % compare each element and the next one c=c+1; break; % exit inner most loop end end if c==0 % if no duplicate, then store it in Y Y(k)=X(i); k=k+1; % increment the index to point to next location in Y end end disp(Y);
  • 11. Exercise 3 Write program that takes the student grade (out of 100) in Math, Science, and English and prints ‘Excellent’ if greater than or equal 90%, ‘Very Good’ if greater than or equal 80% and smaller than 90%, ‘Good’ if greater than or equal 70 and smaller than 80%, ‘Fair’ if greater than or equal 60% and smaller than 70%,’Fail’ if lower than 60%. Sample Input/Output: Enter Math grade from 0 to 100:94 Enter Science grade from 0 to 100:87 Enter English grade from 0 to 100:77 You got Excellent in Math You got Very Good in Science You got Good in English
  • 12. Exercise 3 - Solution Write program that takes the student grade (out of 100) in Math, Science, and English m=correctInput('Enter math grade from 0 to 100:',0,100); s=correctInput('Enter science grade from 0 to 100:',0,100); e=correctInput('Enter english grade from 0 to 100:',0,10); and prints ‘Excellent’ if greater than or equal 90%, ‘Very Good’ if greater than or equal 80% and smaller than 90%, ‘Good’ if greater than or equal 70 and smaller than 80%, ‘Fair’ if greater than or equal 60% and smaller than 70%,’Fail’ if lower than 60%. mr=grade2rank(m); % A function to convert grade numeric to alphanumeric sr=grade2rank(s); er=grade2rank(e); fprintf('You got %s in mathn',mr); fprintf('You got %s in sciencen',sr); fprintf('You got %s in englishn',er);
  • 13. Use Functions for Clean and Organized Code (Modular Programs) m=correctInput('Enter math grade from 0 to 100:',0,100); s=correctInput('Enter science grade from 0 to 100:',0,100); e=correctInput('Enter english grade from 0 to 100:',0,100); mr=grade2rank(m); sr=grade2rank(s); er=grade2rank(e); fprintf('You got %s in mathn',mr); fprintf('You got %s in sciencen',sr); fprintf('You got %s in englishn',er); Main program correctInput grade2rank
  • 14. Example 3 - grade2rank function r=grade2rank(x) % calculate the corresponding rank of the grade x if x>=90 r='Ecellent'; elseif x>=80 r='Very Good'; elseif x>=70 r='Good'; elseif x>=60 r='Fair'; else r='Fail'; end end See MATLAB 
  • 15. function – DisplayTime, DisplayTimeArray function DisplayTime(h,m) fprintf(‘%02d:%02dn’,h,m); end >>DisplayTime(5,10); 05:10 function DisplayTimeArray(t) for i=1:length(t) fprintf(‘%02d:%02dn’,t(i,1),t(i,2)); end end >>DisplayTimeArray([5 10; 11 30]); 05:10 11:30