MATLAB Answers

The third elseif does not work. I cannot figure out what's wrong with my code.

2 views (last 30 days)
Glyzel Ann Madlangbayan
Glyzel Ann Madlangbayan on 20 Jul 2021
Answered: Steven Lord on 20 Jul 2021
slots_a1 = zeros(10,10);
slots_a2 = zeros(10,10);
slots_a3 = zeros(10,10);
slots_a4 = zeros(10,10);
slots_a5 = zeros(10,10);
slots_b1 = zeros(10,10);
slots_b2 = zeros(10,10);
slots_b3 = zeros(10,10);
slots_b4 = zeros(10,10);
slots_b5 = zeros(10,10);
index_a1 = 1;
index_a2 = 1;
index_a3 = 1;
index_a4 = 1;
index_a5 = 1;
index_b1 = 1;
index_b2 = 1;
index_b3 = 1;
index_b4 = 1;
index_b5 = 1;
sum_a1 = 0;
sum_a2 = 0;
sum_a3 = 0;
sum_a4 = 0;
sum_a5 = 0;
sum_b1 = 0;
sum_b2 = 0;
sum_b3 = 0;
sum_b4 = 0;
sum_b5 = 0;
index_ab11=index_a1+index_a2+index_a3+index_a4+index_a5+index_b1+index_b2+index_b3+index_b4+index_b5;
for c = 10:100
%personal information
name = input ('Enter Name: ', 's');
contactno = input ('Enter Contact Number: ');
address = input ('Enter Complete Address: ', 's');
fprintf(1, '\n');
disp ('Choose your preferred vaccination site');
disp ('1 for Aguinaldo Elementary School');
disp ('2 for Binakayan Hospital and Medical Center');
site = input("Enter 1 or 2: ");
if site < 1 || site > 2
continue
elseif site == 1 && (sum_a1 < 100 || sum_a2 < 100 || sum_a3 < 100 || sum_a4 < 100 || sum_a5 < 100)
disp ('Vaccination site: Aguinaldo Elementary School');
fprintf(1, '\n');
%choose vaccine
disp ('Choose your preferred vaccine');
disp ('Pfizer, AstraZeneca, Sinovac')
vaccine = input ('Enter name of vaccine (ex. AstraZeneca): ', 's');
fprintf(1, '\n');
%choose date
disp ('Choose on the following dates (MMDDYY)')
disp ('Aug0221 Aug0921 Aug1621 Aug2321 Aug3021')
date = input('Enter date of first dose (AugDDYY):', 's');
if date == 'Aug0221'
disp ('Date of first dose: Aug0221')
fprintf(1, '\n');
%assign a slot
slots_a1(index_a1)=1;
sum_a1=sum_a1+1;
%print reservation
disp ('COVID-19 Vaccine Reservation')
fprintf ('Name: %s\n', name);
fprintf ('Contact Number: %.f\n', contactno);
fprintf ('Address: %s\n', address);
disp('Vaccination Site: Aguinaldo Elementary School');
fprintf('Date of Vaccination: %s\n', date');
fprintf('Preferred Vaccine: %s\n', vaccine');
disp ('Note: If the preferred vaccine are not available, the vaccine that is available will be administered');
disp(['Slot number: ', num2str(index_a1)]);
fprintf(1, '\n');
%represent slot chart
slot_charta1=slots_a1
index_a1=index_a1+1;
end
if date == 'Aug0921'
disp ('Date of first dose: Aug0921')
fprintf(1, '\n');
%assign a slot
slots_a2(index_a2)=1;
sum_a2=sum_a2+1;
%print reservation
disp ('COVID-19 Vaccine Reservation')
fprintf ('Name: %s\n', name);
fprintf ('Contact Number: %.f\n', contactno);
fprintf ('Address: %s\n', address);
disp('Vaccination Site: Aguinaldo Elementary School');
fprintf('Date of Vaccination: %s\n', date');
fprintf('Preferred Vaccine: %s\n', vaccine');
disp ('Note: If the preferred vaccine are not available, the vaccine that is available will be administered');
disp(['Slot number: ', num2str(index_a2)]);
fprintf(1, '\n');
%represent slot chart
slot_charta2=slots_a2
index_a2=index_a2+1;
end
if date == 'Aug1621'
disp ('Date of first dose: Aug1621')
fprintf(1, '\n');
%assign a slot
slots_a3(index_a3)=1;
sum_a3=sum_a3+1;
%print reservation
disp ('COVID-19 Vaccine Reservation')
fprintf ('Name: %s\n', name);
fprintf ('Contact Number: %.f\n', contactno);
fprintf ('Address: %s\n', address);
disp('Vaccination Site: Aguinaldo Elementary School');
fprintf('Date of Vaccination: %s\n', date');
fprintf('Preferred Vaccine: %s\n', vaccine');
disp ('Note: If the preferred vaccine are not available, the vaccine that is available will be administered');
disp(['Slot number: ', num2str(index_a3)]);
fprintf(1, '\n');
%represent slot chart
slot_charta3=slots_a3
index_a3=index_a3+1;
end
if date == 'Aug2321'
disp ('Date of first dose: Aug2321')
fprintf(1, '\n');
%assign a slot
slots_a4(index_a4)=1;
sum_a4=sum_a4+1;
%print reservation
disp ('COVID-19 Vaccine Reservation')
fprintf ('Name: %s\n', name);
fprintf ('Contact Number: %.f\n', contactno);
fprintf ('Address: %s\n', address);
disp('Vaccination Site: Aguinaldo Elementary School');
fprintf('Date of Vaccination: %s\n', date');
fprintf('Preferred Vaccine: %s\n', vaccine');
disp ('Note: If the preferred vaccine are not available, the vaccine that is available will be administered');
disp(['Slot number: ', num2str(index_a4)]);
fprintf(1, '\n');
%represent slot chart
slot_charta4=slots_a4
index_a4=index_a4+1;
end
if date == 'Aug3021'
disp ('Date of first dose: Aug3021')
fprintf(1, '\n');
%assign a slot
slots_a5(index_a5)=1;
sum_a5=sum_a5+1;
%print reservation
disp ('COVID-19 Vaccine Reservation')
fprintf ('Name: %s\n', name);
fprintf ('Contact Number: %.f\n', contactno);
fprintf ('Address: %s\n', address);
disp('Vaccination Site: Aguinaldo Elementary School');
fprintf('Date of Vaccination: %s\n', date');
fprintf('Preferred Vaccine: %s\n', vaccine');
disp ('Note: If the preferred vaccine are not available, the vaccine that is available will be administered');
disp(['Slot number: ', num2str(index_a5)]);
fprintf(1, '\n');
%represent slot chart
slot_charta5=slots_a5
index_a5=index_a5+1;
end
elseif site == 2 && (sum_b1 < 100 || sum_b2 < 100 || sum_b3 < 100 || sum_b4 < 100 || sum_b5 < 100)
disp ('Vaccination site: Binakayan Hospital and Medical Center');
fprintf(1, '\n');
%choose vaccine
disp ('Choose your preferred vaccine');
disp ('Pfizer, AstraZeneca, Sinovac')
vaccine = input ('Enter name of vaccine (ex. AstraZeneca): ', 's');
fprintf(1, '\n');
%choose date
disp ('Choose on the following dates (MMDDYY)')
disp ('Aug0321 Aug1021 Aug1721 Aug2421 Aug3121')
date = input('Enter date of first dose (AugDDYY):', 's');
if date == 'Aug0321'
disp ('Date of first dose: Aug0321')
fprintf(1, '\n');
%assign a slot
slots_b1(index_b1)=1;
sum_b1=sum_b1+1;
%print reservation
disp ('COVID-19 Vaccine Reservation')
fprintf ('Name: %s\n', name);
fprintf ('Contact Number: %.f\n', contactno);
fprintf ('Address: %s\n', address);
disp('Vaccination Site: Binakayan Hospital and Medical Center');
fprintf('Date of Vaccination: %s\n', date');
fprintf('Preferred Vaccine: %s\n', vaccine');
disp ('Note: If the preferred vaccine are not available, the vaccine that is available will be administered');
disp(['Slot number: ', num2str(index_b1)]);
fprintf(1, '\n');
%represent slot chart
slot_chartb1=slots_b1
index_b1=index_b1+1;
end
if date == 'Aug1021'
disp ('Date of first dose: Aug1021')
fprintf(1, '\n');
%assign a slot
slots_b2(index_b2)=1;
sum_b2=sum_b2+1;
%print reservation
disp ('COVID-19 Vaccine Reservation')
fprintf ('Name: %s\n', name);
fprintf ('Contact Number: %.f\n', contactno);
fprintf ('Address: %s\n', address);
disp('Vaccination Site: Binakayan Hospital and Medical Center');
fprintf('Date of Vaccination: %s\n', date');
fprintf('Preferred Vaccine: %s\n', vaccine');
disp ('Note: If the preferred vaccine are not available, the vaccine that is available will be administered');
disp(['Slot number: ', num2str(index_b2)]);
fprintf(1, '\n');
%represent slot chart
slot_chartb2=slots_b2
index_b2=index_b2+1;
end
if date == 'Aug1721'
disp ('Date of first dose: Aug1721')
fprintf(1, '\n');
%assign a slot
slots_b3(index_b3)=1;
sum_b3=sum_b3+1;
%print reservation
disp ('COVID-19 Vaccine Reservation')
fprintf ('Name: %s\n', name);
fprintf ('Contact Number: %.f\n', contactno);
fprintf ('Address: %s\n', address);
disp('Vaccination Site: Binakayan Hospital and Medical Center');
fprintf('Date of Vaccination: %s\n', date');
fprintf('Preferred Vaccine: %s\n', vaccine');
disp ('Note: If the preferred vaccine are not available, the vaccine that is available will be administered');
disp(['Slot number: ', num2str(index_b3)]);
fprintf(1, '\n');
%represent slot chart
slot_chartb3=slots_b3
index_b3=index_b3+1;
end
if date == 'Aug2421'
disp ('Date of first dose: Aug2421')
fprintf(1, '\n');
%assign a slot
slots_b4(index_b4)=1;
sum_b4=sum_b4+1;
%print reservation
disp ('COVID-19 Vaccine Reservation')
fprintf ('Name: %s\n', name);
fprintf ('Contact Number: %.f\n', contactno);
fprintf ('Address: %s\n', address);
disp('Vaccination Site: Binakayan Hospital and Medical Center');
fprintf('Date of Vaccination: %s\n', date');
fprintf('Preferred Vaccine: %s\n', vaccine');
disp ('Note: If the preferred vaccine are not available, the vaccine that is available will be administered');
disp(['Slot number: ', num2str(index_b4)]);
fprintf(1, '\n');
%represent slot chart
slot_chartb4=slots_b4
index_b4=index_b4+1;
end
if date == 'Aug3121'
disp ('Date of first dose: Aug3121')
fprintf(1, '\n');
%assign a slot
slots_b5(index_b5)=1;
sum_b5=sum_b5+1;
%print reservation
disp ('COVID-19 Vaccine Reservation')
fprintf ('Name: %s\n', name);
fprintf ('Contact Number: %.f\n', contactno);
fprintf ('Address: %s\n', address);
disp('Vaccination Site: Aguinaldo Elementary School');
fprintf('Date of Vaccination: %s\n', date');
fprintf('Preferred Vaccine: %s\n', vaccine');
disp ('Note: If the preferred vaccine are not available, the vaccine that is available will be administered');
disp(['Slot number: ', num2str(index_b5)]);
fprintf(1, '\n');
%represent slot chart
slot_chartb5=slots_b5
index_b5=index_b5+1;
end
elseif (site == 1 && (sum_a1 >= 100 || sum_a2 >= 100 || sum_a3 >= 100 || sum_a4 >= 100 || sum_a5 >= 100)) || (site == 2 && (sum_b1 >= 100 || sum_b2 >= 100 || sum_b3 >= 100 || sum_b4 >= 100 || sum_b5 >= 100))
disp('Sorry, your preferred date of vaccination is already full. Kindly choose another date that is available for you.')
end
end
  2 Comments
Jan
Jan on 20 Jul 2021
Avoid hidden indices in the names of variablöes. Use arrays instead.
Compare:
slots_a1 = zeros(10,10);
slots_a2 = zeros(10,10);
slots_a3 = zeros(10,10);
slots_a4 = zeros(10,10);
slots_a5 = zeros(10,10);
with:
slots_a = zeros(5, 10, 10);
and
index_ab11=index_a1+index_a2+index_a3+index_a4+index_a5+index_b1+index_b2+index_b3+index_b4+index_b5;
with
index_ab11 = sum(index_a) + sum(index_b);

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 20 Jul 2021
slots_a1 = zeros(10,10);
slots_a2 = zeros(10,10);
slots_a3 = zeros(10,10);
slots_a4 = zeros(10,10);
slots_a5 = zeros(10,10);
slots_b1 = zeros(10,10);
slots_b2 = zeros(10,10);
slots_b3 = zeros(10,10);
slots_b4 = zeros(10,10);
slots_b5 = zeros(10,10);
Naming variables this way is an anti-pattern. Rather than defining variables with numbers in their names, define them as arrays.
slots_a = zeros(10, 10, 5);
Now rather than duplicating code to refer to slots_a1, slots_a2, etc. you can loop over the third dimension and access slots_a(:, :, 1), etc. Similarly you can now use functions like sum with a dimension input.
A = cat(3, magic(4), randi(10, 4, 4), eye(4))
A =
A(:,:,1) = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 A(:,:,2) = 9 9 1 4 6 2 9 5 5 1 2 10 10 7 7 2 A(:,:,3) = 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
sum(A, 3)
ans = 4×4
26 11 4 17 11 14 19 13 14 8 9 22 14 21 22 4
sum(A, 1) % Sum down the columns of each page of A
ans =
ans(:,:,1) = 34 34 34 34 ans(:,:,2) = 30 19 19 21 ans(:,:,3) = 1 1 1 1
Doing this may help you avoid or reduce code duplication.

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by