×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous Programs

C program to find two smallest elements in a one dimensional array

In this article we are going to implement a C program to find two smallest elements in a one dimensional array.
Submitted by Radib Kar, on December 10, 2018

Problem statement

Write a C program to find two smallest elements in a one dimensional array.

Example: Type1: (all the elements are not same & no of element is more than two)

Array size: 4
Elements: 32 54 -6 43
Output: -6 32

Example: Type2: (second minimum doesn’t exist as size of array < 2)

Array size : 1
Elements: 4
Output: 4

Example: Type3: (all elements are same, thus only one smallest element)

Array size : 4
Elements: 13 13 13 13
Output: 13

Algorithm

  1. Define two variable min & sec_min
  2. Initialize min to array[0] & sec_min to INT_MAX
  3. Scan the entire array
  4. For(int i=0;i<n;i++)
        if array[i]<min
            then update min to array[i]&sec_min to previousmin value
        else if array[i] is lesser than sec_min but greater than min
            then update sec_min to array value
            do nothing to min
    End for loop
    
  5. If still sec_min has the value INT_MAX
    Only one smallest value exists, print it
    Else
    Print min & sec_min

C implementation to find two smallest elements in a one dimensional array

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

void findTwoMin(int* a, int n) {
  int min = a[0];                   // initialize min to first array element
  int sec_min = INT_MAX, temp = 0;  // initialize second min to INT_MAX

  for (int i = 0; i < n; i++) {  // scan the entire array
    // if a[i]< min then update min to array value
    //& second min to previous min value
    if (a[i] < min) {
      sec_min = min;
      min = a[i];
    }
    // else if a[i] is lesser than second min but
    // greater than min then update second min to array value
    else if (a[i] < sec_min && a[i] > min)
      sec_min = a[i];
    // do nothing to min
  }

  // if second min is still at its initialized value
  // then no second minimum exists at all
  if (sec_min == INT_MAX)
    printf("only one smallest element: %d ", min);
  else
    printf("First smallest element: %d  & second smallest element : %d", min,
           sec_min);
}

int main() {
  int n;

  printf("enter no of elements\n");
  scanf("%d", &n);

  // dynamic array created
  int* a = (int*)malloc(sizeof(int) * n);

  printf("enter the elements........\n");
  // taking input
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);

  // find two smallest no
  findTwoMin(a, n);

  return 0;
}

Output (Type1 example)

enter no of elements
4
enter the elements........
32 54 -6 43
First smallest element: -6  & second smallest element : 32

Output (Type2 example)

enter no of elements
1
enter the elements........
4
only one smallest element: 4 

Output (Type3 example)

enter no of elements
4
enter the elements........
13 13 13 13
only one smallest element: 13

C One-Dimensional Array Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.