How to find the max, the min, and the mid value without using built-in functions?

17 views (last 30 days)
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.

Accepted Answer

Arturo Moncada-Torres
Arturo Moncada-Torres on 16 May 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 =) !

More Answers (2)

Abdullah
Abdullah on 16 May 2011
@Arturo Moncada Torres
I have tested your code, but it didn't work properly. It doesn't give the nearest value to the average.

Abdullah
Abdullah on 16 May 2011
@Arturo Moncada Torres
Thank you for your patience, but I have tried your last code for finding the mid value, and as soon as I enter the second grade in the iteration, this message pops up in the command window:
??? Attempted to access G(2); index out of bounds because numel(G)=1.
Error in ==> mid at 6 newDifference = abs(G(i)-AV);
What's the problem?

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!