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

1. move line 20 so that it increments i after line 22.

2. line 31: There is no need for that j loop. Just make the tests for minimum and maximum within the i loop that starts on line 19. Every time a new number of entered check of the new number > max and check if its < min.

3. When i == 0, both minvalue and maxvalue should be set to input. Since inputs can be negative, the values initialized on lines 11 and 12 may or may not work. So just initilize those two variables for the first keyboard input value.

1. move line 20 so that it increments i after line 22.

2. line 31: There is no need for that j loop. Just make the tests for minimum and maximum within the i loop that starts on line 19. Every time a new number of entered check of the new number > max and check if its < min.

3. When i == 0, both minvalue and maxvalue should be set to input. Since inputs can be negative, the values initialized on lines 11 and 12 may or may not work. So just initilize those two variables for the first keyboard input value.

Thanks, I took little bit of your advice. The j loop was still needed. I just needed to separate the min and max checking outside of the master WHILE loop. This allowed the input of 0 to be entered into the array. Below is the code.

#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);
minvalue = input;
maxvalue = input;



while (input != 0){
    numbers[i] = input;
                       
    ++i;
                printf("Enter the next array element>");
	scanf("%d", &input);

if (input == 0){
numbers[i] = 0;
  ++i;
 
  }

}
for (j =0;j<i;j++){
 if (numbers[j] >= maxvalue){
                                maxvalue = numbers[j];
                            }
                            if(numbers[j] < minvalue){
                                minvalue = numbers[j];
                            }

}

printf("%d\t", maxvalue);
printf("%d\n", minvalue);

	}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.