Thread: Using limits.h

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    21

    Using limits.h

    I was writing this program and I need to make it print out the minimum input, the maximum input, as well as the average. I've got it to print the average, but I don't know how to use limits.h to print out the min and max inputs.

    This is what I have so far....
    Code:
    /*
     * This program prints the smallest number, the largest number, 
     * and the average of all the numbers.
     */
    #include <stdio.h>
    #include <limits.h>
    
    int main(void) {
    
            int input;
            int total;
            int avg;
            int count;
    
            total = 0;
            count = 0;
    
            printf("enter some numbers, and then anything else to the end:\n");
    
            while (scanf("%d", &input) == 1) {
                    total += input;
                    count = ++count;
                    printf("%d\n", input);
            }
                
            avg = total/count;
              
            printf("smallest value is %d\n", INT_MIN);
            printf("largest value is %d\n", INT_MAX);
            printf("total is %d\n", total);
            printf("count is %d\n", count); 
            printf("avg is %d\n", avg);
    
           
    }
    How would I give values to INT_MIN and INT_MAX?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't give values to INT_MIN and INT_MAX. They are already defined as the smallest and largest storable int.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    21
    so how would i be able to print out the smallest and largest inputs?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    By scanning through the array (I don't mean scanf, but searching) and keeping the lowest/highest number found.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    21
    honestly im not sure how to do that

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    21
    does anyone know how i can make this happen?

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Yes.

    Have a variable called 'min' or something, set to the first element. If any element is less than 'min' then set min to that element.

    Same goes for max.

  8. #8
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Why not use something in ur while, like prob ur looking for three numbers then do, while != 3 so u get the three inputs, and then use and if and else statements to look for the highest and lowest numbers
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  9. #9
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by tabstop View Post
    By scanning through the array (I don't mean scanf, but searching) and keeping the lowest/highest number found.
    What array are u talking about though, i dnt see an array, prob she just wnts to use the ints and find the smallest and lowest, my mistake i see they are using one variable to find it, yea array makes sense
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by Matus View Post
    What array are u talking about though, i dnt see an array, prob she just wnts to use the ints and find the smallest and lowest, my mistake i see they are using one variable to find it, yea array makes sense
    The OP can only average something if he keeps track of all the values to begin with. Thus he would need to utilize some sort of array.

    Example:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
     
    #define SCALE  0x1000
     
    int main(void)
    {
      size_t size = SCALE, index = 0;
      int *values, value;
     
      if(!(values = malloc(size * sizeof(*values))))
        return EXIT_FAILURE;
     
      while(scanf("%d", &value) == 1)
      {
        values[index] = value;
     
        if(++index > size)
        {
          void *tmp;
     
          size += SCALE;
          if(!(tmp = realloc(values, size * sizeof(*values))))
          {
            fputs("Oh no! There is no memory!", stderr);
            free(values);
            return EXIT_FAILURE;
          }
     
          values = tmp;
        }  
      }
     
      // Now we have a big array of values to work with.
     
      free(values);
      return EXIT_SUCCESS;
    }

  11. #11
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Yea Matt i understand, cept s/he hasnt declared an array as yet to keep track of em. Not really specified, so was a bit confused in the end.
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    For auxilery help to the above code that I wrote that the OP may find useful.

    Code:
    /*
     * If you are using the above code I wrote
     * be sure to pass in "index" not "size"
     */
    int average(int *array, size_t size)
    {
      int cpy = (int)size;
      int avg = 0;
    
      for(;--size;array++)
        avg += *array;
    
      return avg / cpy; 
    }

  13. #13
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Is Cleepy a girl or a guy? My compassion goes more for the girls, lol , id help more
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    21
    well i'm not exactly working with arrays at all, i haven't actually learned that. when i run through the code it executes and asks me for numbers. i can input as many numbers as i want, there is no limit. and from those inputs i got the while loop to add them and take the average by dividing the total by the int count. what i'm trying to do is take from the inputs, the lowest inputted number and the highest.

  15. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    21
    ohh and the printf in the while loop is just there to help me see what's going on in the loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. limits.h problem!!
    By guitarist809 in forum C++ Programming
    Replies: 21
    Last Post: 04-13-2006, 10:28 PM
  2. limits.h
    By kostas in forum C Programming
    Replies: 10
    Last Post: 03-19-2002, 06:36 PM
  3. need header files (limits.h and float.h)
    By Sapphire19 in forum C Programming
    Replies: 3
    Last Post: 11-06-2001, 05:59 PM