Searching an array for the second greatest value using C++
Hi, this is Mazen. Today we're going to solve a new programming question using C++. We previously talked about searching an array for an entered number. Searching for the greatest or second greatest number is basically the same. Because finding the greatest value in an array isn't a huge issue, we're going to make our question a bit harder to find the second greatest number. In this example, we use arrays, loops, if statements and the ternary operator.
Write a program that finds the second greatest number in an array
Step 1: Declare an integer array with the size of 5, for instance.
int test[5];
for (int i = 0; i < 5; i++) { std::cout << "Integer #" << i + 1 << ": "; std::cin >> test[i]; }
Step 3: Determine the greatest number in the array using the following logic:
1) Create an integer variable, name it greatest and assign the first element of the array (test[0]) to it as an initial value.
2) Create a for loop with four repetitions starting from 1 up to 4.
3) We use the ternary operator to find the greatest value and assign it to the variable greatest. The condition is that greatest is greater than or equal to the current element of the array. If yes, assign greatest to greatest - keep its value as is. If no, then assign the current element of the array (test[i]) to greatest.
int greatest = test[0]; for (int i = 1; i < 5; i++) greatest = greatest > test[i] ? greatest : test[i];
Step 4: Determine the second greatest number of the array using the following logic:
1) Create a variable of type int - like the type of the array. Name it secondGreatest - for instance - and give it an initial value of as small as possible. We can reach the smallest integer variable the system can store through the constant INT_MIN. Make sure you include the header <climits> to get that constant.
2) Create a for loop with five repetitions, starting from 0 up to 4, to pass through the array elements.
3) Inside of the body of the loop, we find the greatest number of the array after overriding the previously found greatest value. That is performed by using both an if statement and a ternary operator. The mission of the if statement is overriding the first greatest number, so the condition is that the current element of the array is equal to greatest. If yes, just override the rest following statements inside of the body of the loop and continue to the next repetition. The ternary operator statement is to determine the second greatest number by assigning test[i] to secondGreatest if test[i] is greater than secondGreatest. If the condition doesn't match, keep the value of secondGreatest as is.
int secondGreatest = INT_MIN; for (int i = 0; i < 5; i++) { if (test[i] == greatest) continue; secondGreatest = test[i] > secondGreatest ? test[i] : secondGreatest; }
Step 5: Finally, just output secondGreatest using a cout statement.
std::cout << "\nThe second greatest number: " << secondGreatest << "\n\n";
Putting it all together:
#include <iostream>
#include <climits>
int main()
{
int test[5];
for (int i = 0; i < 5; i++)
{
std::cout << "Integer #" << i + 1 << ": ";
std::cin >> test[i];
}
int greatest = test[0];
for (int i = 1; i < 5; i++)
greatest = greatest > test[i] ? greatest : test[i];
int secondGreatest = INT_MIN;
for (int i = 0; i < 5; i++)
{
if (test[i] == greatest)
continue;
secondGreatest = test[i] > secondGreatest ?
test[i] : secondGreatest;
}
std::cout << "\nThe second greatest number: "
<< secondGreatest << "\n\n";
system("pause");
return 0;
}
Comments
Post a Comment