In: Computer Science
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500.
Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989
Scores[1] = 486 Scores[4] = 216 Scores[7] = 319
Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
Explanation:
Here is the code which has the array scores initialised as mentioned above.
After that, the array is sorted using the selection sort algorithm.
Then, using another for loop, number of scores less than 500 are counted and also the number of scores more than 500 are counted.
Code:
#include <iostream>
using namespace std;
int main()
{
int scores[10] = {198, 486, 651, 85, 216, 912, 73, 319, 846,
989};
for (int i = 0; i < 9; i++) {
int mini = i;
for(int j=i+1; j<10; j++)
{
if(scores[j]<scores[mini])
mini = j;
}
int temp = scores[mini];
scores[mini] = scores[i];
scores[i] = temp;
}
int less = 0;
int more = 0;
for (int i = 0; i < 10; i++) {
if(scores[i]<500)
less++;
if(scores[i]>500)
more++;
}
cout<<less<<" scores are less than
500."<<endl;
cout<<more<<" scores are more than
500."<<endl;
return 0;
}
Output;
PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!
PLEASE COMMENT IF YOU NEED ANY HELP!