Thread: How to correct code to find the largest and smallest #

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    33

    How to correct code to find the largest and smallest #

    So I'm needing to figure out how to find the largest and smallest number entered by a user. So far I'm able to add, find the average and count how many numbers are entered.
    I'm just having trouble with find the max and min. I tried if statements and it breaks the program will it wont let me exit the while loop or the program will do a force close after entering a value.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(){
    
    
        int maxNum=0, minNum=0, value, count=0, sum=0;
        double average;
    
    
        printf("To exit the program enter -1\n");
        while(value!=-1){
            printf("enter a value\n");
            scanf("%d", &value);
            count++;
            sum = sum + value;
        }
        count= count-1;
        sum= sum+1;
        average = (double)(sum/count);
        printf("number of entries is %d and sum is %d\n", count, sum);
        printf("the average is %.2lf\n", average);
        printf("min is %d and max is %d", minNum, maxNum);
        }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Show us what you've tried with regard to finding the min/max, and we can try to help you fix it.

    "value" is uninitialized the first time you check its value (line 13).

    Code:
    average = (double)(sum/count);
    Here, you're casting the result of integer division (i.e. any fractional part has already been truncated before the cast is applied). Just cast one of the variables instead.

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    33
    what do you mean by uninitialized?
    The code I posted, works fine and no errors

    Here is what I tried with the If statements

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    int main(){
    
    
        int maxNum=0, minNum=0, value, count=0, sum=0;
        double average;
    
    
        printf("To exit the program enter -1\n");
        while(value!=-1){
            printf("enter a value\n");
            scanf("%d", &value);
            count++;
            sum = sum + value;
    
    
            if(value > 0){
                value = minNum;
                value = maxNum;
            }
            if(value > maxNum)
                value = maxNum;
            if(value < minNum)
                value = minNum;
        }
        count= count-1;
        sum= sum+1;
        average = (double)(sum/count);
        printf("number of entries is %d and sum is %d\n", count, sum);
        printf("the average is %.2lf\n", average);
        printf("min is %d and max is %d", minNum, maxNum);
        }
    so youre saying I can just make sum as a double and then I wont need to multiply by the "double"

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by tlxxxsracer View Post
    what do you mean by uninitialized?
    The code I posted, works fine and no errors
    Value is not initialized when you check it's value with line 13. You never gave it a value when you declared it (look at line 7). You are relying on undefined behavior. This code is bound to break. Take the advice you are given and you should do it graciously.

    Plus you should look at how you are supposed to define main
    Last edited by AndrewHunter; 03-13-2015 at 09:39 AM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    so youre saying I can just make sum as a double and then I wont need to multiply by the "double"
    I don't know if you understand what's going on there. There's no "multiply by double" - that is a cast; that is, it converts one type to another.

    Code:
    average = (double)(sum/count);
    So what this is doing is:
    - dividing "sum" by "count" (both integers, so the result is an integer)
    - casting that result to a double

    So if you have, for instance, 5/2, the division yields 2, and the cast changes it to 2.0 (which is wrong).

    If you cast one of the operands to a double, then the result of the division will be a double.

    Code:
    average = (double)sum/count;
    So now, it:
    - treats the int "sum" as a double
    - divides "sum" (seen as a double) by "count".

    Using the same example, you would have 5.0/2, which yields a double result, 2.5



    Your min/max logic is pretty close, but not there yet. For starters, you're overwriting "value" with "minVal"/"maxVal". You have the assignments backwards.

    There are neater ways of doing it, but you seem to be on the right track. The other problem is that the sentinal value (-1) is included in the logic, which you probably don't want.

    You got around this for "count" and "sum" by decrementing/incrementing (respectively) those variables after the loop, but this is a hack that probably won't help much for the min/max portion of the assignment.

    If instead you skipped the logic in your loop with an input of -1, then the min/max would work as expected and you wouldn't have to "correct" count and sum after the loop.

    Thinking carefully about the logic, and planning it well, can result in a much more elegant solution.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 55
    Last Post: 12-14-2011, 03:58 AM
  2. Replies: 4
    Last Post: 04-14-2010, 11:42 AM
  3. Replies: 22
    Last Post: 05-29-2009, 05:44 PM
  4. Find the Largest and Smallest Number
    By Nightsky in forum C Programming
    Replies: 27
    Last Post: 09-04-2006, 03:40 PM
  5. Replies: 2
    Last Post: 09-05-2004, 07:13 AM