C++ Program to Find Smallest Element of an Array

Here in this program, we will learn to access array elements in C++ programming and find out the Smallest element within the given array.

What is an Array?

C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
Suppose a class has 27 students, and we need to store the grades of all of them. Instead of creating 27 separate variables, we can simply create an array.
`dataType arrayName[arraySize];`
For example,
int x[6];
Here,
•    int - the type of element to be stored
•    x - the name of the array
•    6 - the size of the array.

How to access elements in a C++ array?

In C++, each element in an array is associated with a number. The number is known as an array index. We can access elements of an array by using those indices.
array[index];

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[0]. Then check the third element with the first and place the Smallest among these two in arr[0]. Continue the process till the last element.

How to find the Smallest element of an array 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.`
Set the value of Initialization variable to 0 and check for the condition i and update i by an increment. Read the entering number to the array from the ith indices. now our loop will be like
```for ( i = ; i < n; ++i ) { cin >> arr [i] }```
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[0].
Set the value of Initialization variable i to 1 and check for the condition iand update i by an increment. This will access the elements from the array from its beginning. For performing the comparison make a conditional statement.
If  arr[0] > arr [i] then store the the Smallest one in arr[0]. After completing the `for loop` the Smallest among the entered elements can be accessed from arr[0].

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[100];

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[0].

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

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

Step 13: Get the smallest from the arr[0].

Step 14: Exit.

C++ Source Code

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

int main() {

int i, n;
float arr[100];

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[0]
for(i = 1;i < n; ++i) {

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

cout << endl << "Smallest element = " << arr[0];

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
VIEW ALL
VIEW ALL