0

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.

  • Your code is full of basic syntax errors. Assuming those are fixed, what behavior do you see that is not expected? – Botje Mar 13 '23 at 11:16
  • As an alternative approach: why not keep track of the three largest elements while you're walking the array instead of walking the array three times? – Botje Mar 13 '23 at 11:18
  • What is the 3rd largest number in `{100, 100, 90, 80}`? (Should all duplicate elements be counted, or only one of them should be counted?) – MikeCAT Mar 13 '23 at 11:19
  • dublicate number should not be included thus 80 is the third largest. sorry just a beginner – Jude Koh Mar 13 '23 at 11:22
  • i should get the third max of the 100 random numbers generated. i am confused i need for our assignment and i am not even in a programming – Jude Koh Mar 13 '23 at 11:26
  • See https://en.cppreference.com/w/cpp/numeric/random for better ways to generate and use random numbers than the old and crappy C `rand()` function (which you don't even seed with `srand()` btw). – Jesper Juhl Mar 13 '23 at 11:28
  • As for finding the "third max element" you don't need all that code, you can simply use [std::nth_element](https://en.cppreference.com/w/cpp/algorithm/nth_element). – Jesper Juhl Mar 13 '23 at 11:31
  • By the way; [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Jesper Juhl Mar 13 '23 at 11:32
  • can someone help me how to modify this code badly need help – Jude Koh Mar 13 '23 at 12:17
  • When you set a new "first", for example, you are currently losing, rather than demoting, the current first. Similarly with the other comparisons. std::nth_element would only work if duplicates were being retained, but it appears that they are not. (Which then leads to another problem when there are only one or two distinct values in the whole array.) – lastchance Mar 13 '23 at 12:25

0 Answers0