181k views
0 votes
Write a program to read as many test scores as the user wants from the keyboard (assuming at most 50 scores). Print the scores in (1) original order, (2) sorted from high to low (3) the highest score, (4) the lowest score, and (5) the average of the scores. Implement the following functions using the given function prototypes: void displayArray(int array[], int size) - Displays the content of the array void selectionSort(int array[], int size) - sorts the array using the selection sort algorithm in descending order. Hint: refer to example 8-5 in the textbook. int findMax(int array[], int size) - finds and returns the highest element of the array int findMin(int array[], int size) - finds and returns the lowest element of the array double findAvg(int array[], int size) - finds and returns the average of the elements of the array

User Clonk
by
3.5k points

1 Answer

0 votes

Answer: Provided in the explanation segment

Step-by-step explanation:

Below is the code to carry out this program;

/* C++ program helps prompts user to enter the size of the array. To display the array elements, sorts the data from highest to lowest, print the lowest, highest and average value. */

//main.cpp

//include header files

#include<iostream>

#include<iomanip>

using namespace std;

//function prototypes

void displayArray(int arr[], int size);

void selectionSort(int arr[], int size);

int findMax(int arr[], int size);

int findMin(int arr[], int size);

double findAvg(int arr[], int size) ;

//main function

int main()

{

const int max=50;

int size;

int data[max];

cout<<"Enter # of scores :";

//Read size

cin>>size;

/*Read user data values from user*/

for(int index=0;index<size;index++)

{

cout<<"Score ["<<(index+1)<<"]: ";

cin>>data[index];

}

cout<<"(1) original order"<<endl;

displayArray(data,size);

cout<<"(2) sorted from high to low"<<endl;

selectionSort(data,size);

displayArray(data,size);

cout<<"(3) Highest score : ";

cout<<findMax(data,size)<<endl;

cout<<"(4) Lowest score : ";

cout<<findMin(data,size)<<endl;

cout<<"(5) Lowest scoreAverage score : ";

cout<<findAvg(data,size)<<endl;

//pause program on console output

system("pause");

return 0;

}

/*Function findAvg that takes array and size and returns the average of the array.*/

double findAvg(int arr[], int size)

{

double total=0;

for(int index=0;index<size;index++)

{

total=total+arr[index];

}

return total/size;

}

/*Function that sorts the array from high to low order*/

void selectionSort(int arr[], int size)

{

int n = size;

for (int i = 0; i < n-1; i++)

{

int minIndex = i;

for (int j = i+1; j < n; j++)

if (arr[j] > arr[minIndex])

minIndex = j;

int temp = arr[minIndex];

arr[minIndex] = arr[i];

arr[i] = temp;

}

}

/*Function that display the array values */

void displayArray(int arr[], int size)

{

for(int index=0;index<size;index++)

{

cout<<setw(4)<<arr[index];

}

cout<<endl;

}

/*Function that finds the maximum array elements */

int findMax(int arr[], int size)

{

int max=arr[0];

for(int index=1;index<size;index++)

if(arr[index]>max)

max=arr[index];

return max;

}

/*Function that finds the minimum array elements */

int findMin(int arr[], int size)

{

int min=arr[0];

for(int index=1;index<size;index++)

if(arr[index]<min)

min=arr[index];

return min;

}

cheers i hope this help!!!

User Axs
by
3.3k points