Thread: Mean, Min, Max of N numbers without using array

  1. #1
    Registered User
    Join Date
    Feb 2012
    Location
    Fresno, California, United States
    Posts
    6

    Mean, Min, Max of N numbers without using array

    I am not sure how to determine a min and a max with my program. it finds the average but is there a way i can also get it to print a min and max. here what i have so far

    Code:
    int value;
    int n;
    int count;
    int input_status;
    float sum = 0, x, avg;
    
    printf("\nEnter How Many Numbers : ");
    scanf("%d", &n);
    
            for(count = 1; count <= n; count++)
            {
                    printf("x = ");
                    scanf("%f", &x);
                    sum += x;
            }
    
            avg = sum / n;
    printf("\nThe Average of Numbers is : %0.2f", avg);

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Create a min variable and initialize it to the largest value possible for the type, create a max variable and initialize it to the smallest value possible for the type. Within the loop compare the values read in from the user to the values in these min/max variables and update as needed... if value from user is less than current min then update min, if value from user is more than current max then update max.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by hk_mp5kpdw View Post
    Create a min variable and initialize it to the largest value possible for the type, create a max variable and initialize it to the smallest value possible for the type.
    ..or set both to the first element the user inputs.

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Code:
    if (count == 1)
    {
       min = x;
       max = x;
    }
    else if (x < min)
      min = x;
    else if (x > max)
      max = x;
    et voila...

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Oh and btw, it's just a little detail. But it'd be better if you start your counter at 0 and let it loop while counter < n, everything in IT starts with 0...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-10-2012, 01:14 AM
  2. Replies: 2
    Last Post: 12-07-2011, 11:22 PM
  3. Can anyone help me to break numbers into an array numbers
    By s2xcracker in forum C++ Programming
    Replies: 4
    Last Post: 08-25-2009, 05:17 PM
  4. Numbers in an array
    By legacye in forum C Programming
    Replies: 2
    Last Post: 11-23-2003, 05:53 AM
  5. Numbers into an array
    By Guti14 in forum C++ Programming
    Replies: 12
    Last Post: 09-09-2003, 09:16 PM