Program to find Second Smallest number in Array in C Language

program-to-find-second-smallest-number-in-array-in-c-language

Program Description:

Write a program to find the Second Smallest number in Array in C programming language. The program should accept the size of the array and array elements from the user and find the smallest number in the array.

Example Input and Output:

Input:

Output:

Prerequisites:

It is recommended to have knowledge of the Arrays in C and functions in C language. So please go through the following articles to learn more about them

Second Smallest number in Array in C Program Explanation:

  1. First of all, Declare a numbers array, The numbers can hold 100 elements(Max). Take the size of the array from the user and update it in size variable.
  2. Prompt the user to provide the array elements. Update the array elements with provided user input
  3. Then, create two variables, smallest and second_smallest, and initialize them with the INT_MAX. ( Learn more about the INT_MAX at Size and Limits of datatypes in C)
  4. Iterate through all array elements
    • Update the second_smallest and smallest variables if you find a number that is smaller than the present smallest (i.e ( numbers[i] < smallest)). First, update the second_smallest with present smallest, Then update the smallestwith number[i].
    • If you find any number which is larger than smallest number and less than the second_smallest ( i.e (numbers[i] < second_smallest && numbers[i] > smallest)) Then update the second_smallest number with number[i].
  5. Once the above loop is completed, The smallest and second_smallest variables hold the Smallest and second smallest numbers correspondingly.
  6. Finally, display the smallestand second_smallest numbers on the console.

The Second Smallest number in Array in C Algorithm:

  1. Declare numbers array with 100 size.
  2. Take the desired array size from the user and update the size variable.
  3. Update the array with user input.
    • Iterate from 0 to size-1 and update &numbers[i] using scanf() function.
  4. Create smallest and second_smallest. Initialize them with INT_MAX.
  5. Iterate over array elements and update the smallest
    and second_smallest elements.
    • Iterate from zero(0) to ( size-1).
    • If numbers[i] < smallest, Then update second_smallest = smallest; and smallest = numbers[i];
    • else if(numbers[i] < second_smallest && numbers[i] > smallest), Then update the second_smallest = numbers[i];
  6. Print the smallestand second_smallestnumbers of the array using printf function.

Program to find Second Smallest number in Array in C:

📢 Note: As we are using the INT_MAX, make sure to include the limits.h header file

Here is the C program to find the smallest and second smallest numbers.

Program Output:

Let’s compile and Run the Program.

Compile the Program

gcc second_smallest.c

Run the program.

Test 1:

smallest-second-smallest-number-in-c-program-output

Test 2:

Second Smallest Number in Array using Functions in C Langauge:

Let’s rewrite the above program and divide the main() function into the sub-functions. Create a couple of functions. One function is to read the input from the user and another function is to calculate the second smallest number.

We have defined two functions in the above program.

  1. The read() function.
    • Prototype of the read() function – void read(int numbers[], int size);
    • The read() function takes two arguments – numbers array and its size. The read() function prompts the user for array elements and updates the number array.
  2. The get_second_smallest() function.
    • Prototype of the get_second_smallest() function – int get_second_smallest(int numbers[], int size);
    • The get_second_smallest() function also takes two formal arguments one is the input array numbers[] and the sizeof the number’s array.
    • The get_second_smallest() function finds the second smallest element in the array by comparing all elements.
    • Finally, It returns the second smallest element in the array back to the caller.

📢 Here we have returned only a single value from the get_second_smallest() function. If you want to return multiple values like smallest and second_smallest, Then create a static array and return it back to the caller.

Program Output:

Let’s compile and run the program.

second-smallest-number-in-array-using-functions

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] C Program to Find Second Smallest Element in Array […]

Leave a Reply