Ordo
Ordo

Reputation: 705

Finding the smallest number in an array of integers

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.

Upvotes: 1

Views: 17415

Answers (5)

Shane
Shane

Reputation: 790

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();

Upvotes: 1

SivGo
SivGo

Reputation: 224

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.

Upvotes: 0

Jim Brissom
Jim Brissom

Reputation: 32919

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;
}

Upvotes: 8

DReJ
DReJ

Reputation: 1974

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

Upvotes: 0

sevenseacat
sevenseacat

Reputation: 25029

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.

Upvotes: 0

Related Questions