Minimum element in an array in C++


Q.Write a C++ program to find minimum element in an array.

#include <iostream>     
using namespace std;
  
int main()
{  
    int a[100], size, i, min;
    cout << "Enter the number of elements in array: ";
    cin >> size;
    cout << "Enter the integers: ";
    for (i = 0; i < size; i++)
    {
        cin >> a[i];
    }
    min = a[0];
    for (i = 1; i < size; i++)
    {
        if (a[i] < min)
        {
            min  = a[i];
        }
    }
    cout << "Minimum element in an array is " << min;
    return 0;    
}  

Q. Write an algorithm to find minimum element in an array.

1. Start
2. Declare an array `a` with a maximum size (e.g., 100).
3. Declare integer variables `size`, `i`, and `min`.
4. Prompt the user to enter the number of elements and read the input into `size`.
5. Prompt the user to enter the integers.
6. For `i` from 0 to `size - 1`:
    - Read the element and store it in `a[i]`.
7. Initialize `min` with the value of the first element of the array `a[0]`.
8. For `i` from 1 to `size - 1`:
    - Compare `a[i]` with `min`.
    - If `a[i] < min`, update `min = a[i]`.
9. After completing the loop, `min` will hold the minimum element in the array.
10. Print the minimum element.
11. End 



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.