Please remove the line "min != 0;" from your program, recompile, run it.
....
Now re-read my post.
Please remove the line "min != 0;" from your program, recompile, run it.
....
Now re-read my post.
Well, let's see I ran this:
and my output was -858993460.Code:#include <stdio.h> int main(void){ int num; num != 0; printf("%d", num); return(0); }
The variable hasn't been initialized so anything you try to compare it with will result in undefined behaviour. Which means no one knows what it will do. I guarantee that this will not be accepted by your teacher. It isn't gramatically correct because it isn't an assignment operator, thus you never put any value into your variable min.
Oh, I am sorry. I never said what you said was wrong, I just said my way worked. I now realized that min = 0 is unnecessary. It is no longer in my code.
Here is what my code now looks like:
I'm pretty sure that it is flawless now. Correct me if I am wrong.Code:#include <stdio.h> #include "genlib.h" #include "simpio.h" main() { 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(); max = 0; while (num!=0) { if (num < min) min = num; if (num > max) max = num; printf ("Enter your next number: "); num = GetInteger(); } printf ("\nThe maximum number in the list is %d.\n", max); printf ("The minimum number in the list is %d.\n", min); getchar(); }
EDIT: And of course this:
I'm sort of confused on this one. Are you talking about max = 0? If I don't have that then the program doesn't work.
EDIT: I get it now.
Is this correct?Code:#include <stdio.h> #include "genlib.h" #include "simpio.h" main() { 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(); max=min=num; while (num!=0) { if (num < min) min = num; if (num > max) max = num; printf ("Enter your next number: "); num = GetInteger(); } printf ("\nThe 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-15-2011 at 12:03 AM.
The easiest way to do this is:Code:main() { 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(); max = 0; while (num!=0) { if (num < min)WHAT IS THE VALUE OF MIN RIGHT NOW? min = num; if (num > max) max = num; printf ("Enter your next number: "); num = GetInteger(); } printf ("\nThe maximum number in the list is %d.\n", max); printf ("The minimum number in the list is %d.\n", min); getchar(); }
This way before entering your while loop max and min are both initialized to a value, namely the first number entered. Which if you follow the logic of how you would do it by paper works. Watch keep track of max and min for me, ready?Code: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(); max = min=num; while (num!=0) {....
First number: 5
So what is the max and min of the number sequence I gave you?
It's ok, I'm not getting frustrated I was just trying to show you where in the code and it is hard to do without caps and colors. The code in the edit looks good, as long as you understand the logic. Is this a summer program thing?
Ok, well after the class then take a look at getting a good compiler. May I suggest:
Pelles C A great compiler and IDE but it is just C
MS Visual Studio Express Great compiler and IDE and does C/C++
Note both of these compilers are free. Also keep in mind you are learning how to program with this class but you are not learning standard C. If you want to get into that then our FAQ and tutorial board is a great place to start.