C++ program to find the largest and second-largest numbers present in an array along with their location index on the array. This program first find the largest number by going through all the numbers in the array. It stores the index of the largest number on the variable pos1 and largest number is the largest variable. A second for loop is applied, which also check for the largest number. When it hits on the largest found by the first loop, it is bypassed using a continue statement and hence will get the second-largest number.
#include <iostream> #include <conio.h> using namespace std; void main() { int a[10], i, largest = 0, second_largest = 0, pos1, pos2; int n; cout << "Enter Number of elements :"; cin >> n; for (i = 0; i < n; ++i) { cout << "n Enter " << (i + 1) << "th Element :"; cin >> a[i]; } //Finding Largest for (i = 0; i < 10; ++i) { if (a[i] > largest) { largest = a[i]; pos1 = i; } } //finding second largset for (i = 0; i < 10; ++i) { if (a[i] > second_largest) { if (a[i] == largest) continue; //Ignoring largest in order to get second largest second_largest = a[i]; pos2 = i; } } cout << "nn Largest Number :" << largest << " at position " << (pos1 + 1); cout << "nn Second Largest Number :" << second_largest << " at position " << (pos2 + 1); getch(); return; }
*Compiled using Visual Studio
… [Trackback]
[…] Read More to that Topic: genuinecoder.com/cpp-program-find-largest-and-second-largest-number-array-html/ […]
… [Trackback]
[…] Read More Info here to that Topic: genuinecoder.com/cpp-program-find-largest-and-second-largest-number-array-html/ […]
… [Trackback]
[…] Read More Information here on that Topic: genuinecoder.com/cpp-program-find-largest-and-second-largest-number-array-html/ […]
… [Trackback]
[…] Read More Information here to that Topic: genuinecoder.com/cpp-program-find-largest-and-second-largest-number-array-html/ […]
… [Trackback]
[…] Info on that Topic: genuinecoder.com/cpp-program-find-largest-and-second-largest-number-array-html/ […]
… [Trackback]
[…] Information to that Topic: genuinecoder.com/cpp-program-find-largest-and-second-largest-number-array-html/ […]
… [Trackback]
[…] Read More Information here to that Topic: genuinecoder.com/cpp-program-find-largest-and-second-largest-number-array-html/ […]
… [Trackback]
[…] Read More Info here to that Topic: genuinecoder.com/cpp-program-find-largest-and-second-largest-number-array-html/ […]
… [Trackback]
[…] Find More Info here on that Topic: genuinecoder.com/cpp-program-find-largest-and-second-largest-number-array-html/ […]
… [Trackback]
[…] Find More here to that Topic: genuinecoder.com/cpp-program-find-largest-and-second-largest-number-array-html/ […]
… [Trackback]
[…] Read More to that Topic: genuinecoder.com/cpp-program-find-largest-and-second-largest-number-array-html/ […]
… [Trackback]
[…] Find More on to that Topic: genuinecoder.com/cpp-program-find-largest-and-second-largest-number-array-html/ […]
… [Trackback]
[…] Here you will find 52940 more Information on that Topic: genuinecoder.com/cpp-program-find-largest-and-second-largest-number-array-html/ […]
… [Trackback]
[…] Info on that Topic: genuinecoder.com/cpp-program-find-largest-and-second-largest-number-array-html/ […]
… [Trackback]
[…] Read More to that Topic: genuinecoder.com/cpp-program-find-largest-and-second-largest-number-array-html/ […]
… [Trackback]
[…] Read More to that Topic: genuinecoder.com/cpp-program-find-largest-and-second-largest-number-array-html/ […]
… [Trackback]
[…] Find More Information here on that Topic: genuinecoder.com/cpp-program-find-largest-and-second-largest-number-array-html/ […]
… [Trackback]
[…] Here you can find 6780 more Information to that Topic: genuinecoder.com/cpp-program-find-largest-and-second-largest-number-array-html/ […]
… [Trackback]
[…] Information on that Topic: genuinecoder.com/cpp-program-find-largest-and-second-largest-number-array-html/ […]
… [Trackback]
[…] Info on that Topic: genuinecoder.com/cpp-program-find-largest-and-second-largest-number-array-html/ […]
… [Trackback]
[…] Here you will find 5746 more Information to that Topic: genuinecoder.com/cpp-program-find-largest-and-second-largest-number-array-html/ […]
… [Trackback]
[…] Read More Info here to that Topic: genuinecoder.com/cpp-program-find-largest-and-second-largest-number-array-html/ […]
… [Trackback]
[…] Read More on to that Topic: genuinecoder.com/cpp-program-find-largest-and-second-largest-number-array-html/ […]
… [Trackback]
[…] There you can find 47640 more Information to that Topic: genuinecoder.com/cpp-program-find-largest-and-second-largest-number-array-html/ […]
… [Trackback]
[…] Find More Info here on that Topic: genuinecoder.com/cpp-program-find-largest-and-second-largest-number-array-html/ […]
… [Trackback]
[…] Information on that Topic: genuinecoder.com/cpp-program-find-largest-and-second-largest-number-array-html/ […]
… [Trackback]
[…] There you will find 80703 additional Information on that Topic: genuinecoder.com/cpp-program-find-largest-and-second-largest-number-array-html/ […]
… [Trackback]
[…] Info to that Topic: genuinecoder.com/cpp-program-find-largest-and-second-largest-number-array-html/ […]
… [Trackback]
[…] Read More Info here to that Topic: genuinecoder.com/cpp-program-find-largest-and-second-largest-number-array-html/ […]
… [Trackback]
[…] Read More Info here to that Topic: genuinecoder.com/cpp-program-find-largest-and-second-largest-number-array-html/ […]