0% found this document useful (0 votes)
6 views14 pages

Practice 1 Exercises in Matlab

The document outlines various programming problems related to calculations in different scenarios, such as discounts, grades, salaries, and retirement classifications. It includes algorithms for computing final payments after discounts, determining student grades based on averages, and calculating overtime pay for workers. Additionally, it covers input validation and conditional logic to handle different cases in each problem.
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)
6 views14 pages

Practice 1 Exercises in Matlab

The document outlines various programming problems related to calculations in different scenarios, such as discounts, grades, salaries, and retirement classifications. It includes algorithms for computing final payments after discounts, determining student grades based on averages, and calculating overtime pay for workers. Additionally, it covers input validation and conditional logic to handle different cases in each problem.
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/ 14

Sequential Problems

A store offers a 15% discount on the total of the


purchase and a customer wants to know how much they will finally have to pay
for your purchase.

enter the purchase amount

fordescuento=0.85

totalToPay = totalOfPurchase * discount;


the total to pay is, %d
end

A student wants to know what their final grade will be in the Algorithms subject. That grade
is composed of the following percentages:
55% of the average of your three partial grades.
30% of the final exam grade.
15% of the final project grade.

P1=input('Enter Partial Grade 1: ');


P2=input('Enter partial grade 2: ');
Enter Partial 3 grade:
AverageP=(P1+P2+P3)/3
Exam grade:
Input('Work grade: ');
NF = (55/100) * AverageP + (30/100) * FinalExam + (15/100) * FinalWork;
Final Note:
disp(NF)

A teacher wants to know what percentage of men and what percentage of women there are.
in a group of students.
%program that calculates the percentage
N=input('Enter the total number of students: ');
H=input ('Enter the total number of students: ');
M=input ('Enter the total number of students: ');
T=H+M
if T == N
PM=M/N*100;
PH=H/N*100;
fprintf ('Percentage of men = %1.2f %% ',PH);
Percentage of women = %1.2f %%
else
The entered values are incorrect... Please try again
again
end

Simple Selective Problems

A man wants to know how much money is generated from the concept of
interest on the amount you have invested in the bank.
He will decide to reinvest the interests as long as these
exceed $7000, and in that case wants to know how much money he will have
finally in his/her account.

enter the amount to invest


enter the interest rate

interest = amount_to_invest * interest_rate / 100;

if interest > 7000


totalamounttoinvest+interest;
the final capital obtained is: %d
end
the interest generated is: %d
end

2. Fromdetermine if a student passes or fails a course, knowing that they will pass if their average
three grades are greater than or equal to 70; fail otherwise.

Grade1=input('Enter grade 1: ');


Enter grade 2:
Grade3=input('Enter grade 3: ');
Average=0;
if(Grade1+Grade2+Grade3)/3>=70
Approved
else
Disapproved
end

In a store, a 20% discount is given to customers whose purchase exceeds $1000.


What will be the amount that a person will pay for their purchase?

%program that calculates discounts for purchases over $1000


clc
m=input('Enter the total purchase amount: ');
ifm>1000
p = m - m * 20 / 100;
The total amount to be paid is = $ %1.2f
else
p=m;
fprintf('The total amount to pay is = $ %1.2f\n',p)
end

4. A worker needs to calculate his weekly salary, which is


obtained in the following way:
If you work 40 hours or less, you are paid $16 per hour.
If you work more than 40 hours, you are paid $16 for each one of the
first 40 hours and $20 for each extra hour.

enter the hours worked per day

weekly hours
the hours worked during the week are:
%d',weeklyhours);
if weekly hours > 40
16*40+20*(horassemanales-40);
fprintf('the weekly salary is: %d', weeklypay);
else if weekly hours < 40
weekly payment = 16 * weekly hours;
the weekly salary is: %d
end
end

5.A sick person, weighing 70 kg, is resting and wants to know


how many calories does your body consume during the entire time you perform a
same activity. The activities that are allowed to be performed are
only sleeping or sitting still. The data you have is that
while sleeping, it consumes 1.08 calories per minute and while sitting at
rest consumes 1.66 calories per minute.

Enter the minutes you sleep:


Enter the minutes you remain seated or at rest:
CT=1.08*MD+1.66*MS;
display('Calories consumed');
disp(CT);

6.Create an algorithm that calculates the total to be paid for the purchase of
shirts. If three or more shirts are purchased, a 20% discount applies.
about the total of the purchase and if there are less than three shirts a discount
of 10%

%amount to pay for the shirts


clc
n=input('Enter the number of shirts purchased: ');
c=input('Enter the cost of a shirt: ');
ifn>=3
pt=n*c;
payment=pt-pt*20/100;
The total amount to pay is = $ %1.2f
else
pt=n*c;
payment=pt-pt*10/100;
The total amount to be paid is = $ %1.2f
end

A company wants to make a purchase of several pieces of the same.


class to a spare parts factory. The company, depending on the amount
total of the purchase, will decide what to do to pay the manufacturer.
If the total purchase amount exceeds $500,000 the company will have
the ability to invest 55% of the amount of your own money
buy, borrow 30% from the bank and the rest will be paid
requesting a loan from the manufacturer.
If the total purchase amount does not exceed $500,000, the company
will have the capacity to invest 70% of their own money and the remaining
30% will be paid by requesting credit from the manufacturer.
The manufacturer charges 20% in interest on the
amount to be paid on credit.

enter the cost per piece


input('enter the number of pieces');

totalformat=costperpiece*numberofpieces;
if montototal > 500000;
cantidadinv = montototal * 0.55;
loan=totalamount*0.30;
montototal * 0.15;
elsemontototal<500000;
montototal * 0.70;
totalAmount * 0.30;
0
end
interest = 0.20 * credit;
end
the total amount is:%d
the money that the company invested was: %d
The loan requested at the bank was: %d
the credit to the manufacturer is:%d
the interest generated from the credit is: %d

Compound Selective Problems

1. Determine the amount of money that a worker will receive by concept


of the overtime worked in a company, knowing that when the
working hours exceed 40, the rest are considered overtime and
these are paid at double the normal hourly rate when they do not exceed 8; if the
overtime hours exceeding 8 are paid at double the rate of the first 8
they pay the normal hours and the rest at triple.

HP=input('Enter the payment value for 1 hour: ');


Enter the number of hours worked:
if HT > 40 & HT <= 48
HT-40
PT=40*HP+E1*2*HP;
elseifHT>48
E2=HT-48;
E3=HT-E2-40;
(40*HP)+(E2*3*HP)+(E3*2*HP)
elseif HT <= 40
PT=HT*HP;
end
disp(PT)

2. Calculate the profit that a worker receives in the annual profit sharing if this is assigned to them.
as a percentage of their monthly salary that depends on their seniority in the company according to
with the following table:
Time Utility
Less than 1 year 5% of the salary
1 year or more and less than 2 years 7% of the salary
2 years or more and less than 5 years 10% of the salary
5 years or more and less than 10 years 15% of the salary
10 years or more 20% of the salary

annual profit received by a worker


clc
s=input('Enter the corresponding monthly salary $: ');
t=input('Enter the time in years working: ');
ifs<=0 | t<=0
The entered data is incorrect.
The values of time and salary must be greater than 0;
else
ift>0 & t<1
u = s * 5 / 100;
The corresponding profit is = $ %1.2f
else if t >= 1 & t < 2
u = s * 7 / 100;
The corresponding profit is = $ %1.2f
else if t >= 2 & t < 5
u=s*10/100;
The corresponding profit is = $ %1.2f
else if t >= 5 and t < 10
u=s*15/100;
The corresponding profit is = $ %1.2f
else if t >= 10
u equals s times 20 divided by 100;
fprintf('The corresponding utility is = $ %1.2f\n',u);
end
end

3. In a discount store, a promotion is being carried out in which


a discount is applied to the total purchase value according to the color of
the little ball that the customer takes out when paying at the checkout. If the ball is from
no discount will be applied to white, if it is green a discount will be made
10% discount, if it is yellow a 25%, if it is blue a 50% and if it is
red at 100%. Determine the final amount that the customer must pay
for your purchase. It is known that there are only little balls of the colors
mentioned.

input('Enter the color of the ball')


input('enter the total purchase')
blanca=1;
verde=2;
azul=3;

ifcolordebola==1;
moneytopay=totalpurchase-0;
else
ifcolordebola==2;
totaldecompra - totaldecompra * 0.10;
else
ifcolordebola==3;
totalpurchase - totalpurchase * 0.25;
else
amounttodipay = totalpurchase - totalpurchase;
end
end
end

the total to pay is: %d

4. The IMSS needs to classify the individuals who will retire in the
year 1997. There are three types of retirements: by age, by
youth seniority and adult seniority. The people assigned
To retire by age, they must be 60 years or older and have a seniority.
in their employment of less than 25 years. The people assigned to the
young retirement due to seniority must be under 60 years old and a
seniority in their employment of 25 years or more. The people assigned
to retirement for older tenure must be 60 years old or more and
a seniority in their job of 25 years or more.
Determine what type of retirement a person will be assigned to.

Enter your age:


Enter years of work:
if Age >= 60 & Employment < 25
Retired by Age
if age < 60 & employment >= 25
Retired for Seniority Young
elseif age >= 60 & employment >= 25
Retirement due to Long Service
else
You do not belong to a retirement.
end

Cycles

Calculate the average of a student who has 7 grades


the subject of Structured Algorithm Design

Program that calculates the average of 7 grades


clc
N=7;
sum=0;
for(i=1:N);
data = input('Enter grade= ');
sum = sum + data;
p=suma/i;
end
The average of the grades is = %1.0f

2. Read 10 numbers and print only the positive numbers

a = input('please enter your number: ');


b = input('Please enter your number: ');
c= input('please enter your number: ');
d = input('please enter your number: ');
e = input('please enter your number: ');
f = input('please enter your number: ');
g = input('enter your number: ');
h= input('please enter your number: ');
i = input('please enter your number: ');
j = input('please enter your number: ');

n=0;
p=0;
[a b c d e f g h i j]
ifm>0
p = p + 1;
else
n=n+1;
end
end
positive values %d

3. Read 20 numbers and print how many there are positives, how many
negatives and how many neutrals.
a = input('please enter your number: ');
b= input('enter your number: ');
c= input('enter your number: ');
d = input('enter your number: ');
e= input('enter your number: ');
f = input('enter your number: ');
g = input('enter your number: ');
h= input('enter your number: ');
i= input('enter your number: ');
j = input('please enter your number: ');
k= input('enter your number: ');
l = input('enter your number: ');
m= input('enter your number: ');
n = input('enter your number: ');
o = input('enter your number: ');
p = input('enter your number: ');
q= input('enter your number: ');
r = input('enter your number: ');
s= input('enter your number: ');
t= input('enter your number: ');
0
negativo=0;
positivo=0;
forx = [a b c d e f g h i j k l m n o p q r s t]
if x > 0
positive = positive + 1;
elseif x < 0
negative=negative+1;
elsex=0;
neutro=neutro+1;
end
end
fprintf('positive values %d\n',positive)
negative values %d
fprintf('neutral values %d\n', neutro)

4. Suppose you have a set of grades from a group of


40 students. Create an algorithm to calculate the grade.
average and the lowest grade of the whole group.

program that calculates the average grade and the lowest one of the group
clc
Enter the grades as a vector:
A=mean(C);
B=min(C);
The minimum grade is = %1.2f
The average grade is = %1.2f

5.% Simulate the behavior of a digital clock, printing the


hours, minutes, and seconds of a day from 00:00:00 hours until
11:59:59 PM

forH=1:23
forM=1:59
for S=1:59
end
end
end
%d hours %d min %d sec.

Problems

The pressure, volume, and temperature of a mass of air


related by the formula:

%Mass = (pressure * volume) / (0.37 * (temperature + 460))

Calculate the average air mass of the tires of n vehicles


that are in repair in a wheel alignment and balancing service. The
vehicles can be motorcycles or cars.

clc
enter the value of the pressure
enter the temperature value
input('enter the value of the volume');

formasa=pressure*volume/(0.37*(temperature+460));
the value of the mass is %d
end

2. Determine the weekly amount of money that each one will receive.
The n workers of a company. It is known that when the hours that
I work as a laborer exceeding 40, the rest becomes overtime hours.
overtime that is paid double the normal hourly rate, when it does not exceed
from 8; when overtime exceeds 8, the first ones are paid
8 times double what is paid for a normal hour and the rest to the
triple.

HP=input('Enter the payment value for 1 hour: ');


HT=input('Enter the number of hours worked: ');
ifHT<=168
if HT > 40 && HT <= 48
E1=HT-40;
PT=40*HP+E1*2*HP;
elseif HT > 48
E2=HT-48;
E3=HT-E2-40;
(40*HP)+(E2*3*HP)+(E3*2*HP);
elseif HT <= 40
HT * HP;
end
disp(PT)
else
I work more than 1 week
end

3. On a farm, some information is required to determine the selling price for each.
kilo of egg. It is important to determine the average quality of the hens that are present in the
farm. The quality of each hen is obtained according to the formula:
Quality = weight of the hen * height of the hen
Number of eggs laid
Finally, to set the price of a kilogram of eggs, the following table is used as a basis:
TOTAL QUALITY PRICE PRICE PER KILO OF EGG
Greater than or equal to 15 1.2 * average quality
Greater than 8 and less than 15 1.00 * average quality
Less than or equal to 8 0.80 * average quality

%program that indicates the price according to the quality of the hen
clc
w=input('Enter the weight of the hen (kg): ');
h=input('Enter the height of the chicken (cm): ');
n=input('Enter the number of eggs laid:');
c = w * h / n;
ifw<=0 | h<=0 | n<=0
The entered values are incorrect...Please try again.
again
else
ifc>=15
p=1.2*c;
The price per kilo of eggs is =$ %1.2f
elseif c > 8 and c < 15
p=1.0*c;
The price per kilo of eggs is = $ %1.2f
elseifc<=8
p=0.8*c;
The price per kilo of eggs is =$ %1.2f
end
end

4. A survey is being conducted in the Chamber of Deputies with everyone.


the members in order to determine what percentage of the n
lawmakers are in favor of the Free Trade Agreement, which
percentage is against and what percentage abstains from
to opine.

clc
%case for 10 representatives
a = input('enter your number:');
b = input('enter your number:');
c = input('enter your number:');
d = input('enter your number:');
e = input('enter your number:');
f = input('enter your number:');
g = input('enter your number:');
h = input('enter your number:');
i = input('enter your number:');
j = input('enter your number:');

f=1;
c=-1;
a=0;
form = [a b c d e f g h i j]

ifm>0
f = f + 1;
else
ifm < 0
c=c+1;
else
a=a+1;
end
end
end
The percentage of those in favor is %d
fprintf('the percentage of those who are against %d\n',c*100/10)
the percentage of those who abstain is %d

A person who goes shopping at the store 'Enano, S.A.' decides


keep track of what you are buying, to know the
amount of money you will have to pay upon reaching the checkout. The
the store has a promotion of 20% discount on those
items whose label is red. Determine the quantity of
money that this person will have to pay.

Objects=input('Cost of the purchased objects: ');


Objects with red labels:
ifTags==0
Expenses
elseifTags>0
Expenses = Items - (20/100) * Labels;
end
display(Expenses);

6. A census taker collects certain data by conducting surveys for the


latest National Census of Population and Housing. Wishes to obtain from
all the people I managed to survey in a day, that
percentage has primary, secondary, college education
technique, professional studies and postgraduate studies.

percentage_of_education_level_of_the_surveyed_people
clc
n=input('Enter the total number of surveyed people: ');
Enter the number of people with primary education:
');
s=input('Enter the number of people with education at the level
secondary
t=input('Enter the number of people with technical degrees: ');
u=input('Enter the number of people with professional degrees: ');
ps=input('Enter the number of people with postgraduate studies: ');
ifn==p+s+t+u+ps
pp = p/n * 100;
ps=s/n*100;
pt=t/n*100;
pu = u/n * 100;
pps=ps/n*100;
RESULTS :
Primary education studies = %1.2f %%
fprintf('Secondary education studies = %1.2f %% \n', ps);
Technical careers = %1.2f %%
Professional studies= %1.2f %%
Postgraduate studies = %1.2f %%
else
Error entering the data... Please try again.
end

A polling station chief wants to determine how many people from each
one of the sections that make up your area attends on the day of the
Voting. The sections are: north, south, and center. Also
wants to determine which is the section with the highest number of
voters.
enter the number of people in the south section
northern_section=input('enter the number of people in the section
north
sectionCenter = input('enter the number of people in the section
center

if south_section > north_section && south_section > center_section


The southern section is the one with the most voters.
else
if northsection > southsection && northsection > centersection
the northern section is the one with the most voters
else
The central section is the one with the most voters.
end
end

A copying business has a daily production limit of 10.


000 copies if the printing type is offset and 50,000 if the
it is standard. If there is a request from an employee, they have to
check that the pending copies up to this moment and the copies
requested do not exceed the production limit. If the limit of
production exceeding the requested work could not be
accepted. The employee needs to maintain good control of the copies
requested so far to decide quickly if the
jobs requested on the day must be accepted or not.

Pending offset copies:


Requested offset copies:
Pending standard copies:
Copies requested standard:
ifCPO<=10000
10000-CPO
ifCSO<=S1
CTO = CPO + CSO;
Accept, Number of Offset Copies:
disp(CTO);
else
E1=CPO+CSO-10000;
Do not accept, Excess of offset copies:
disp(E1);
end
else
CPO-10000
Exceeds offset copies
disp(EE1)
end
ifCPE<=50000
S2=50000-CPE;
ifCSE<=S2
CTE=CPE+CSE;
Accept, Number of standard copies:
disp(CTE);
else
E2=CPE+CSE-50000;
Do not accept, Excess of standard copies:
display(E2);
end
else
CPE-50000
Exceeds standard copies
disp(EE2)
end

9. Calculate the following sum:


100 + 98 + 96 + 94 + ... + 0 in this order

program to find the sum of the even integers of


4950
clc
n=100;
sum = 0;
for X = n: -2: 0
sum = sum + X;
X=X-2;
end
The sum is:
disp(sum)

The sum is:


2550

10. Read 50 ratings from a group of students. Calculate and


write the percentage of failures. Considering that the
The minimum passing grade is 70.

a = input('please enter your number: ');


b= input('enter your number: ');
c= input('enter your number: ');
d = input('Enter your number: ');
e= input('enter your number: ');
f = input('enter your number: ');
g = input('Enter your number: ');
h = input('please enter your number: ');
i = input('enter your number: ');
j = input('enter your number: ');
k = input('introduce your number: ');
l = input('enter your number: ');
m= input('enter your number: ');
n = input('enter your number: ');
o = input('enter your number: ');
p = input('enter your number: ');
q= input('enter your number: ');
r = input('enter your number: ');
s = input('enter your number: ');
t = input('enter your number: ');
u = input('enter your number: ');
v= input('enter your number: ');
w = input('enter your number: ');
x= input('enter your number: ');
y= input('please enter your number: ');
z= input('enter your number: ');
A = input('enter your number: ');
B = input('enter your number: ');
C= input('enter your number: ');
D = input('please enter your number: ');
E = input('please enter your number: ');
F = input('enter your number: ');
G = input('enter your number: ');
H= input('enter your number: ');
I = input('enter your number: ');
J = input('enter your number: ');
K= input('please enter your number: ');
L= input('enter your number: ');
M = input('enter your number: ');
N= input('please enter your number: ');
O= input('enter your number: ');
P= input('enter your number: ');
Q= input('enter your number: ');
R = input('please enter your number: ');
S= input('enter your number: ');
T= input('enter your number: ');
U= input('enter your number:');
V= input('enter your number: ');
W = input('please enter your number: ');
X= input('enter your number: ');

aprobado=0;
reprobado=0;
The provided text does not contain a translatable phrase.
F G H I J K L M N O P Q R S T U V W X
if Y < 70
failed = failed + 1;
else Y>70
approved = approved + 1;

end
end
fprintf('the percentage of failures is: %d', failed*100/50);

11. Read for each student of Structured Algorithm Design their


control number and its grade in each of the 5 units
of the subject. In the end, write the student's control number
who achieved the highest average. Assume that the students have
different averages.

n=input('Enter number of students: ');


b=input('Number of units: ');

c=[];
fore=1:n
Enter the student code:
v=[];
c=[c l];
for j = 1 to b
m=input('Enter the grade (from 0 to 20): ');
v=[v m];
y=mean(v);
end
r=[r y];
end
[x,p]=max(r);
u=c(p);
display('The average value is')
display(x)
The student with the best average is
display(u)
12. The teacher of a subject wants to know the number of his students.
that do not have the right to the leveling exam. Design an algorithm
that reads the grades obtained in the 5 units by each one
of the 40 students and write down the number of them who do not have the right
to the leveling exam.

program that calculates the average grade and the lowest one of the group
clc
The columns represent each student;
it would be 40
disp('The rows represent the grades'); %for the case it would be 5
rows(grades)
A=input('Enter matrix: ');
B=mean(A);
N=B(B<=10);
L=length(N);
The students who need to take the leveling exam are =
%1.0f

You might also like