The problem here is getting the third largest number of the random numbers.
My code out should be generating random 100 numbers and by the sorting algorithm I'll get the 3rd largest number.
#include <iostream>
#include <climits>
#include <stdlib.h>
using namespace std;
void thirdmax(int arr[], int arr_size)
{
if (arr_size < 3) {
printf(" Invalid Input ");
return;
}
// Find first max element
int first = arr[0];
for (int i = 1; i < arr_size; i++)
if (arr[i] > first)
first = arr[i];
// Find second max element
int second = int_min;
for (int i = 0; i < arr_size; i++)
if (arr[i] > second && arr[i] < first)
second = arr[i];
// Find third max element
int third = int_min;
for (int i = 0; i < arr_size; i++)
if (arr[i] > third && arr[i] < second)
third = arr[i];
std::cout<<"The Third Max Element is:"<<third;
}
int main()
{
int i;
int arr_size;
cout<<" Random Numbers : ";
for(i=1;i<=100;i++)
{
arr_size=rand()%100;
cout<<" "<<arr_size<<" ";
}
cout<<"\n";
int n = sizeof(int arr_size) / sizeof(int arr_size[0]);
thirdmax(int rr_size, n);
return 0;
}
My code out should be generating random 100 numbers and by the sorting algorithm I'll get the 3rd largest number.