 # C++ Program to Find Smallest Element of an Array

October 5, 2021, Learn eTutorial
1108

In this C++ program, we will learn to access array elements and find out its Smallest element.

## What is an Array?

An array is a datatype defined in C++ which is used to store a collection of elements of the same data type in the continuous memory locations which can be accessed by an index variable. An array can be defined as

`dataType arrayName[arraySize];`

For example,
int x;
Here,
•    int - the type of element to be stored
•    x - the name of the array
•    6 - the size of the array.

## How do you find the Smallest number in an array?

Assume an array is with n number of elements. To find the Smallest element, check the first two elements of the array and store the Smallest of these two elements in the first index, arr. Then check the third element with the first and place the Smallest among these two in arr. Continue the process till the last element.

## How to implement calculating smallest array element logic in C++?

Take the number of elements from the user from 1 – 100 and read the value to the integer type variable n. Store the values entered by the user into the array by using a `for loop.`

Now the user entered n elements are stored within the array. We have to find the Smallest element among the entered elements. Declare another for loop to find the Smallest one and store the Smallest to arr.

This will access the elements from the array from its beginning. For performing the comparison make a conditional statement. If arr > arr [i] then store the Smallest one in arr. After completing the `for loop` the Smallest among the entered elements can be accessed from arr.

### Algorithm

Step 1: Call the header file `iostream.`

Step 2: Use the `namespace std.`

Step 3: Open the integer type main function; `int main().`

Step 4: Declare integer type variables i, n; and float type array arr;

Step 5: Ask the user to enter the total number of elements ( 1 to 100 ).

Step 6: Read the value to the variable n;

Step 7: Ask the user to enter the elements into the array.

Step 8: Read the entered elements into the array using a `for loop.`

Step 9: Acess the first two elements of the array.

Step 10: Compare the first two elements and store the smallest element in the array indices arr.

Step 11: Compare the third element and the element in arr and store the smallest on arr.

Step 12: Continue the process until the final element using a` for loop`.

Step 13: Get the smallest from the arr.

Step 14: Exit.

## C++ Source Code

```                                          ```#include <iostream>
using namespace std;

int main() {

int i, n;
float arr;

cout << "Enter total number of elements(1 to 100): ";
cin >> n;
cout << endl;

// Store number entered by the user
for(i = 0; i < n; ++i) {
cout << "Enter Number " << i + 1 << " : ";
cin >> arr[i];
}

// Loop to store smallest number to arr
for(i = 1;i < n; ++i) {

// Change > to < if you want to find the largestelement
if(arr > arr[i])
arr = arr[i];
}

cout << endl << "Smallest element = " << arr;

return 0;
}```
```

## OUTPUT

```Enter total number of elements(1 to 100): 5
Enter Number 1 : 6
Enter Number 2 : 8
Enter Number 3 : 2
Enter Number 4 : 0
Enter Number 5 : 7
Smallest element = 0```

VIEW ALL