Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to find the second biggest number in a set of random numbers in C++

1 Answer

0 votes
#include <iostream>
#include <ctime>

int findSecondMax(int total, int rndmax) {
    int max, before_max, n;

    max = before_max = n = rand() % rndmax + 1;
    std::cout << n << std::endl;

    for (int i = 1; i < total; ++i) {
        n = rand() % rndmax + 1;
        std::cout << n << std::endl;
        if (n > max) {
            before_max = max;
            max = n;
        } else if (n > before_max) {
            before_max = n;
        }
    }

    return before_max;
}

int main() {
    std::srand(static_cast<unsigned>(std::time(nullptr)));

    int secondMax = findSecondMax(10, 100);
    
    std::cout << "The second biggest number is: " << secondMax << std::endl;
}


/*
run:

88
58
46
11
79
55
56
92
18
56
The second biggest number is: 88

*/

 



answered Oct 4, 2025 by avibootz
edited Oct 4, 2025 by avibootz
...