From f9bfd193c5a0b6e06920aeb783c96cdeaebebfac Mon Sep 17 00:00:00 2001 From: Luke Kenneth Casson Leighton Date: Tue, 14 Nov 2023 11:58:09 +0000 Subject: [PATCH] fix maxloc --- maxloc/maxloc.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/maxloc/maxloc.c b/maxloc/maxloc.c index e37160c6..f0786e73 100644 --- a/maxloc/maxloc.c +++ b/maxloc/maxloc.c @@ -1,23 +1,22 @@ -#include +#include int m2(int *a, int n) { - int m, nm; - int i; + int m = 0; + int nm = -1; + int i = 0; - m = 0; - nm = -1; - i=0; while (i m) { - m = a[i]; - nm = i; - } - i++; + while (i < n && a[i] > m) { + printf("%d idx %d > m %d\n", i, a[i], m); + m = a[i]; + nm = i; + i++; + } } return nm; } -- 2.30.2