How to find the max, the min, and the mid value without using built-in functions?
18 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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.
Respuesta aceptada
Arturo Moncada-Torres
el 16 de Mayo de 2011
I suppose you just put the two pieces of code together, which won't work, because the first one is to get the mid value with an array and the second one shows how to get the max and min without an array.
Your code should be like this:
% Inquire the user for input
N=input('Enter the number of the students: ');
% Define limits
Max=0;
Min=inf;
G = zeros(1,N); % Preallocate memory for efficient execution.
% Obtain the maximum and the minimum
for a=1:N
grade = input('Enter the grade: ');
G(a) = grade;
% Evaluate maximum
if grade > Max
Max = grade;
end
% Evaluate minimum
if grade < Min
Min = grade;
end
% Notice how to calculate the maximum and the minimum, you do not need
% an array, just the current value you are obtaining from the user. You
% use the array to calculate the nearest grade to the average (mid
% value) further below.
end
% Display the results
fprintf('\nThe max is %f\n', Max);
fprintf('The min is %f\n\n', Min);
AV = sum(G)/N; % Calculate the average
% Obtain the mid value (nearest grade to the average)
difference = inf; % Initialize the difference
for i = 1:N
newDifference = abs(G(i)-AV);
if newDifference < difference
midIndex = i;
difference = newDifference;
end
end
% Display the result on console
fprintf('The average is %f\n', AV);
fprintf('The mid value is %f\n\n', G(midIndex));
This is the definitive version! I already tried it and it works perfectly. Anyway, try it by yourself and let me know what it turns out =) !
2 comentarios
Arturo Moncada-Torres
el 16 de Mayo de 2011
I am glad it worked. Keep trying and learning MATLAB ;-)
Más respuestas (2)
Ver también
Categorías
Más información sobre Get Started with MATLAB en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!