This statement
second_large = arr[1];
at once selects the largest number in the array { 4, 5, 1, 2, 3 }.
So the expression of the nested if statement within this for loop
for(i=0;i<n;i++)
{
if(arr[i] != large)
{
if(arr[i]>second_large)
second_large = arr[i];
}
}
never evaluates to true. That is the second largest number will always equal to the first largest number.
Pay attention to that in general an array can contain all elements equal each other.
I can suggest the following approach.
large = 0;
second_large = n;
i = 1;
while ( i < n && arr[i] == arr[large] ) ++i;
if ( i < n )
{
if ( arr[large] < arr[i] )
{
large = i;
second_large = 0;
}
else
{
second_large = i;
}
while ( ++i < n )
{
if ( arr[large] < arr[i] )
{
second_large = large;
large = i;
}
else if ( arr[second_large] < arr[i] )
{
second_large = i;
}
}
}
if ( second_large == n )
{
puts( "\nAll elements are equal each other." );
}
else
{
printf("\nThe largest of these numbers is : %d", arr[large] );
printf("\nThe second largest of these numbers is : %d", arr[second_large] );
}
Here is a demonstration C program (It is a C program because the program provided by you in fact has nothing from C++)
#include <stdio.h>
int main( void )
{
int arr[] = { 4, 5, 1, 2, 3 };
const size_t n = sizeof( arr ) / sizeof( *arr );
size_t large = 0;
size_t second_large = n;
size_t i = 1;
while (i < n && arr[i] == arr[large]) ++i;
if (i < n)
{
if (arr[large] < arr[i])
{
large = i;
second_large = 0;
}
else
{
second_large = i;
}
while (++i < n)
{
if (arr[large] < arr[i])
{
second_large = large;
large = i;
}
else if (arr[second_large] < arr[i])
{
second_large = i;
}
}
}
for (i = 0; i < n; i++)
{
printf( "%d ", arr[i] );
}
putchar( '\n' );
if (second_large == n)
{
puts( "All elements are equal each other." );
}
else
{
printf( "The largest of these numbers is : %d\n", arr[large] );
printf( "The second largest of these numbers is : %d\n", arr[second_large] );
}
}
The program output is
4 5 1 2 3
The largest of these numbers is : 5
The second largest of these numbers is : 4
A C++ demonstration program can look the following way
#include <iostream>
#include <functional>
#include <iterator>
#include <algorithm>
int main()
{
int arr[] = { 4, 5, 1, 2, 3 };
const size_t n = sizeof( arr ) / sizeof( *arr );
auto last = std::next( arr, n );
for (auto first = std::begin( arr ); first != last; ++first)
{
std::cout << *first << ' ';
}
std::cout << '\n';
auto it = std::adjacent_find( std::begin( arr ), last,
std::not_equal_to<>() );
if (it == last)
{
std::cout << "All elements are equal each other.\n";
}
else
{
auto [second_large, large] = std::minmax( { it, std::next( it ) },
[]( const auto &it1, const auto &it2 )
{
return *it1 < *it2;
} );
for (auto current = std::next( it, 2 ); current != last; ++current)
{
if (*large < *current)
{
second_large = std::exchange( large, current );
}
else if (*second_large < *current)
{
second_large = current;
}
}
std::cout << "The largest of these numbers is "
<< *large << " that is present at position "
<< std::distance( std::begin( arr ), large )
<< '\n';
std::cout << "The second largest of these numbers is "
<< *second_large << " that is present at position "
<< std::distance( std::begin( arr ), second_large )
<< '\n';
}
}
The program output is
4 5 1 2 3
The largest of these numbers is 5 that is present at position 1
The second largest of these numbers is 4 that is present at position 0