Upload
others
View
0
Download
0
Embed Size (px)
Citation preview
Lecture 3Algorithms Part I
Calculating sum and average, Getting the maximum, Counting occurrences,
Finding matches
Overview
• Calculating sum & average of students’ grades
• Getting the maximum
• Counting number of zeroes
• Finding locations of zeroes
• Counting number of occurrences
• Finding the matched elements
One Dimension Arrays
X= [4 5 2 6 8]
length(X): number of elements
To access the array elements use for-loop.
for i=1:length(X) % use X(i)
end
4
5
2
6
8
X(1)
X(2)
X(3)
X(4)
X(5)
Calculate sum & average of students’ grades (Program and Testing)
X=input(‘enter student grades:’);sum=0;for i=1:length(X)
sum=sum+X(i);endavg = sum/length(X);disp(sum);disp(avg);
i X(i) sum
0
1 8 8
2 7 15
3 9 24
4 5 29
5 6 35
enter student grades: [8 7 9 5 6]
Getting the maximum (Problem & Algorithm)
• Given N numbers, find their maximum value
[3 5 -2 7 1] has a maximum value 7
Algorithm- Get N numbers- Assume max value = 0- Make a for loop from 1 to N
In every iteration, get one number xif x is greater than my max then replace my max
- Display my max
Getting the maximum (Program)
X=input(‘enter elements:’);max=0;for i=1:length(X)
if X(i)>maxmax=X(i);
endenddisp(max);
i X(i) max
0
1 3 3
2 5 5
3 -2 5
4 7 7
5 1 7
enter elements: [3 5 -2 7 1]
Getting the maximum and its index (Program)
X=input(‘enter elements:’);max=0;for i=1:length(X)
if X(i)>maxmax=X(i);maxi = i;
endenddisp(max);disp(maxi);
i X(i) max maxi
0
1 3 3 1
2 5 5 2
3 -2 5 2
4 7 7 4
5 1 7 4
enter elements: [3 5 -2 7 1]
Counting number of zeroes I (Problem)
• Given a list of values, count how many zeroes is found in the list.
• Example 1:
– Input: [6 0 12 0 12]
– Output: 2
• Example 2:
– Input: [6 5 12 14 10]
– Output: 0
Counting number of zeroes I(Algorithm)
- Get N elements
- Set count=0
- Make a for loop from 1 to N
In every iteration, get one number x
if x equal zero then increment count
- Print count
Counting number of zeroes I(Program)
X=input(‘enter elements:’);
count=0;
for i=1:length(X)
if X(i)==0
count=count+1;
end
end
disp(count);
i X(i) count
0
1 6 0
2 0 1
3 12 1
4 0 2
5 12 2
enter elements: [6 0 12 0 12]
Counting number of zeroes II (Problem)
• Given a list of values, count how many zeroes is found in the list. If not found then print “Not Found”
• Example 1: – Input: [6 0 12 0 12]
– Output: 2
• Example 2: – Input: [6 5 12 14 10]
– Output: Not Found
Counting number of zeroes II (Algorithm)
- Get N elements
- Set count=0
- Make a for loop from 1 to N
In every iteration, get one number x
if x equal zero then increment count
- if count >0 then print count
Otherwise print ‘Not Found’
Counting number of zeroes II(Program)
X=input(‘enter elements:’);count=0;for i=1:length(X)
if X(i)==0count=count+1;
endendIf count > 0
disp(count);else
disp(‘Not Found’);end
enter elements: [6 0 12 0 12]
i X(i) count
0
1 6 0
2 0 1
3 12 1
4 0 2
5 12 2
Counting number of zeroes II(Program)
X=input(‘enter elements:’);
count=0;
for i=1:length(X)
if X(i)==0
count=count+1;
end
end
If count > 0
disp(count);
else
disp(‘Not Found’);
end
i X(i) count
0
1 6 0
2 5 0
3 12 0
4 14 0
5 12 0
enter elements: [6 5 12 14 12]
Counting number of occurrence (Problem)
• Given a list of values and a key, count how many times the key is found in the list. If not found then print “Not Found”
• Example 1: – Input: [6 5 12 14 12] and key=12
– Output: 2
• Example 2: – Input: [6 5 12 14 10] and key=7
– Output: Not Found
Counting number of occurrence(Algorithm)
- Get the key
- Get N elements
- Set count=0
- Make a for loop from 1 to N
In every iteration, get one number x
if x equal key then increment count
- if count >0 then print countOtherwise print ‘Not Found’
Counting number of occurrence (Program)
Key=input(‘enter the key:’);X=input(‘enter elements:’);count=0;for i=1:length(X)
if X(i)==keycount=count+1;
endendIf count > 0
disp(count);else
disp(‘Not Found’);end
i X(i) count
0
1 6 0
2 5 0
3 12 1
4 14 1
5 12 2
enter the key: 12enter elements: [6 5 12 14 12]
Counting number of occurrence (Program)
Key=input(‘enter the key:’);X=input(‘enter elements:’);count=0;for i=1:length(X)
if X(i)==keycount=count+1;
endendIf count > 0
disp(count);else
disp(‘Not Found’);end
i X(i) count
0
1 6 0
2 5 0
3 12 0
4 14 0
5 12 0
enter the key: 7enter elements: [6 5 12 14 12]
Finding the matched elements (Problem)
• Given a list of values and a key, check if the key is found in the list and print the locations of these match otherwise, print Not Found.
• Example 1: – Input: [6 5 12 14 12] and key=12
– Output: 3
5
• Example 2: – Input: [6 5 12 14 12] and key=7
– Output: Not Found
Finding the matched elements (Algorithm)
- Get the key
- Get N elements
- Make a for loop from 1 to N
In every iteration, get one number x
if x equal key then display the iteration number
- If the key is not found then print “not Found”
Finding the matched elements (Program)
Key=input(‘enter the key:’);X=input(‘enter elements:’);count=0;for i=1:length(X)
if X(i)==keydisp(i);count=count+1;
endendif count==0
disp(‘Not Found’);end
i X(i) count
0
1 6 0
2 5 0
3 12 1
4 14 1
5 12 2
enter the key: 12enter elements: [6 5 12 14 12]35
Finding the matched elements (Program)
Key=input(‘enter the key:’);X=input(‘enter elements:’);count=0;for i=1:length(X)
if X(i)==keydisp(i);count=count+1;
endendif count==0
disp(‘Not Found’);end
i X(i) count
0
1 6 0
2 5 0
3 12 0
4 14 0
5 12 0
enter the key: 7enter elements: [6 5 12 14 12]Not Found
Thank You
Course Site: http://scholar.cu.edu.eg/?q=eldeib/classes/genn004-computers-engineers
Computers for Engineers – GENN004