Dremendo Tag Line

Input 10 numbers in 1d array and find largest and the smallest number position in C++

One Dimensional Array - Question 3

In this question, we will see how to input 10 numbers in a one dimensional integer array and find the position of the largest and the smallest number in C++ programming. To know more about one dimensional array click on the one dimensional array lesson.

Q3) Write a program in C++ to input 10 numbers in a one dimensional integer array and find the position of the largest and the smallest number.

Program

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

using namespace std;

int main()
{
    int a[10], i,ln,lnp,sn,snp;

    cout<<"Enter 10 numbers\n";
    for(i=0; i<10; i++)
    {
        cin>>a[i];
    }

    for(i=0; i<10; i++)
    {
        if(i==0)
        {
            ln=a[i];
            sn=a[i];
            lnp=i;
            snp=i;
        }
        else if(a[i]>ln)
        {
            ln=a[i];
            lnp=i;
        }
        else if(a[i]<sn)
        {
            sn=a[i];
            snp=i;
        }
    }

    cout<<"Largest Number Position = "<<lnp+1<<endl;
    cout<<"Smallest Number Position = "<<snp+1;
    return 0;
}

Output

Enter 10 numbers
63
12
57
61
98
11
3
4
75
82
Largest Number Position = 5
Smallest Number Position = 7
video-poster