I think this can be fixed by removing the "else" in this statement:Step 2 TOTALLY messes up the program. Say I input 76 and 8. It puts the max number to 0 and the minimum to 8.
Code:if (num < min) num = min; else if (num > max) num = max;
I think this can be fixed by removing the "else" in this statement:Step 2 TOTALLY messes up the program. Say I input 76 and 8. It puts the max number to 0 and the minimum to 8.
Code:if (num < min) num = min; else if (num > max) num = max;
Code:#include <stdio.h> #include "genlib.h" #include "simpio.h" int main (void) { int min, max, num; printf ("This will compute the minimum and maximum number in the list. Signal the end of the list with 0.\n"); printf ("Enter your first number: "); num = GetInteger(); min = 0; max = 0; while (num!=0) { if (num < min) min = num; else if (num > max) max = num; else num = num; printf ("Enter your next number: "); num = GetInteger(); } printf ("The maximum number in the list is %d.\n", max); printf ("The minimum number in the list is %d.\n", min); getchar(); }
If your input is never going to be zero, you can get away with doing this (though it's sloppy):
This will not let the value of "min" be updated if the input is zero.Code:if ((num < min)&&(num != 0)
Let me repost the ops original code block for all you "do while()er's" out there...
So if GetInteger is 0 it will exit, the real problem is that he is starting out with min = 0, so of course nothing will be LESS then zero, that is the whole purpose of "step 2" originally posted by subhashCode:num = GetInteger(); min = 0; max = 0; while (num!=0) { if (num < min) num = min; else if (num > max) num = max; else num = num; /* btw what is this line about? dont even have an ELSE if you dont need one */ printf ("Enter your next number: "); num = GetInteger(); /* whats the next statement after this executed? */ }
The only missing problem is that the logic needs to change from "if else if" to just "if and if" ... the max should always be set despite the fact the min was set
Last edited by nonpuz; 07-14-2011 at 11:27 PM. Reason: Forgot to make clear that assignments are still backwards (and added comment)
It does if the first value you enter is the largest, assuming you assigned "min" to an initial value much greater than zero.That didn't change anything.
EDIT: nonpuz summed this up first above.
Thanks! That worked. Thanks to everybody else for your time and effort. Before when i tried it it was min = 9999 and it was if else if. With that combination it did not work, but when I changed it to if if it worked.
EDIT: I used min != 0, not min = 9999. It looks and works much better.
Final Code:
Code:#include <stdio.h> #include "genlib.h" #include "simpio.h" int main (void) { int min, max, num; printf ("This will compute the minimum and maximum number in the list.\nSignal the end of the list with 0.\n"); printf ("Enter your first number: "); num = GetInteger(); min != 0; max = 0; while (num!=0) { if (num < min) min = num; if (num > max) max = num; printf ("Enter your next number: "); num = GetInteger(); } printf ("The maximum number in the list is %d.\n", max); printf ("The minimum number in the list is %d.\n", min); getchar(); }
Last edited by PYROMANIAC702; 07-14-2011 at 11:35 PM.
min != 0; is not an assignment operation its a conditional statement, resulting in NOTHING in this case. What really happened is that your "min" is now initialized to some random data (by compiler) since you didn't set it specifically equal to zero
Last edited by nonpuz; 07-14-2011 at 11:39 PM. Reason: misspelled "now" as not
Ok, so the easiest way to do this would be:
And != does not work as an assignment operator.Code:num = GetInteger(); max=min=num; while.....