Question 01 of 02

Find Largest, Smallest, and Linear Search in 1D Array

Write a Turbo C++ program to read N elements into a 1D array. Find and display the largest element, the smallest element, and search for a key element using Linear Search.

C++
#include <iostream.h>
#include <conio.h>

void main() {
    int a[50], n, i, largest, smallest, key, found = 0, pos = -1;

    clrscr();

    cout << "Enter number of elements (max 50): ";
    cin >> n;

    cout << "Enter " << n << " integers:" << endl;
    for (i = 0; i < n; i++) {
        cout << "Element [" << i << "]: ";
        cin >> a[i];
    }

    largest = a[0];
    smallest = a[0];

    for (i = 1; i < n; i++) {
        if (a[i] > largest)
            largest = a[i];
        if (a[i] < smallest)
            smallest = a[i];
    }

    cout << "\n--- Array Statistics ---" << endl;
    cout << "Largest Element  = " << largest << endl;
    cout << "Smallest Element = " << smallest << endl;

    cout << "\nEnter element to search: ";
    cin >> key;

    for (i = 0; i < n; i++) {
        if (a[i] == key) {
            found = 1;
            pos = i;
            break;
        }
    }

    if (found)
        cout << "Element " << key << " FOUND at index position " << pos << " (Element #" << (pos + 1) << ")." << endl;
    else
        cout << "Element " << key << " NOT FOUND in the array." << endl;

    getch();
}
Question 1 of 2