#include <stdio.h>
int main() {
int a[10];
int min;
int max;
int q;
int p;
for (int i = 0; i < 10; i++) {
scanf("%d", &a[i]);
}
max = a[0];
for (int i = 0; i < 10; i++) {
if (a[i] > max) {
max = a[i];
p = i + 1;
}
}
printf("%d ", p);
printf("%d ", max);
min = a[0];
for (int i = 0; i < 10; i++) {
if (a[i] < min) {
min = a[i];
q = i + 1;
}
}
printf("%d ", q);
printf("%d ", min);
return 0;
}
This code reads 10 integers into an array, then finds the maximum and minimum values along with their positions (1-indexed). It prints the position and value of the maximum, then the position and value of the minimum.
- The variables `p` and `q` are used without being initialized. If the first element is already the maximum or minimum, the condition `if (a[i] > max)` or `if (a[i] < min)` will never be true for that element, so `p` or `q` will remain uninitialized and print garbage. Consider what should happen when the first element is the max or min.
- The code prints the position of the maximum before the maximum value, and the position of the minimum before the minimum value. Check if the output order matches what the task expects.