Help with Min/Max Value loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dleary4395
    New Member
    • Feb 2012
    • 1

    Help with Min/Max Value loop

    Hello, my question is how to find the minimum value of a set of numbers in a loop. I used a for loop, and the maximum value is always right but the minimum is always 0. I think it's because the minValue is set to 0 to begin with so it thinks it's an input and not just the starter value. This is my code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define pause system("pause")
    #define LOOPMAX 5
    
    int main() {
    	int loopCounter = 0;
    	float maxNum = 0.0;
    	float num = 0.0;
    	float minNum = 0.0;
    	
    
    	for (loopCounter = 0; loopCounter <LOOPMAX; loopCounter++) {
    		printf("Enter a Number:  ");
    		scanf("%f" ,&num);
    		if (num>=maxNum) {
    			maxNum=num;
    		}
    		if (num<=minNum) {
    			minNum=num;
    		}
    		} //end loop
    	printf("The Maximum of the values entered is %.2f.\n" ,maxNum);
    	printf("The Minimum of the values entered is %.2f.\n" ,minNum);
    	pause;
    }

    any help would be greatly appreciated
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    The min and max should always be set to the first value entered.

    Comment

    Working...