From 097d1fd52b15113f11b90446d060931618ed06c5 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