In coding, an array is a data arrangement holding a group of elements sharing a common data type. These elements are stored in adjacent memory locations, ensuring they are sequentially positioned in memory.
Arrays are frequently utilized for managing a collection of similar items, like a series of numbers, characters, or entities. Accessing individual elements within an array is achieved by referencing their specific index, indicating their position within the array. In the majority of programming languages, array indexing starts from 0, meaning the initial item is at index 0, the subsequent one at index 1, and so forth.
Example 1:
Array: {4, 2, 1, 5, 3}
- The smallest element is 1 while the second-smallest is 2.
- The function findSecondSmallest is called with this array, and it correctly finds 2 as the second smallest element.
- Output: "Example 1: The second smallest element in the array is: 2"
Example 2:
Array: {9, 7, 6, 4, 10}
- The lowest element is 4, while the second smallest is 6.
- The function findSecondSmallest is called with this array, and it properly finds 6 as the second smallest element.
- Output: "Example 2: The second smallest element in an array is: 6"
Pseudocode:
Function findSecondSmallest(array)
smallest = Infinity
secondSmallest = Infinity
For each element in the array
If element < smallest
secondSmallest = smallest
smallest = element
Else if element < secondSmallest and element ≠ smallest
secondSmallest = element
Return secondSmallest
This pseudocode describes the steps required to identify the second smallest entry in an array:
- First, initialize variables smallest and secondSmallest to positive infinity (or the highest value possible for your data type).
- Iterate through every element of the array to check for the elements.
- If the current element is less than the smallest, update both the smallest and secondSmallest.
- If the current element is less than secondSmallest but not equal to the smallest, update secondSmallest.
- After iterating over all components, return secondSmallest.
Example 1:
Let's consider an example to demonstrate the process of locating the second smallest item in an array using C++.
#include <iostream>
#include <limits>
int findSecondSmallest(int arr[], int size) {
int smallest = std::numeric_limits<int>::max(); // Initialize to maximum possible value
int secondSmallest = std::numeric_limits<int>::max(); // Initialize to maximum possible value
for (int i = 0; i < size; ++i) {
if (arr[i] < smallest) {
secondSmallest = smallest;
smallest = arr[i];
} else if (arr[i] < secondSmallest && arr[i] != smallest) {
secondSmallest = arr[i];
}
}
return secondSmallest;
}
int main() {
int arr[] = {4, 2, 1, 5, 3}; // Example array
int size = sizeof(arr) / sizeof(arr[0]);
int secondSmallest = findSecondSmallest(arr, size);
std::cout << "The second smallest element in the array is: " << secondSmallest << std::endl;
return 0;
}
Output:
Example 2:
Let's consider another scenario to demonstrate the process of identifying the second lowest element in an array using C++.
#include <iostream>
#include <limits>
int findScndsml(int a[], int s) {
int smlest = std::numeric_limits<int>::max(); // Initialize to maximum possible value
int scndsmlst = std::numeric_limits<int>::max(); // Initialize to maximum possible value
for (int i = 0; i < s; ++i) {
if (a[i] < smlest) {
scndsmlst = smlest;
smlest = a[i];
} else if (a[i] < scndsmlst && a[i] != smlest) {
scndsmlst = a[i];
}
}
return scndsmlst;
}
int main() {
int a1[] = {1, 2, 5, 4, 3}; // Example array 1
int s1 = sizeof(a1) / sizeof(a1[0]);
int scndsmlest1= findScndsml(a1, s1);
std::cout << "Example 1: The second smallest element in the array is: " << scndsmlest1 << std::endl;
int a2[] = {6, 7, 9, 5, 4}; // Example array 2
int s2 = sizeof(a2) / sizeof(a2[0]);
int scndsmlest2= findScndsml(a2, s2);
std::cout << "Example 2: The second smallest element in the array is: " << scndsmlest2 << std::endl;
return 0;
}
Output:
Example 3:
// C++ program to find the smallest and second smallest elements
#include <bits/stdc++.h>
using namespace std; /* For INT_MAX */
void print2S(int a[], int a_size)
{
int i, f, s;
/* There should be atleast two elements */
if (a_size < 2) {
cout << " Invalid Input ";
return;
}
f = s = INT_MAX;
for (i = 0; i < a_size; i++) {
/* If the present element is smaller than the first then update both the first and second elements in an array */
if (a[i] < f) {
s = f;
f = a[i];
}
/* If a[i] is in between the first and second elements of an array then update the second element */
else if (a[i] < s && a[i] != f)
s = a[i];
}
if (s == INT_MAX)
cout << "There is no second smallest element\n";
else
cout << "The smallest element is " << f
<< " and second "
"Smallest element is "
<< s << endl;
}
/* Driver code */
int main()
{
int a[] = { 111, 13, 25, 9, 34, 1 };
int n = sizeof(a) / sizeof(a[0]);
print2S(a, n);
return 0;
}
Output: