Summing values from a loop

3 Ansichten (letzte 30 Tage)
AH
AH am 1 Nov. 2019
I am pretty new to Matlab, and this is a homework assignment I am struggleing with. We are supposed to create a script that will ask for a registration number repeatedly, then when you enter '9999999', it sums how many numbers are in each category. Here is the exact question:
Registration numbers for hot air balloons are strings of seven characters. The fifth character specifies balloon type: P, for propane filled; S, for solar heated; and H, for helium or hydrogen filled. The sixth character will identify the subtype: e, for helium and y, for hydrogen. The sixth character for types P and S should be x (no subtypes). We want to count the number of licenses of each type and subtype. The input will be given by the user and the string 9999999 will be used to indicate the end of the input. Write a MATLAB script to perform this counting. Create your own functions if needed. The interactive input/output should look like
Enter balloon registration number: "1322Px7"
Enter balloon registration number: "5721Hy6"
. . .
. . .
Enter balloon registration number: "9999999"
Propane filled balloons : 12
Solar heated balloons : 4
Helium filled balloons : 7
Hydrogen filled balloons : 3
My code asks for the registration number, and displays what it needs to, except that I can't seem to make it add the number of registration numbers in each category. I have no idea what to do, because my prof. never really covered this in class. Here is what I have so far:
clear
clc
condition = true;
while condition
regNum = input('Enter balloon registration number: ', 's');
if (length(regNum)>7 || length(regNum)<7)
disp('Invalid registration number.')
condition = false;
else
i = 1;
if regNum(5)=='P' && regNum(6)=='x' % for P with x
countPx = i;
continue;
elseif regNum(5)=='S' && regNum(6)=='x' % for S with x
countSx = i;
continue;
elseif regNum(5)=='H' && regNum(6)=='y' % H with y
countHy = i;
continue;
elseif regNum(5)=='H' && regNum(6)=='e' % H with e
countHe = i;
continue;
elseif regNum == '9999999'
totalPx = sum(countPx);
totalSx = sum(countSx);
totalHy = sum(countHy);
totalHe = sum(countHe);
matrix1 = [totalPx];
matrix2 = [totalSx];
matrix3 = [totalHy];
matrix4 = [totalHe];
pOutput = ['There are' matrix1 'propane balloons.'];
disp(pOutput);
sOutput = ['There are' matrix2 'solar heated balloons.'];
disp(sOutput);
hYOutput = ['There are' matrix3 'hydrogen balloons.'];
disp(hYOutput)
hEOutput = ['There are' matrix4 'helium balloons.'];
disp(hEOutput)
condition = false;
else
disp('Invalid registration number.')
condition = false;
end
end
end
Any ideas are greatly appreciated. Thanks in advance!

Akzeptierte Antwort

David Hill
David Hill am 2 Nov. 2019
function [P,S,He,Hy,RegList]= f()
condition = true;
count=1;
while condition
regNum = input('Enter balloon registration number: ', 's');
if length(regNum)>7 || length(regNum)<7
disp('Invalid registration number.')
elseif regNum=='9999999'
condition=false;
else
RegList(count,:)=regNum;
count=count+1;
end
end
a=RegList(:,5);
b=RegList(:,6);
P=sum(a=='P');
S=sum(a=='S');
He=sum(a=='H'&b=='e');
Hy=sum(a=='H'&b=='y');
end
  4 Kommentare
AH
AH am 2 Nov. 2019
I was able to fix it. With a few changes, I was able to get the desired output. Thank you!
Benjamin Archibald
Benjamin Archibald am 16 Nov. 2020
Can you show me what changes you made? I am struggling with the same problem.
Thanks!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Mathematics finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by