#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int max, min;
for (int i = 0; i < n; i++) {
int num;
scanf("%d", &num);
if (i == 0) {
max == num;
min == num;
} else {
if (num > max) max = num;
if (num < min) min = num;
}
}
printf("%d\n", max - min);
return 0;
}
This program reads an integer `n`, then reads `n` more integers, and attempts to print the difference between the maximum and minimum of those numbers. However, it has a critical mistake that prevents it from working correctly.
- In the `if (i == 0)` block, you used `==` (comparison operator) instead of `=` (assignment operator). The lines `max == num;` and `min == num;` compare values but do not store anything, so `max` and `min` remain uninitialized. This leads to undefined behavior when they are later used in comparisons.
- Since `max` and `min` are not initialized before the loop, and the first values are not assigned correctly, the program may produce incorrect output or crash.