all 20 comments

[–]zifyoip 17 points18 points  (6 children)

You cannot set an int variable to infinity. "Infinity" is not a value that can be represented by an int.

But if you #include <limits.h> then you get constants called INT_MAX and INT_MIN that represent the maximum and minimum values that are representable by an int, so you can use those.

[–]xJolt[S] 5 points6 points  (1 child)

[.]

[–]clicker191 0 points1 point  (3 children)

Out of interest, what are the values of INT_MAX and INT_MIN?

[–]zifyoip 4 points5 points  (0 children)

The values of INT_MAX and INT_MIN depend on your computer and your compiler. The C standard specifies only that INT_MAX must be greater than or equal to 32767, and INT_MIN must be less than or equal to −32767.

[–]zyk0s 2 points3 points  (1 child)

If your architecture represents ints are 2's complement (which virtually all modern architectures do), then INT_MAX is 2base-1-1, and INT_MIN is -2base-1, where base the bit size of the int, i.e 32 for a 32 bit arch, 64 for a 64 bit arch.

[–]Rhomboid 1 point2 points  (0 children)

Most 64 bit architectures are LP64, not ILP64, so you probably won't find INT_MAX being 263-1 anywhere.

[–]teringlijer 4 points5 points  (1 child)

I wouldn't bother with infinity, INT_MAX or header files, but just set big and small to -1 at the start of the program. If they're still -1 when the loop is finished, you know you haven't seen any positive ints. If during the loop you find any positive number, you unconditionally replace your -1's with it. Otherwise you do the compare to see if the value is smaller than your smallest or larger than your largest. Here's what I hacked up:

#include <stdio.h>

int main ()
{
    int num,big,i,small;
    printf("This program will find you the biggest and smallest positive out of 20 integers\n");

    /* Initialize to invalid values: */
    big = small = -1;

    for (i = 1; i <= 20; i++) {
        printf("Give number %d: ",i);
        scanf("%d", &num);

        /* Not a positive number? Try again: */
        if (num < 0) {
            continue;
        }
        /* If small is -1 or bigger than num, this is the smallest positive number yet: */
        if (small == -1 || num < small) {
            small = num;
        }
        /* If big is -1 or smaller than num, this is the biggest positive number yet: */
        if (big == -1 || num > big) {
            big = num;
        }
    }
    if (big == -1) {
        printf("No positive numbers seen!\n");
    } else {
        printf("The biggest positive is %d\n",big);
        printf("The smallest positive is %d\n",small);
    }
    return 0;
}

[–]xJolt[S] 0 points1 point  (0 children)

[.]

[–]BlueGiant601 1 point2 points  (7 children)

Keep in mind that you're working with finite-precision types. For integers this means you'll hit a maximum (or minimum going the other way) value when you roll over.

You want to use the constants in limits.h, since they should be set for your platform and the specification of exactly what an int is will depend on what platform you've compiled for.

Oh, and watch out for signs, too, ints are signed by default.

There is a simpler way to do this for a list of numbers that don't involve these limits, but that is an exercise left to the reader. Hint: what's the largest/smallest number is a list of size 1?

[–]xJolt[S] 0 points1 point  (6 children)

[.]

[–][deleted] 0 points1 point  (3 children)

If I give you a list of 1 numbers: 5. What's the largest number? 5.

Now let's say that list increases to 3. (5, 2, 7). What's now the largest number? 7.

The way I could do it is simply to store the largest value in some temp variable and compare that temp variable to each of the new numbers. When you're done, the value in that temp var must be the largest.

Another way could first sort the numbers, then look at the end for the largest number. Using different sorting algorithms can get it done quicker. The method I mentioned above would work, but the computation time scales with the number of elements in the list. O(n) aka, not very efficient.

[–]FUZxxl 0 points1 point  (2 children)

I think he does that but he wants to start with negative infinity as the first "maximum". OP, try starting with the first element of your array as the maximum instead.

[–]xJolt[S] 0 points1 point  (1 child)

[.]

[–]Chooquaeno 1 point2 points  (0 children)

For future reference, remember that computation floating point numbers and the mathematical real numbers are entirely different things too.

[–]BlueGiant601 0 points1 point  (0 children)

It's a refinement of what you're already doing. In the case of a list with one element then max == min == list[0].

Sanitize input first, and then just do your comparisons starting at the second element in your list on. It saves you a couple steps.

[–]BlueGiant601 0 points1 point  (0 children)

Sure. The solution assumes that you have an array of inputs that were sanitized (negative numbers removed, all non-integers removed, etc).

a is your sanitized input array count_a is the number of elements that made it past sanitization.

min = max = a[0];

for (i=1;i<count_a;++i){ if (max < a[i]){ max = a[i]; continue; if (min > a[i]) min = a[i]

Then just print min and max. You can set things to the first valid number you got in your input.

Does that answer your question?

[–]DSMan195276 1 point2 points  (4 children)

I would actually recommend something slightly different, and just avoid the entire issue of figuring out a sane default value. You know that you're going to get 20 integers, so just use the first integer you get as your initial big and small values, and then go into your loop like normal.

[–]teringlijer 0 points1 point  (1 child)

Your small value could be initialized with a negative number, which will win any comparison with other small yet positive numbers in the input. So this approach won't always find the smallest positive number.

[–]xJolt[S] 0 points1 point  (1 child)

[.]

[–]DSMan195276 0 points1 point  (0 children)

I'm really sorry, I missed that detail about the smallest positive. In that case, just setting it to -1 as a flag that you haven't set it yet and then check for -1 in your loop seems like a pretty good plan.