GENN004 Lect6 Practice
GENN004 Lect6 Practice
Practice 1
Overview
• Counting/Removing duplicate elements
• Multiplication table
• Matrix multiplication
• Sorting
Displaying the multiplication table
for i=1:5
for j=1:5
fprintf(‘%8d’,i*j);
end
fprintf(‘\n’);
1 2 3 4 5
end 2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Displaying a modified multiplication table
for i=1:5
for j=i:5
fprintf(‘%8d’,i*j);
end
fprintf(‘\n’);
1 2 3 4 5
end 4 6 8 10
9 12 15
16 20
25
Array Sorting
• Given a list of N integer values, sort them in an
ascending order.
5
for i=1 to N 4
for j=i+1 to N 2
If A[i]>A[j] 1
6
swap A[i] and A[j]
Array Sorting
X=input(‘enter an array:’); i=1
for i=1:length(X) j= 2 3 4 5
for j=i+1:length(X)
5 4 2 1 1
if X(i)>X(j)
t=X(i); 4 5 5 5 5
X(i)=X(j); 2 2 4 4 4
X(j)=t; 1 1 1 2 2
end
6 6 6 6 6
end
end Swapping values
disp(X);
enter an array: [5 4 2 1 6]
What is the output of this program?
N=input(‘enter a number:’);
for i=1:N
for j=1:i
fprintf(‘*’); Enter a number: 5
end *
fprintf(‘\n’); **
***
end ****
*****
Problem 1
Write a program that reads a number N from the
user and prints an array containing the numbers
(from 1 to N) that are multiples of 3 or 5
Also prints their sum
Input/Output Sample:
Enter number: 10
The array that has multiples of 3 or 5 = 3 5 6 9 10
The sum of the multiples of 3 or 5 = 33
clc; clear;
n=input('Enter number:');
k=1;
s=0;
for i=1:n
if rem(i,3) ==0 ||rem(i,5) == 0
z(k)=i;
k=k+1;
s=s+i;
end
end
fprintf('The array that has multiples of 3 or 5 = ');
disp(z)
fprintf('The sum of the multiples of 3 or 5 = %d\n',s);
Problem 2
N children at a party eat scoops of ice cream, write a program
to calculate how many scoops of ice cream do they eat
altogether and who eats more, if the user enters a negative
number, the program asks the user to re-enter the number.
Input/Output Sample:
Enter the number of children: 4
Enter number of scoops/ child: 1
Enter number of scoops/ child: -2
Error! Enter Positive Number: 4
Enter number of scoops/ child: 5
Enter number of scoops/ child: 3
Number of ice cream scoops all children ate: 13
Childe ate more ice scoops: 3
clc; clear;
n=input('Enter the number of children:');
s=0; max=0;
for i=1:n
ice=input('Enter number of scoops/ child:');
while ice<0
ice=input('Error! Enter Positive Number:');
end
s=s+ice;
if ice>max
max=ice;
child=i;
end
end
fprintf('Number of ice cream scoops all children ate:%d\n',s);
fprintf('Child ate more ice scoops: %d\n',child);
Problem 3
In train station, the price of ticket depends on the destination
distance. There is a 20% discount for prices>160, 5% for
prices<100 and 10% for any other prices.
Input/Output Sample:
Input/Output Sample:
Enter your array of numbers: [2 3 55 88 65 57 99 78]
Count of numbers between 50, 90 is: 5
Max is: 88
Avg is: 68.6
clc; clear;
n=input('Enter your array of numbers: ');
s=0; c=0; max=0;
for i=1:length(n)
if n(i)>50 && n(i)<90
s=s+n(i);
c=c+1;
if n(i)>max
max=n(i);
end
end
end
if c==0
disp('there are no numbers between 50, 90 in the array');
else
fprintf('Count of numbers between 50, 90 is:%d\n',c);
fprintf('Max is: %d\n',max);
fprintf('Avg is: %d\n',s/c);
end
Thank You
Course Site:
http://scholar.cu.edu.eg/?q=eldeib/classes/genn004-computers-engineers