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 Largest Difference Between Two Elements of Array

Write a C program to find largest / maximum difference between two elements of an array, such that larger element or number appears after the smaller number in the array.

Note: I’m not considering time complexity in this video tutorial intentionally. We’ll have a completely dedicated video teaching about time complexity from basic. My intention in this video is to make the logic as simple and understandable as possible.

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

Example: Expected Output

Enter 6 integer numbers
7
9
5
6
13
2
The largest difference is 8, and its between 13 and 5.

Visual Representation

Largest Difference Between Two Elements of Array

Video Tutorial: C Program To Find Largest Difference Between Two Elements of Array


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

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

Source Code: C Program To Find Largest Difference Between Two Elements of Array

#include<stdio.h>

#define N 6

int main()
{
    int num[N], i, big, small;

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

    big = small = num[0];

    for(i = 1; i < N; i++)
    {
        if(num[i] > big)
            big = num[i];

        if(num[i] < small)
            small = num[i];
    }

    printf("The largest difference is %d, ", (big - small));
    printf("and its between %d and %d.\n", big, small);

    return 0;
}

Output:
Enter 6 integer numbers
7
9
5
6
13
2
The largest difference is 11, and its between 13 and 2.

Here we find biggest element in the array and smallest element in the array. Next we subtract smallest element from biggest element to get the largest different between two array elements.

Find Largest Difference, where Largest Element Appears After Smallest Number in Array

#include<stdio.h>

#define N 6

int main()
{
    int num[N], i, big, small, pos = 0;

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

    big = small = num[0];

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

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

    printf("The largest difference is %d, ", (big - small));
    printf("and its between %d and %d.\n", big, small);

    return 0;
}

Output:
Enter 6 integer numbers
7
9
5
6
13
2
The largest difference is 8, and its between 13 and 5.

Logic To Find Largest Difference b/w Two Elements of Array, where biggest number appears after the smallest number

As per the problem statement, the smallest number in the array must be chosen from index 0 to the position where the biggest number of the array is present.

For Example: Assume that we’ve an array {1, 2, 5, 3, 0}. Here biggest element in the array is 5 and its position in the array is 2. Now we need to select smallest number in the array between the index range 0 to 2. So the smallest number will be 1.

Step 1: First we iterate through the array using a for loop and find the biggest element in the array and we also determine the index position at which this biggest number is present.

Step 2: We write another for loop and iterate from index 1 to the position where the biggest number is present. And inside for loop we select the smallest element between the range.

Step 3: Now we subtract the smallest element from step 2 with the biggest element from step 1, and we get the largest difference between two elements of an array, where biggest number appears after the smaller number in the array.

Note:
1. We initialize variables big and small to the first element of the array, because we need to have some value to compare it with other elements of the array.
2. In both the for loops we initialize i to 1, as both variables big and small is assigned with value present at index 0. So there is no point in comparing with itself, so we start the comparison from index 1. That’s the reason we initialize i to 1 in both the for loops.

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