Hi there, I am trying to find the max and min value in a an array of size 100, but the input is stopped when there is a 0 entered, and it too gets added to the array.
The code below works good, EXCEPT it's min value is always -2, for some reason but the max value is 100% right. Can somebody please tell me the answer, this is due tomorrow and I have been working on this for 2 days now.
#include <stdio.h>
#include <stdlib.h>
#define N 100
int main(void){
int numbers[N];
int i = 0;
int j;
int input;
int maxvalue =1;
int minvalue = 1;
printf("Enter the next array element>");
scanf("%d", &input);
while (input != 0){
++i;
numbers[i] = input;
printf("Enter the next array element, while loop>");
scanf("%d", &input);
if (input == 0){
++i;
numbers[i] = 0;
for (j=0;j<i;j++){
if (numbers[j] > maxvalue){
maxvalue = numbers[j];
} else if(numbers[j] < minvalue){
minvalue = numbers[j];
}
}
}
}
printf("%d\t", maxvalue);
printf("%d\t", minvalue);
printf("%d\n", numbers[i-2]);
}
Here is the output I am talking about:
Enter the next array elernnt>2
Enter the next array elenent while loop>3
Enter the next array elenent. while loop>0
3 —2 2
Press [Enter] to close the terminal