How to find the max, the min, and the mid value without using built-in functions?
Ältere Kommentare anzeigen
Suppose that we want to assign a number of students, each one should be assigned to a numerical grade. The user is the one who should choose the number of the students, and then he should be able to calculate the average of the grades, the max value, the min value, and the mid value, each one of the last three requirements should their rank (position) be specified.
As it seems, the exercise is pretty simple, even with the rule set by my instructor, which is no built-in function can be used to find the max, the min, and the mid (middle) value. Here's the code that I wrote, and my questions follow it underneath:
N=input('Enter the number of the students: ');
Max=0;
Min=inf;
for a=1:N
G(a)=input('Enter the grades: ');
end
AV=sum(G)/N;
for a=1:N
if G(a)>Max
Max=G(a);
R1=a;
end
if G(a)<Min;
Min=G(a);
R2=a;
end
end
My questions:
1-Isn't there another way to find the max and the min value without assigning the grades into an array?
2-How would you suggest to find the mid value?
Note:The mid value required, is the nearest grade to the average of the grades. For example, if the average is 57 and we have two grades 50 and 60 inserted by the user, then the mid would be 60.
Thanks in advance.
1 Kommentar
Matt Fig
am 16 Mai 2011
A de facto FAQ.
Akzeptierte Antwort
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!