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

GENN004 Lect6 Practice

This document discusses various MATLAB programming concepts including: - Counting and removing duplicate elements in arrays - Creating and displaying multiplication tables - Sorting arrays in ascending order - Calculating discounts on ticket prices based on different price ranges - Counting elements between a given range and calculating max and average It also provides sample code and inputs/outputs for problems involving these concepts, such as finding multiples of 3 or 5 in a range, calculating total ice cream scoops eaten by children, and analyzing arrays.

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)
20 views

GENN004 Lect6 Practice

This document discusses various MATLAB programming concepts including: - Counting and removing duplicate elements in arrays - Creating and displaying multiplication tables - Sorting arrays in ascending order - Calculating discounts on ticket prices based on different price ranges - Counting elements between a given range and calculating max and average It also provides sample code and inputs/outputs for problems involving these concepts, such as finding multiples of 3 or 5 in a range, calculating total ice cream scoops eaten by children, and analyzing arrays.

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/ 16

Lecture 6

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.

Write a program to print final price for each station.

Input/Output Sample:

Enter tickets price/station: [150 210 50 75 80 170]

Discounted prices are: 135.00 159.00 47.50 71.25 76.00 136.00


clc; clear;
n=input('Enter tickets price/station:');
fprintf('Discounted prices are: ');
for i=1:length(n)
if n(i)>160
n(i)=0.8*n(i);
elseif n(i)<100
n(i)=0.95*n(i);
else n(i)=0.9*n(i);
end
fprintf(‘%10.2f‘,n(i));
end
fprintf(‘\n');
Problem 4
Write a program that reads an array from user and counts
the number of elements that have values between 50 and
90 and calculates the max, and average of these numbers.
If the array does not contain numbers in the required
range the program should display “there are no numbers
between 50, 90 in the array”

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

Computers for Engineers – GENN004

You might also like