1

I have written a small program which takes 5 numbers from the user and stores them in an array of integers. The array is passed to a function. The function is used to find the smallest number in the array and print it out. Sincerly the output is not correct and i don't know why. The function always prints out the first element of the array which should be the smallest number but it's not.

#include <stdio.h>

void smallestint (int intarray [], int n)

{
    int i;
    int temp1 = 0;
    int temp2 = 0;

    for(i = 0; i < n; i ++)
    {

        if (intarray [i] < temp1)
        {
            intarray [i-1] = intarray [i];
            temp2 = intarray[i];
            intarray[i] = temp1;
            temp1 = temp2;
        }
        else 
            temp1 = intarray[i];
    }

    printf("%d\n", intarray[0]);
}

int main ()

{
    const int n = 5;
    int temp = 0;
    int i;
    int intarray [n];

    printf("Please type in your numbers!\n");

    for(i = 0; i < n; i ++)
    {
        printf("");
            scanf("%d", &temp);         
        intarray[i] = temp;

    }

    smallestint (intarray, n);


    getchar();
    getchar();
    return 0;
}


I have updated my code. Now I'm initializing the temp values before the for loop. But it's still not working.

3
  • Is there any reason why you made things more complicated than they need to be, i.e. shuffling the contents of the array around? Also, using zero as an initializer does not sound like a very good idea. See limits.h. Commented Jan 17, 2011 at 7:15
  • I'm confused by your algorithm. Just compare every number against the previous one, storing the smaller value. Use INT_MAX as the initial smallest value. And don't shuffle the array! Commented Jan 17, 2011 at 7:17
  • Or use intarray[0] as your initial value (but make sure that (n > 0) first). Commented Jan 17, 2011 at 7:31

5 Answers 5

8

If you simply want to print out the smallest element of an array, this is about the most basic way to do it:

#include <limits.h>
#include <stdio.h>

int smallest(int* values, int count)
{
        int smallest_value = INT_MAX;
        int ii = 0;
        for (; ii < count; ++ii)
        {
                if (values[ii] < smallest_value)
                {
                        smallest_value = values[ii];
                }
        }
        return smallest_value;
}

int main()
{
        int values[] = {13, -8, 237, 0, -3, -1, 15, 23, 42};
        printf("Smallest value: %d\n", smallest(values, sizeof(values)/sizeof(int)));
        return 0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Pretty much exactly what I was alluding to, minus the INT_MAX part.
1

The least amount of code will have to be using LINQ:

var example_arr = new [] {3,49, 12, 11, 78, 1};
var smallest = example_arr.Select(t=>t).Concat(new[]{INT_MAX}).Min();

Comments

0

You're re-initializing your temp variable on every iteration of the loop.

You should be storing the current smallest number outside the loop (initialized to the first element of the array), and checking against that.

2 Comments

I thought that was the only problem too, but moving the initialization outside the loop still does not work.
It does seem to be very overcomplicated with all the shifting array elements, too. You just need to loop over the array once, checking each value against your predefined 'min' value, which should default to intArray[0], and if smaller, set min to intArray[i]. My syntax might be slightly off, it's been a few years since I've done any C.
0

temp1 variable must be initialized with VERY big value (for example, INT_MAX) outside loop.

Comments

0

if you only want to return the smallest number - don't bother sorting the array.

anyway,

  • in the first iteration you place the first element of the array in index -1 (which is "legal" in C - but it's not what you want to do...) : intarray [i-1] = intarray [i]. you should start your loop from 1.

  • you run over the value of temp1 on every iteration (since you initialize it to 0 inside the loop). I suggest you'll initialize it outside the loop.

BTW, if you will initialize the variable temp1 before the loop to be intarry[0] (instead of 0) you will be able to support negative numbers as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.