Find elements

Cpp program to find largest elements of an Array

Cpp program to find largest elements of an Array

In this tutorial, we will discuss the small concept of Cpp program to find the largest elements of an Array.

In this topic, we will learn how to find the largest elements of array elements in Cpp programming language

This program gets “n” number of elements and Enter the elements of the array as input from the user. then, this program finds and displays the largest elements from the array using for loops.

In this programs, we can see step by step procedure for completion of the program.

  • array declaration
  • Essential variable declaration
  • Get input from the user for array length
  • Get input from the user for array elements
  • loop for largest value
  • Display result on the screen

Find largest elements from integer Array

Program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int arr[30];//array declaration
    int length,i, max; //essential variable declaration
    cout<<"Enter the length of the array: ";
    cin>>length;//get input from user for length
    for(i=0; i<length; i++){
    cout<<"Enter element "<<i+1<<" :";//enter elements for array
    cin>>arr[i];
    }
    max=arr[0];

    for(i=0; i<length; i++){
    if(max<arr[i]){
        max=arr[i];
    }
    }
    cout<<"\nlargest elements is:"<<max;
getch();
    return 0;
}

When the above code is compiled and executed, it produces the following results

Enter the length of the array: 5
Enter element 1:87
Enter element 1:65
Enter element 1:78
Enter element 1:43
Enter element 1:56

largest element si: 87

 

Find largest elements from float Array

Program 2

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    float array[100];//Array declaration
    int length,i;
    double max;//variable declaration
    cout << "Enter the length of the array: " << endl;
    cin>>length;//Takes input from user for length
    for(i=0; i<length; i++){
        cout<<"Enter element "<<i+1<<" :";//enter elements for array
        cin>>array[i];
    }
    max=array[0];
    for(i=0; i<=length; i++){
        if(max<array[i]){
            max=array[i];
    }
}
cout<<"\n Largest elements is: "<<max;
getch();
    return 0;
}

 

When the above code is compiled and executed, it produces the following results

Enter the length of the array:
5
Enter element 1 :4.6
Enter element 1 :3.7
Enter element 1 :7.9
Enter element 1 :9.8
Enter element 1 :3.2

Largest elements is: 9.8

 

 

Suggested for you

Operator in C++ language

Single dimension Array in C++ language

For loop in C++ language

If statements in C++

Data type in C++ programming language

Program to find largest number among three numbers in Java
Java code to find largest elements of an Array
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago