C Program To Find Smallest Element in An Array using Pointers

Write a c program using pointers to find the smallest number in an array of 25 integers.

Pointers: A pointer variable is a variable which holds the address of another variable, of its own type.

Important Note:
1. Array elements are always stored in contiguous memory location.
2. A pointer when incremented always points to an immediately next location of its own type.

Related Read:
C Program To Find Smallest Element In An Array
Basics of Pointers In C Programming Language
Introduction To Arrays: C Programming Language
C Program To Find Smallest Element in An Array using Recursion

Important Video Tutorial
C Programming: Arrays, Pointers and Functions

Example: Expected Output

Enter 5 integer numbers
1
5
5
0
2
Smallest Element In The Array: 0

Visual Representation

Smallest Element In An Array using Pointers

Video Tutorial: C Program To Find Smallest Element in An Array using Pointers


[youtube https://www.youtube.com/watch?v=bTDqhyaxPtE]

YouTube Link: https://www.youtube.com/watch?v=bTDqhyaxPtE [Watch the Video In Full Screen.]

Source Code: C Program To Find Smallest Element in An Array using Pointers

#include<stdio.h>

#define N 5

int main()
{
    int a[N], i, *small;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    small = &a[0];

    for(i = 1; i < N; i++)
    {
        if( *(a + i) < *small)
            *small = *(a + i);
    }

    printf("Smallest Element In The Array: %d\n", *small);

    return 0;
}

Output:
Enter 5 integer numbers
5
2
6
4
3
Smallest Element In The Array: 2

Here we are assigning base address to pointer variable small.

Logic To Find Smallest Element In An Array using Pointers

We ask the user to input N integer numbers and store it inside a[N]. Next we assign base address to pointer variable small. Next we iterate through the array elements one by one using a for loop, and check if any of the elements of the array is smaller than whatever the value present at *small. If there is any element smaller than *small, we assign that value to *small.

Once the control exits the for loop, we print the value present in pointer variable *small, which holds the smallest element in the array.

Initializing *small to last elements address

#include<stdio.h>

#define N 5

int main()
{
    int a[N], i, *small;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    small = &a[N - 1];

    for(i = 0; i < N - 1; i++)
    {
        if( *(a + i) < *small)
            *small = *(a + i);
    }

    printf("Smallest Element In The Array: %d\n", *small);

    return 0;
}

Output:
Enter 5 integer numbers
5
2
6
4
3
Smallest Element In The Array: 2

Here the logic is same, but we are assigning the address of last element of the array to pointer variable small.

Here we are simply making use of array variable for comparison

#include<stdio.h>

#define N 5

int main()
{
    int a[N], i, *small;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    small = &a[0];

    for(i = 1; i < N; i++)
    {
        if( a[i] < *small)
            *small = a[i];
    }

    printf("Smallest Element In The Array: %d\n", *small);

    return 0;
}

Output:
Enter 5 integer numbers
2
1
3
4
5
Smallest Element In The Array: 1

As you can see in the above source code we are using array variable in if condition. But compiler converts a[i] to *(a + i). So internall a[i] is denoted as *(a + i) – which is *(base address + index).

Explanation With Example

N = 5;
a[N] = {5, 4, 6, 2, 3};
small = &a[0];

    small = &a[0];

    for(i = 1; i < N; i++)
    {
        if( *(a + i) < *small)
            *small = *(a + i);
    }

Here index variable i is initialized to 1. That is because pointer variable small has base address i.e., address of first element of the array. So we need not compare the value with itself. So we skip comparing it with a[0], and start the comparison from next index, which is 1.

i*(a + i) < *small*small
14 < 54
26 < 44
32 < 42
43 < 22

Smallest Element In The Array: 2

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find Smallest Element in An Array using Recursion

Write a C program to find smallest element / number in an array using pointers and recursion.

We have covered both these logic in this video tutorial
1. Recursive function with no return type.
2. Recursive function with return type.

Related Read:
C Program To Find Smallest Element In An Array
Recursive Functions In C Programming Language
Basics of Pointers In C Programming Language
Introduction To Arrays: C Programming Language

Important Video Tutorial
C Programming: Arrays, Pointers and Functions

Example: Expected Output

Enter 5 integer numbers
5
2
6
4
3
Smallest Element In The Array: 2

Visual Representation

Smallest Element In An Array using Recursion

Video Tutorial: C Program To Find Smallest Element in An Array using Recursion


[youtube https://www.youtube.com/watch?v=sGGxml1TCBI]

YouTube Link: https://www.youtube.com/watch?v=sGGxml1TCBI [Watch the Video In Full Screen.]

Source Code: C Program To Find Smallest Element in An Array using Recursion

Method 1: With No Return Type

#include<stdio.h>

#define N 5

void smallest(int *num, int n, int small)
{
    if(n < 0)
        printf("Smallest Element In The Array: %d\n", small);
    else
    {
        if(small > *num)
            small = *num;

        smallest(++num, --n, small);
    }
}

int main()
{
    int a[N], i;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    smallest(a, N - 1, a[0]);

    return 0;
}

Output:
Enter 5 integer numbers
2
1
3
4
5
Smallest Element In The Array: 1

Logic To Find Smallest Element In An Array using Recursion

We ask the user to enter N integer numbers and store it inside array variable a[N]. We pass base address(address of first element in the array) of the array, which is present in &a[0] or a, and last index of the array(indicating size of the array, from index 0), and first element of the array(assuming first element itself as smallest element).

Inside Recursive function
Base Condition: This is the condition to terminate the recursive call. Here we check if the size of the array is less than zero. If it’s less than zero, then we display the value present inside variable small, which holds the smallest element in the array.

void smallest(int *num, int n, int small)
{
    if(n < 0)
        printf("Smallest Element In The Array: %d\n", small);
    else
    {
        if(small > *num)
            small = *num;

        smallest(++num, --n, small);
    }
}

We need to take a pointer variable to accept the base address. Next we’ll have base condition. If base condition isn’t met – we check if the value present in variable small is greater than value present at *num. If it’s true, then we transfer the value of *num to small. Next we increment the address of num by 1 and decrement the value of n by 1 and pass them to the same function(recursive call) along with the value of small. This is called recursive function call.

Once value of n is less than 0, we display the value of variable small, which holds the smallest element of the array.

Important Note:

1. Array elements are always stored in contiguous memory location.
2. A pointer when incremented always points to an immediately next location of its own type.

Source Code: Find Smallest Element of An Array using Recursion: With Return Value

Method 2: With Return Type

#include<stdio.h>

#define N 5

int smallest(int num[], int n, int small)
{
    if(n < 1)
        return small;
    else
    {
        if(num[n] < small)
            small = num[n];

        return smallest(num, --n, small);
    }
}

int main()
{
    int a[N], i;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    printf("Smallest Element In The Array: %d\n", smallest(a, N - 1, a[0]));

    return 0;
}

Output:
Enter 5 integer numbers
2
1
0
3
5
Smallest Element In The Array: 0

After repeatedly incrementing value of num and decrementing the value n, we’ll reach a point where value of n will be less than 0. That’s when all the comparisons end, and variable small will have smallest element of the array. This result will be returned to the calling function, which in turn returns the result to the calling function and so on ..until the result is returned to the first function call, which was from with in main method – where we print the value of variable small.

Source Code: Using Array Variable In Recursive Function

Find Smallest Element of An Array using Recursion

#include<stdio.h>

#define N 5

void smallest(int num[], int n, int small)
{
    if(n < 0)
        printf("Smallest Element In The Array: %d\n", small);
    else
    {
        if(small > num[n])
            small = num[n];

        smallest(num, --n, small);
    }
}

int main()
{
    int a[N], i;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    smallest(a, N - 1, a[0]);

    return 0;
}

Output:
Enter 5 integer numbers
1
0
2
-5
4
Smallest Element In The Array: -5

Here we are taking array variable to receive the base address. We keep checking if num[n] is smaller than value present at variable small. If true, then we transfer num[n] value to variable small, and then recursively call the same function by decrementing the value of n by 1, and also pass the new value of small.

Once the value of n is less than 0, we return the value present in variable small, which holds the smallest element of the array.

Explanation With Example

N = 5;
a[N] = {5, 2, 6, 4, 3};
n = N – 1 = 5 – 1 = 4;
num = a[0] = 5;
big = a[0] = 5;

smallest(num, --n, small);
nnum[n]small
433
343
263
122
052
-12

Smallest Element in the array: 2

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find Smallest Element In An Array

Lets write a C program to find smallest element in an array, without sorting the elements. And also print the position at which the smallest number is present in the array.

Related Read:
C Program To Find Biggest Element of An Array

Example: Expected Output

Enter 5 integer numbers
5
2
6
4
3

Biggest of 5 numbers is 2, at position 2.
array with size 5

Video Tutorial: C Program To Find Smallest Element In An Array


[youtube https://www.youtube.com/watch?v=0lnvlxKPD3A]

YouTube Link: https://www.youtube.com/watch?v=0lnvlxKPD3A [Watch the Video In Full Screen.]

Source Code: C Program To Find Smallest Element of An Array

Method 1

#include<stdio.h>

#define N 5

int main()
{
    int a[N], i, small, pos;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);

        if(i == 0 || small > a[i])
        {
            small = a[i];
            pos   = i + 1;
        }
    }

    printf("Smallest Number: %d, at position %d.\n", small, pos);

    return 0;
}

Output 1:
Enter 5 integer numbers
9
8
6
2
5
Smallest Number: 2, at position 4.

Output 2:
Enter 5 integer numbers
5
6
1
3
2
Smallest Number: 1, at position 3.

Logic To Find Smallest Element In An Array

We ask the user to input N integer numbers. N being Macro, used to assign array size. In above source code N is assigned a value of 5. So user enters 5 integer numbers. While the user inputs numbers we check if it’s the first number input by the user. If its the first number/element, then we assign that first number/element entered by the user to variable small and assign 1 to variable pos, indicating that its the first element in the array.

Next, for each consecutive iteration of the for loop we check if the new value entered by the user is smaller than the value present in variable small. If it’s true, then we assign the new value entered by the user to variable small and also update the value of pos accordingly.

Once i < N condition is false, control exits for loop and we print the value present inside variable small and pos which will have smallest of N numbers or the smallest element of the array and position of that element in the array.

Explanation With Example

If int a[5] = {5, 2, 6, 4, 3};

    for(i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);

        if(i == 0 || small > a[i])
        {
            small = a[i];
            pos   = i + 1;
        }
    }
indexa[index]smallpos = index + 1
0a[0] = 551
1a[1] = 222
2a[2] = 6
3a[3] = 4
4a[4] = 3
5

a[5] = {5, 2, 6, 4, 3}
small = 2;
pos = 2;

Source Code: C Program To Find Smallest Element of An Array

Method 2

 #include<stdio.h>

#define N 5

int main()
{
    int a[N], i, small, pos;

    printf("Enter %d integer numbers\n", N);

    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    small = a[0];
    pos   = 1;

    for(i = 1; i < N ; i++)
    {
        if(small > a[i])
        {
            small = a[i];
            pos   = i + 1;
        }
    }

    printf("Smallest Number: %d, at position %d.\n", small, pos);

    return 0;
}

Output 1:
Enter 5 integer numbers
9
8
6
2
5
Smallest Number: 2, at position 4.

Output 2:
Enter 5 integer numbers
5
2
6
4
3
Smallest Number: 2, at position 2.

Method 2: LOGIC

Here we accept N integer numbers from the user. Next we assign the first element of the array to variable small and 1 to pos. Now we use another for loop to loop through the array.

Inside for loop
Inside for loop we check if the value present inside variable small is greater or bigger than the value present at a[i]. If it’s true, then we assign the value of a[i] to small and value of (i+1) to variable pos.

At the end of for loop, variable small and pos will have smallest element of the array and the position at which this number is present in the array.

Note: Since we assign the value of first element of the array(which is present at location a[0]) to variable small, while comparing with other elements of the array, we start comparing from a[1] till a[N-1]. That’s the reason we’ve assigned initial value of i to 1 in the second for loop.

Since a[0] is assigned to variable small, there is no point in comparing big with a[0]. So we start comparison from a[1].

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert