0% found this document useful (0 votes)
15 views

GENN004 Lect4 Algorithms1

This document summarizes key algorithms for processing arrays: 1. Calculating the sum and average of array elements uses a for loop to iterate through the array, adding each element to a running sum and then dividing the sum by the array length to get the average. 2. Finding the maximum element uses a for loop to iterate through the array, tracking the largest element seen so far and updating it if a larger element is found. 3. Counting occurrences of a value (like zeros) uses a for loop to iterate through the array, incrementing a counter variable each time the target value is found. It reports the final counter value.

Uploaded by

Mahmoud shendy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

GENN004 Lect4 Algorithms1

This document summarizes key algorithms for processing arrays: 1. Calculating the sum and average of array elements uses a for loop to iterate through the array, adding each element to a running sum and then dividing the sum by the array length to get the average. 2. Finding the maximum element uses a for loop to iterate through the array, tracking the largest element seen so far and updating it if a larger element is found. 3. Counting occurrences of a value (like zeros) uses a for loop to iterate through the array, incrementing a counter variable each time the target value is found. It reports the final counter value.

Uploaded by

Mahmoud shendy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Lecture 4

Algorithms 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]
X(1) 4
length(X): number of elements X(2) 5
X(3) 2
To access the array elements use
for-loop.
X(4) 6
X(5) 8
for i=1:length(X)
% use X(i)
end
Calculate sum & average of students’
grades (Program and Testing)
X=input(‘enter student grades:’); i X(i) sum
sum=0; 0
1 8 8
for i=1:length(X)
2 7 15
sum=sum+X(i); 3 9 24
end 4 5 29
avg = sum/length(X); 5 6 35

disp(sum);
disp(avg);

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 x
if x is greater than my max then replace my
max
- Display my max
Getting the maximum (Program)
X=input(‘enter elements:’);
i X(i) max
max=0; 0
for i=1:length(X) 1 3 3
if X(i)>max 2 5 5

max=X(i); 3 -2 5
4 7 7
end 5 1 7
end
disp(max);

enter elements: [3 5 -2 7 1]
Getting the maximum and its index
(Program)
X=input(‘enter elements:’); i X(i) max maxi
max=0; 0
for i=1:length(X)
1 3 3 1
if X(i)>max
2 5 5 2
max=X(i);
maxi = i; 3 -2 5 2
end 4 7 7 4
end 5 1 7 4
disp(max);
disp(maxi);

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:’);
i X(i) count
count=0;
0
for i=1:length(X)
1 6 0
if X(i)==0 2 0 1
count=count+1; 3 12 1
end 4 0 2

end 5 12 2

disp(count);

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; i X(i) count
for i=1:length(X) 0
if X(i)==0 1 6 0
count=count+1;
2 0 1
end
3 12 1
end
If count > 0 4 0 2
disp(count); 5 12 2
else
disp(‘Not Found’);
end

enter elements: [6 0 12 0 12]


Counting number of zeroes II
(Program)
X=input(‘enter elements:’);
count=0;
i X(i) count
for i=1:length(X)
0
if X(i)==0
1 6 0
count=count+1;
2 5 0
end
3 12 0
end
4 14 0
If count > 0
5 12 0
disp(count);
else
disp(‘Not Found’);
end
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 count
Otherwise print ‘Not Found’
Counting number of occurrence
(Program)
Key=input(‘enter the key:’);
X=input(‘enter elements:’); i X(i) count
count=0;
0
for i=1:length(X)
if X(i)==key 1 6 0
count=count+1; 2 5 0
end 3 12 1
end
4 14 1
If count > 0
disp(count); 5 12 2
else
disp(‘Not Found’);
end

enter the key: 12


enter elements: [6 5 12 14 12]
Counting number of occurrence
(Program)
Key=input(‘enter the key:’);
X=input(‘enter elements:’);
count=0; i X(i) count
for i=1:length(X) 0
if X(i)==key 1 6 0
count=count+1;
2 5 0
end
end 3 12 0
If count > 0 4 14 0
disp(count); 5 12 0
else
disp(‘Not Found’);
end

enter the key: 7


enter 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; i X(i) count
for i=1:length(X) 0
if X(i)==key 1 6 0
disp(i);
2 5 0
count=count+1;
end 3 12 1
end 4 14 1
if count==0 5 12 2
disp(‘Not Found’);
end

enter the key: 12


enter elements: [6 5 12 14 12]
3
5
Finding the matched elements
(Program)
Key=input(‘enter the key:’);
X=input(‘enter elements:’); i X(i) count
count=0;
0
for i=1:length(X)
if X(i)==key 1 6 0
disp(i); 2 5 0
count=count+1; 3 12 0
end
4 14 0
end
if count==0 5 12 0
disp(‘Not Found’);
end
enter the key: 7
enter 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

You might also like