GENN004 Lect8 Algorithms2
GENN004 Lect8 Algorithms2
Algorithms Part II
Outline
• Modularity
• Best Practice
• Examples
Modularity
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 = %d\n,S);
Example 2 - removeDuplicate
function Y = removeDuplicate(X)
% copy non duplicate elements from X to Y
>>DisplayTime(5,10);
05:10
function DisplayTimeArray(t)
for i=1:length(t)
fprintf(‘%02d:%02d\n’,t(i,1),t(i,2));
end
end