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

Csci101 lect08b matlab_programs

  • 1.
  • 2.
    Outline • Modularity • BestPractice • Examples
  • 3.
    3 Modularity • How doyou 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 howthe 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 • Amodule 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 programthat 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 forClean 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