#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    28
    Rep Power
    0

    Simple C Program Help


    It seems straight forward but I can't seem to get my program to run.

    Write a program that reads numbers(integers) until a negative number is read,and prints the number of values read, the largest value, and the smallest value. (You may want to use INT MAX,and INT MIN for the initial values of min and max. They are defined in <limits.h>
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    > It seems straight forward but I can't seem to get my program to run.
    It seems straight forward, but we can't see your code.

    Post your attempt, then we can tell you how to improve, what to fix, why it won't work etc.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Sorry, but you need to be physically in my presence and within arm's length for the Vulcan mind-meld to have any chance of working. Of course, I'm not sure if even that would since 1) I am not a Vulcan and 2) I've yet to be able to physically lay hands on a newbie idiot who wanted to force us to play stupid guessing games with him.

    So instead of expecting us all to read your mind, why don't you post your code? I dunno, does that seem too straight forward for you to comprehend?

    And when you post your code, don't forget to indent and format it properly and to use code tags to preserve that formatting. HTML just strips away all that leading white space and reduces code to an unreadable mess.

    Here is how you use code tags:
    [code] copy and paste your properly indented code into here [/code]
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by salem
    > It seems straight forward but I can't seem to get my program to run.
    It seems straight forward, but we can't see your code.

    Post your attempt, then we can tell you how to improve, what to fix, why it won't work etc.
    #include <math.h>
    #include <stdio.h>
    #include <limits.h>


    int main()

    {
    double max;
    double min;
    double num;
    max = INT_MIN;
    min = INT_MAX;


    printf("Enter numbers\n");

    scanf("%lf",&num);

    while(num>0)
    {

    scanf("%lf", &num);
    if(num>max)

    max=num;

    else if(num>min)

    min=num;

    else if (max>min)
    printf("no positive value entered");
    }
    printf("Minimum: %lf\n", min);
    printf("Maximum: %lf\n", min);
    printf("Number of inputs: %lf\n", num);





    system("pause");
    return 0;
    }
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    What part of use code tags can't you understand?

    Code:
    #include <math.h>
    #include <stdio.h>
    #include <limits.h>
    
    
    int main()
    
    { 
        double max;
        double min;
        double num;
        max = INT_MIN;
        min = INT_MAX;
       
        
             printf("Enter numbers\n"); 
             
             scanf("%lf",&num);
             
             while(num>0)
             {
                         
                         scanf("%lf", &num);
                         if(num>max)
                         
                         max=num;
                         
                         else if(num>min)
                         
                         min=num;
                         
                         else if (max>min)
                         printf("no positive value entered");
             }
             printf("Minimum: %lf\n", min);
             printf("Maximum: %lf\n", min);           
             printf("Number of inputs: %lf\n", num);
             
             
             
            
       
       system("pause");
       return 0;
    }
    You also need to learn how to indent code consistently. You really do need to learn that, since you are not the only person who will ever have to read your code. Though the person who will benefit the most from you making your code as readable as possible will be you yourself.

    Also, just what exactly do you mean by "I can't seem to get my program to run."? Are you expecting us to read your mind again? Would you call your doctor on the phone and challenge him to diagnose your illness without any description of the symptoms than "I'm sick."? So should you think that that behavior would work here?

    • Does your program compile?
    • Does your program crash when you try to run it?
    • Is your program unresponsive to your attempts to input into it?
    • Does your program spit out garbage for output?
    • Does your program output values that are wrong? How are they wrong? What output would be right? Exactly what output are you getting?

    Any or none of those symptoms could be indicated by your "I can't seem to get my program to run." Why do you expect us to read your mind in order to determine what you mean?

    What is the program doing or not doing? And how does that differ from what you expect it to do?

    Here's your code properly and consistently formatted and with a few comments added in red:
    Code:
    #include <math.h>
    #include <stdio.h>
    #include <limits.h>
    
    
    int main()
    { 
        double max;
        double min;
        double num;
        // You chose the initial values for min and max correctly.
        // No problem here.
        max = INT_MIN;
        min = INT_MAX;
        
        printf("Enter numbers\n"); 
        // This scanf does belong here in order to make
        //     the while loop work, or at least to ensure that we
        //     will enter the loop.
        // But if you change the while loop to a do-while, then 
        //     it won't be necessary here.
        scanf("%lf",&num);
         
        // This could also be done as a do-while.
        // Read the other comments to understand how.
        while(num>0)
        {
            // As it stands, you just threw away the
            //     first value that had been entered.  Do you really
            //     want to do that?
            // If you keep the while loop, this should be moved to
            //     the bottom of the loop.  But if you change to a
            //     do-while, then it would remain here.
            scanf("%lf", &num);
            if(num>max)
                max=num;
            // Examine the logic of the condition.
            // Will that really detect when num should replace min?
            else if(num>min)
                min=num;
            // Examine the logic of the condition.
            // Does that conditional really detect the condition that
            //     you are reporting?  On its face, it looks like that
            //     would detect "situation normal."  And just what
            //     is "no positive value entered" supposed to mean?
            else if (max>min)
                printf("no positive value entered");
        }
        printf("Minimum: %lf\n", min);
        printf("Maximum: %lf\n", min);           
        // But you weren't using num to maintain
        //    a count of the number of inputs.
        printf("Number of inputs: %lf\n", num);
         
        system("pause");
        return 0;
    }
    PS
    Your program also does not compile cleanly:
    C:TEST>gcc -Wall vilodic1.c
    vilodic1.c: In function `main':
    vilodic1.c:27: warning: use of `l' length character with `f' type character
    vilodic1.c:28: warning: use of `l' length character with `f' type character
    vilodic1.c:29: warning: use of `l' length character with `f' type character
    vilodic1.c:31: warning: implicit declaration of function `system'

    C:TEST>
    You can't use l with %f in printf, though it is vitally needed in the scanf call. In printf, an l only works for integers, characters, and pointers, according to the documentation. Read the documentation, AKA RTFM! ("Read The Manual!"). If you don't have a help file or the man pages on your computer, then Google on man page printf.

    In order to use a library function, you need to #include its header file, always! You didn't do that for system. Sometimes, some compilers and linkers will take care of it for you, but not always. I have seen library function calls fail completely, some even resulting in crashes, for this very reason. #include the header file. RTFM to find out which one it is.

    Never ignore warnings. Warnings are the compiler's way of telling you that something is very likely wrong with your program. If you were an experienced programmer, you could be doing something very clever, though in that case you'd also know how to do it without raising warnings. But since you are not an experienced programmer, warnings mean that you've done something stupid. And doing something stupid means that your program is broken and won't do what you think it should. And when you've done something stupid, then the warnings should point you right to where the problem lies. That makes warnings much more important than error messages. Never ignore warnings!
    Last edited by dwise1_aol; October 18th, 2013 at 04:48 PM.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by dwise1_aol
    What part of use code tags can't you understand?

    Code:
    #include <math.h>
    #include <stdio.h>
    #include <limits.h>
    
    
    int main()
    
    { 
        double max;
        double min;
        double num;
        max = INT_MIN;
        min = INT_MAX;
       
        
             printf("Enter numbers\n"); 
             
             scanf("%lf",&num);
             
             while(num>0)
             {
                         
                         scanf("%lf", &num);
                         if(num>max)
                         
                         max=num;
                         
                         else if(num>min)
                         
                         min=num;
                         
                         else if (max>min)
                         printf("no positive value entered");
             }
             printf("Minimum: %lf\n", min);
             printf("Maximum: %lf\n", min);           
             printf("Number of inputs: %lf\n", num);
             
             
             
            
       
       system("pause");
       return 0;
    }
    You also need to learn how to indent code consistently. You really do need to learn that, since you are not the only person who will ever have to read your code. Though the person who will benefit the most from you making your code as readable as possible will be you yourself.

    Also, just what exactly do you mean by "I can't seem to get my program to run."? Are you expecting us to read your mind again? Would you call your doctor on the phone and challenge him to diagnose your illness without any description of the symptoms than "I'm sick."? So should you think that that behavior would work here?

    • Does your program compile?
    • Does your program crash when you try to run it?
    • Is your program unresponsive to your attempts to input into it?
    • Does your program spit out garbage for output?
    • Does your program output values that are wrong? How are they wrong? What output would be right? Exactly what output are you getting?

    Any or none of those symptoms could be indicated by your "I can't seem to get my program to run." Why do you expect us to read your mind in order to determine what you mean?

    What is the program doing or not doing? And how does that differ from what you expect it to do?

    Here's your code properly and consistently formatted and with a few comments added in red:
    Code:
    #include <math.h>
    #include <stdio.h>
    #include <limits.h>
    
    
    int main()
    { 
        double max;
        double min;
        double num;
        // You chose the initial values for min and max correctly.
        // No problem here.
        max = INT_MIN;
        min = INT_MAX;
        
        printf("Enter numbers\n"); 
        // This scanf does belong here in order to make
        //     the while loop work, or at least to ensure that we
        //     will enter the loop.
        // But if you change the while loop to a do-while, then 
        //     it won't be necessary here.
        scanf("%lf",&num);
         
        // This could also be done as a do-while.
        // Read the other comments to understand how.
        while(num>0)
        {
            // As it stands, you just threw away the
            //     first value that had been entered.  Do you really
            //     want to do that?
            // If you keep the while loop, this should be moved to
            //     the bottom of the loop.  But if you change to a
            //     do-while, then it would remain here.
            scanf("%lf", &num);
            if(num>max)
                max=num;
            // Examine the logic of the condition.
            // Will that really detect when num should replace min?
            else if(num>min)
                min=num;
            // Examine the logic of the condition.
            // Does that conditional really detect the condition that
            //     you are reporting?  On its face, it looks like that
            //     would detect "situation normal."  And just what
            //     is "no positive value entered" supposed to mean?
            else if (max>min)
                printf("no positive value entered");
        }
        printf("Minimum: %lf\n", min);
        printf("Maximum: %lf\n", min);           
        // But you weren't using num to maintain
        //    a count of the number of inputs.
        printf("Number of inputs: %lf\n", num);
         
        system("pause");
        return 0;
    }
    PS
    Your program also does not compile cleanly:

    You can't use l with %f in printf, though it is vitally needed in the scanf call. In printf, an l only works for integers, characters, and pointers, according to the documentation. Read the documentation, AKA RTFM! ("Read The Manual!"). If you don't have a help file or the man pages on your computer, then Google on man page printf.

    In order to use a library function, you need to #include its header file, always! You didn't do that for system. Sometimes, some compilers and linkers will take care of it for you, but not always. I have seen library function calls fail completely, some even resulting in crashes, for this very reason. #include the header file. RTFM to find out which one it is.

    Never ignore warnings. Warnings are the compiler's way of telling you that something is very likely wrong with your program. If you were an experienced programmer, you could be doing something very clever, though in that case you'd also know how to do it without raising warnings. But since you are not an experienced programmer, warnings mean that you've done something stupid. And doing something stupid means that your program is broken and won't do what you think it should. And when you've done something stupid, then the warnings should point you right to where the problem lies. That makes warnings much more important than error messages. Never ignore warnings!
    Sorry for being unclear once again. Here are all the details. In the compiler(using Dev C++) source code compiles. However when I run the program I get numbers that don't go with the numbers I used for "n". I am supposed to enter integers and then the program should calculate the max and min of those numbers until a negative integer is entered.

    Finally the program should print the number of integers inputted. So for example:

    Enter values:
    3
    4
    1
    2
    102
    -2

    Max value: 102
    Min value: 3
    Number of values read: 5

    That is how the program is supposed to run. However when I run it.

    example
    Inputs: 3 4 5 102 7 -4
    Max value: I get some incredibly high number in the millions
    Min value: same problem as above
    Number of values entered: I will always get whatever negative number was inputted,in this case I would get -4


    Sorry about the formatting I am new to the forum as you can tell. I apologize if I didn't post sufficient information. I appreciate the help I'm getting thanks.
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Did you read the comments I had inserted in red? They point right to some problems and questionable code that I saw. I had written them for a reason.

    Originally Posted by vilodic
    Max value: I get some incredibly high number in the millions
    Min value: same problem as above
    That led me to look at the printf's and I just now saw this:
    Code:
        printf("Minimum: %lf\n", min);
        printf("Maximum: %lf\n", min);
    You don't even try to print out the value of max, but rather you print out min twice. From the looks of your code, if you were to actually printf the value of max, then it would correctly display the actual maximum value you entered.

    The problem with min is in the test that I had pointed to as not doing what you want it to do. As your code is (incorrectly) written now, in order to change the value of min, you would need to enter a value that is greater than INT_MAX. But then you would just end up with min set to an even larger value. Again: Are you really sure you want to test for num being greater than min? With pencil and paper, start with an initial value for min and throw some test values for num at that if statement and see what happens. Then think of what it needs to say instead in order to do what you want it to do ... and test that too.

    Originally Posted by vilodic
    Number of values entered: I will always get whatever negative number was inputted,in this case I would get -4
    Yes, of course you get that, but that is because that is exactly what you had told the program to do. If you want a different outcome, then you need to tell the computer what you want it to do.

    Read my comment to that printf: "But you weren't using num to maintain a count of the number of inputs." What does that mean? That means that the value that you are outputting is not a count of the number of values you had input. Rather, num is the actual value that you had input, so naturally when you enter a negative number to exit the loop, then num will contain that negative number. The facts are starkly obvious. For that matter, in your program you never ever maintain a count of how many times you input a value, so how could you possibly report such a count which is non-existent?

    Normally to maintain a count, you reserve a variable to hold that count (meaning that that is the only thing that you use that variable for), you initialize it to zero, and every time something happens that you want to be counted, you increment the counter variable. If you want to report a count, then you need to use a counter variable.

    You may also want to post the current version of your code in further messages when you have made a number of changes. That way we won't have to try to read your mind when you simply say, "I made some changes and now it does something different." leaving us with absolutely no clue what changes you had made. I only mention that, because that is exactly what far too many people have done here in the past.
    Last edited by dwise1_aol; October 18th, 2013 at 06:23 PM.
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Originally Posted by vilodic
    In the compiler(using Dev C++) source code compiles.
    Which Dev C++? It was originally developed and maintained by Bloodshed (intended in evoke Churchill's famous speech about "blood, sweat, and tears"), but then they dropped support and it was later picked up by Orwell who currently maintains it.

    About 8 years ago I installed Bloodshed's Dev C++ and found it so unusably buggy that I gave up on it completely. However, I continue to use the MinGW gcc compiler that it installed. And as I understand, Orwell's Dev C++ still uses MinGW gcc, though undoubtedly a later version. So the compiler I used to show you those warnings is the same one that you are using, disregarding version differences. The main difference is that I turned warnings on and up, which is what you also need to do.

    Somewhere in the IDE there is an option for turning warnings on or off. Never turn warnings off! That would be like Zaphod Beeblebrox' peril-sensitive sunglasses: in order to ensure that you don't get upset or worried about anything, they detect approaching danger and turn opaque just as you are about to be obliterated by that danger, just so you won't see what's about to happen to you and get upset in any way. Turning warnings off is incredibly more stupid than wearing peril-sensitive sunglasses. As the programmer, you need to know when there's something wrong with your code!

    Unfortunately, users here of Orwell Dev-C++ have reported that even when they have turned warnings on the IDE still doesn't report warnings to them. Of course, I never have that kind of a problem because I always run MinGW gcc as it was meant to be run, from the command line.

    Whatever it takes, you need to get your IDE to display all warnings. If there's a users' forum, you may want to pursue the question there.

    In the meantime, rest assured that when you post code and claim that it compiles, I will verify that and I will post the warnings that it gives me. Because you do have a need to know!
    Last edited by dwise1_aol; October 18th, 2013 at 06:42 PM.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by dwise1_aol
    Which Dev C++? It was originally developed and maintained by Bloodshed (intended in evoke Churchill's famous speech about "blood, sweat, and tears"), but then they dropped support and it was later picked up by Orwell who currently maintains it.

    About 8 years ago I installed Bloodshed's Dev C++ and found it so unusably buggy that I gave up on it completely. However, I continue to use the MinGW gcc compiler that it installed. And as I understand, Orwell's Dev C++ still uses MinGW gcc, though undoubtedly a later version. So the compiler I used to show you those warnings is the same one that you are using, disregarding version differences. The main difference is that I turned warnings on and up, which is what you also need to do.

    Somewhere in the IDE there is an option for turning warnings on or off. Never turn warnings off! That would be like Zaphod Beeblebrox' peril-sensitive sunglasses: in order to ensure that you don't get upset or worried about anything, they detect approaching danger and turn opaque just as you are about to be obliterated by that danger, just so you won't see what's about to happen to you and get upset in any way. Turning warnings off is incredibly more stupid than wearing peril-sensitive sunglasses. As the programmer, you need to know when there's something wrong with your code!

    Unfortunately, users here of Orwell Dev-C++ have reported that even when they have turned warnings on the IDE still doesn't report warnings to them. Of course, I never have that kind of a problem because I always run MinGW gcc as it was meant to be run, from the command line.

    Whatever it takes, you need to get your IDE to display all warnings. If there's a users' forum, you may want to pursue the question there.

    In the meantime, rest assured that when you post code and claim that it compiles, I will verify that and I will post the warnings that it gives me. Because you do have a need to know!

    Code:
    #include <math.h>
    #include <stdio.h>
    #include <limits.h>
    
    
    int main()
    
    { 
        double max;
        double min;
        double num;
        max = INT_MIN;
        min = INT_MAX;
        
       
        
             printf("Enter numbers\n"); 
             
             scanf("%lf",&num);
             
             while(num>0)
             {
                         
                         scanf("%lf", &num);
                         if(num>max)
                         
                         max=num;
                         
                         else if(num<min)
                         
                         min=num;
                         
                         else if (num>0)
                         printf("no positive value entered");
             }
             printf("Minimum: %lf\n", min);
             printf("Maximum: %lf\n", max);           
             printf("Number of inputs: %lf\n", num);
             
             
             
            
       
       system("pause");
       return 0;
    }
    Ok so now the I get the max and min. However it is giving the negative value i enter as the minimum and I don't want that.

    From the previous version of the code I only changed the error of printing two min values.

    Here's an example of running the program.

    2 3 6 10 1 -12

    Max: 10
    Min: -12
    Number of inputs: -12

    Ok so now that at least I'm getting max and min values. How do I fix it so it does not read the negative number (-12) but instead it prints the least positive integer (2).

    Also I'm not sure on how to do so that it read the number of inputs. I have an Idea of doing a for loop for that but I am not sure of what to put in the part sum+.


    int sum=0

    for (i=1;i<=n;i++)
    {
    sum+=???
    }

    I really appreciate all the comments and help you have provided. I am using DEV-C++ 4.9.9.2 by Bloodshed. Bear with me if my programming is really bad, I am a student with only about 3 months of experience in C-language programming.
  18. #10
  19. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    I realize that you're probably going into a kind of sensory overload with all the things to think about. You'll get there, but I'll still have to overload you. And thanks for spelling "bear" correctly; it bothers me when people ask me to get naked just because they are ("bare with me") [grin].

    You need to #include stdlib.h, because that's what system needs. You also should change the printf's from "%lf" to "%f", since that's what the other warnings are about.

    I really am very serious about not ignoring warnings. I lost count of all the threads started here asking for the solution of really crazy problems in "code that compiles cleanly", when in reality those programs threw warnings and those warnings pointed us directly to the problems. Warnings really and truly are more important than error messages.

    Look at this part of your code (BTW, good! you're using code tags!):
    Code:
            printf("Enter numbers\n"); 
             
             scanf("%lf",&num);
             
             while(num>0)
             {
                         
                         scanf("%lf", &num);
                         if(num>max)
                         
                         max=num;
                         
                         else if(num<min)
                         
                         min=num;
                         
                         else if (num>0)
                         printf("no positive value entered");
             }
    Try this experiment: make 120 the first number that you enter. What does your program report for the max now? Not 120. Why is that? Read your code to find out.

    First you call scanf and you get the first number, 120. Since num is greater than zero, you get into the while loop. What is the very first thing you do in that loop? You read the second number and you assign that to num, which throws away that first value of 120. Let's say that it's 6, so now 6 is the max. What you want to do is to perform those min/max test before you read in the second value. One way to do that is to move that second scanf from top of the loop to the bottom of the loop. IOW, instead of it being the first statement in the loop, make it the last statement. Do you see why you should do that?

    As your code is now, when you enter the negative number you do so at the top of the loop. Then you go through and perform all the min/max testing and, since a negative number is less than any possible positive number, min always gets set to that negative number. What you want to do is to perform those min/max tests only if the input number is positive. You could do that with an if statement. Or you could do that by moving the loop's scanf from the top of the loop to the bottom, in which case the while loop's test would be what would determine whether the min/max test is applied.

    Also I'm not sure on how to do so that it read the number of inputs. I have an Idea of doing a for loop for that but I am not sure of what to put in the part sum+.


    int sum=0

    for (i=1;i<=n;i++)
    {
    sum+=???
    }
    Not a separate for-loop, because what you are wanting to count will no longer be around. Rather, every time you read in a value, you need to increment sum, but only if that value is positive. If it's a negative value, you don't want to count that one, right?
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by dwise1_aol
    I realize that you're probably going into a kind of sensory overload with all the things to think about. You'll get there, but I'll still have to overload you. And thanks for spelling "bear" correctly; it bothers me when people ask me to get naked just because they are ("bare with me") [grin].

    You need to #include stdlib.h, because that's what system needs. You also should change the printf's from "%lf" to "%f", since that's what the other warnings are about.

    I really am very serious about not ignoring warnings. I lost count of all the threads started here asking for the solution of really crazy problems in "code that compiles cleanly", when in reality those programs threw warnings and those warnings pointed us directly to the problems. Warnings really and truly are more important than error messages.

    Look at this part of your code (BTW, good! you're using code tags!):
    Code:
            printf("Enter numbers\n"); 
             
             scanf("%lf",&num);
             
             while(num>0)
             {
                         
                         scanf("%lf", &num);
                         if(num>max)
                         
                         max=num;
                         
                         else if(num<min)
                         
                         min=num;
                         
                         else if (num>0)
                         printf("no positive value entered");
             }
    Try this experiment: make 120 the first number that you enter. What does your program report for the max now? Not 120. Why is that? Read your code to find out.

    First you call scanf and you get the first number, 120. Since num is greater than zero, you get into the while loop. What is the very first thing you do in that loop? You read the second number and you assign that to num, which throws away that first value of 120. Let's say that it's 6, so now 6 is the max. What you want to do is to perform those min/max test before you read in the second value. One way to do that is to move that second scanf from top of the loop to the bottom of the loop. IOW, instead of it being the first statement in the loop, make it the last statement. Do you see why you should do that?

    As your code is now, when you enter the negative number you do so at the top of the loop. Then you go through and perform all the min/max testing and, since a negative number is less than any possible positive number, min always gets set to that negative number. What you want to do is to perform those min/max tests only if the input number is positive. You could do that with an if statement. Or you could do that by moving the loop's scanf from the top of the loop to the bottom, in which case the while loop's test would be what would determine whether the min/max test is applied.


    Not a separate for-loop, because what you are wanting to count will no longer be around. Rather, every time you read in a value, you need to increment sum, but only if that value is positive. If it's a negative value, you don't want to count that one, right?
    Thank you so much, finally had some time to really sit down and fix all the problems you pointed out. The program runs perfectly. And thanks for not being one of those guys that just gives out the answer, specially since I'm the process of learning I appreciate that. Hopefully I won't make the same mistakes in the future. I'll keep this forum as a close ally for help in the future. Thanks!
  22. #12
  23. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    The philosophy of this forum is to help you learn rather than just do the work for you. So many beginners come here expecting us to do their homework for them and some of them get very upset when we won't play their game. Your attitude is a welcome contrast to them.

IMN logo majestic logo threadwatch logo seochat tools logo