필터 지우기
필터 지우기

Finding the maximum value without using built-in functions

조회 수: 13 (최근 30일)
Abdullah
Abdullah 2011년 5월 13일
My instructor assigned me a problem to solve, but unfortunately I couldn't, though I know it's an easy trick. I will write the program first, then I will ask the question:
n=input('Enter the number of the student: ');
for a=1:n
B(a)=input('Enter the grades as numerical values: ');
end
It's clear that variable B is a matrix containing the grades as numerical values. The problem that's assigned to me, is to find a way to show the biggest grade in the matrix without using MAX function or any other built-in functions, but rather by comparing the grades using for loop. It's like comparing the first grade with the other grades, and then the loop will keep assigning the bigger value each time the condition is verified.
I don't know how to do it although I have tried numerous times, so I would be thankful for your help.

채택된 답변

Leah
Leah 2011년 5월 13일
I think sort counts as a built-in function. I assume all of your grades will be positive
n=input('Enter the number of the student: '); maxgrade=0; for a=1:n
B(a)=input('Enter the grades as numerical values: ');
if B(a) > maxgrade
maxgrade=B(a);
end
end
sprintf('Max Grade: %0.5g',maxgrade)
  댓글 수: 1
Sean de Wolski
Sean de Wolski 2011년 5월 13일
That was the point. Typically the question is "How can I find the max without using MAX"; the two examples I provided were cheap workarounds. However, this professor has also eliminated other built-ins, making them wise up to our awesomeness.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Andy
Andy 2011년 5월 13일
Before the loop, create a variable called currMax, and set it equal to 0. Within the loop, with each grade that's entered, compare it to currMax. If it is bigger than currMax, then you can replace currMax with that new grade. Otherwise, do not replace currMax. There's not much more to say without doing your assignment for you.
  댓글 수: 3
Andy
Andy 2011년 5월 13일
Fair enough, but if the class is receiving negative grades, then there are bigger problems than our mistaken assumption. :)
Sean de Wolski
Sean de Wolski 2011년 5월 13일
Initialize currMax to -inf, for Andy's method.

댓글을 달려면 로그인하십시오.


Sean de Wolski
Sean de Wolski 2011년 5월 13일
Those professors are wising up to the:
the_max = min(-x);
%or
s = sort(x);
the_max = s(end);
tricks...
  댓글 수: 1
Matt Tearle
Matt Tearle 2011년 5월 13일
Fight back! This assignment, as stated ("any other built-in function"), is impossible! Proof:
which gt
edit gt

댓글을 달려면 로그인하십시오.


Abdullah
Abdullah 2011년 5월 13일
Thank you all for your contributions, but Leah has given me the right answer. I just don't believe that I haven't though in such a solution.
I found another problem that I think can't be solved without using a built-in function, but let's see if you have it. The problem is that I need to find the rank of the biggest grade in the array B. The traditional way by using MAX function is to write for example, [y,c]=max(B), where y is the biggest value, and c is the position of that value in the matrix, hence it's the rank in our question. Is there a way to get the rank shown up without using a built-in function?
  댓글 수: 1
Sean de Wolski
Sean de Wolski 2011년 5월 13일
just record a inside the if statement.
if B(a) > maxgrade
maxgrade=B(a);
idx = a;
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by