int A[5] = {2, 2, 1, 4, 1};
int max = A[0], min = A[0];
int k,l;
for (i = 0; i < 5; i++) {
if (A[i] >= max) {
max = A[i];
k = i;
}
}
for (j = 0; j < 5; j++) {
if (A[j] <= min) {
min = A[j];
l = j;
}
}
printf("densely is: %d ", k);
printf("\n");
printf("loosely is: %d ", l);
Program prints position of densely
( max
) as 3
. It prints out position of loosely
( min
) as 4
. But the correct answer for the loosely
( min
) position should be 2
and 4
. In my case, the max
number is 4
, so the position should be 3
(counted from 0
). The min
number is 1
, so the position should be 2
and 4
. Now, my code only shows one position for the min
number: 4
. It should print both positions: 2
and 4
.
I want to print out all positions for the max
and min
numbers.
This is actually very good question. The key to the solution is to find the min
and max
value in the array A
. That part has been done and works well. The only improvement here is to start from index 1
and remove =
from >=
and <=
comparisons.
The missing part is to remember all indexes for min
and max
.
That is difficult to do in one pass. What you can do in one pass is to find both min
and max
.
Once you have min
and max
in the second pass you could mark all the index positions for max
and min
in additional index tables called maximums
and minimums
. (Since I want to print those values I have two separate loops for it.)
The solution is quite simple:
#include <stdio.h>
// Find all minimums and all maximums in the array A
// Find and remember where `max` and `min` values were in the array A
// The algorithm is symmetrical for minimum and maximum
int main(void)
{ // 0 1 2 3 4
int A[5] = { 2, 2, 1, 4, 1};
int maximums[5] = {0}; // we keep max positions here
int minimums[5] = {0}; // we keep min positions here
int max = A[0]; // we assume max at index 0
int min = A[0]; // we assume min at index 0
for (int i = 1; i < 5; i++) { // we can start from index 1 because of the above assumption
if (A[i] > max) max = A[i]; // densely
if (A[i] < min) min = A[i]; // loosely
}
// Find the all elements equal to `max` and `min`
// and register them in `maximums` and `minimums` arrays
// Print the results:
printf("max value is: %d and it occurs at the following index(es):\n", max);
for (int i = 0; i < 5; i++) {
if (A[i] == max){
maximums[i] = 1; // mark this index as maximum being here
printf("%d ", i);
}
}
printf("\nmin value is: %d and it occurs at the following index(es):\n", min );
for (int i = 0; i < 5; i++) {
if (A[i] == min){
minimums[i] = 1; // mark this index as minimum being here
printf("%d ", i);
}
}
// At this moment we not only printed the results
// but the array minimums and maximums remember where the `min` and `max` were.
return 0;
}
Output:
max value is: 4 and it occurs at the following index(es):
3
min value is: 1 and it occurs at the following index(es):
2 4
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.