Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

- Change Lab Exercise 7.2 (given BELOW) by doing the following instructions: // This program will read in a group of test scores( positive integers

- Change Lab Exercise 7.2 (given BELOW) by doing the following instructions:

// This program will read in a group of test scores( positive integers from 1 to 100) // from the keyboard and then calculates and outputs the average score // as well as the highest and lowest score. There will be a maximum of 100 scores.

//LAB EXERCISE 7.2 PART A:

#include

using namespace std;

const int ARRAY_SIZE=100;

float findAverage (int array[], int size); // finds average of all grades int findHighest (int array[], int size); // finds highest of all grades int findLowest (int array[], int size); // finds lowest of all grades

int main() {

int grades[ARRAY_SIZE]; // the array holding the grades. int temp; // variable for the input number int numberOfGrades; // the number of grades read. int pos; // index to the array. float avgOfGrades; // contains the average of the grades. int highestGrade; // contains the highest grade. int lowestGrade; // contains the lowest grade.

cout << "Please input a grade from 1 to 100, (or -99 to stop)" << endl; cin >> temp;

pos=0; while(pos < ARRAY_SIZE && temp != -99) { // Fill in the code to assign temp to the next element in intArray grades[pos]=temp;

// Fill in the code to read the next number cout << "Please input a grade from 1 to 100, (or -99 to stop)" << endl; cin>>temp; ++pos; }// end for

numberOfGrades = pos; // Fill blank with appropriate identifier

avgOfGrades = findAverage(grades, numberOfGrades);

// Fill in the call to the function that calculates highest grade highestGrade = findHighest(grades, numberOfGrades);

// Fill in the call to the function that calculates lowest grade lowestGrade = findLowest(grades, numberOfGrades);

cout << endl << "The average of all the grades is " << avgOfGrades << endl; cout << endl << "The highest grade is " << highestGrade << endl;

// Fill in code to write the lowest to the screen cout << endl << "The lowest grade is " << lowestGrade << endl; return 0; } // end main

//***************************************************************** // findAverage // // task: This function receives an array of integers and its size. // It finds and returns the average of the numbers in the array // data in: array of floating point numbers, size // data returned: avarage of the numbers in the array //

//*****************************************************************

float findAverage (int array[], int size) { float sum = 0; // holds the sum of all the numbers

for (int pos = 0; pos < size; pos++) sum = sum + array[pos];//adding grades[2]

return (sum / size); //returns the average }

//***************************************************************** // findHighest // // task: This function receives an array of integers and its size. // It finds and returns the highest value of the numbers in // the array // data in: array of floating point numbers, size // data returned: highest value of the numbers in the array // //***************************************************************** int findHighest (int array[], int size) { // Fill in the code for this function int highest=array[0];//better than highest = 0; for( int i=1; i < size; ++i ){// OK to do: int i=0; if( array[i] > highest ) highest = array[i]; } // end for return highest; }// end findHighest

//***************************************************************** // findLowest // // task: This function receives an array of integers and its size. // It finds and returns the lowest value of the numbers in // the array // data in: array of floating point numbers, size // data returned: lowest value of the numbers in the array // //***************************************************************** int findLowest (int array[], int size) {

// Fill in the code for this function int lowest=array[0]; for( int i=1; i < size; ++i ){// OK to do: int i=0; if( array[i] < lowest ) lowest = array[i]; } // end for return lowest; } // end findLowest

//LAB EXERCISE PART B, ONLY CHANGES MAIN plus #include int main() {

int grades[ARRAY_SIZE]; // the array holding the grades.

int temp; // variable for the input number

int numberOfGrades; // the number of grades read.

int pos; // index to the array.

float avgOfGrades; // contains the average of the grades.

int highestGrade; // contains the highest grade.

int lowestGrade; // contains the lowest grade.

ifstream infile; // INPUT FILE

infile.open("gradefile.txt");

if( infile.fail() ){ cout<<"Unable to open input file, ending program "; return 1; } // end if // Read in the values into the array

pos = 0;

// cout << "Please input a grade from 1 to 100, (or -99 to stop)" << endl;

while (pos < ARRAY_SIZE && infile>> grades[pos]) { // ORDER OF TESTING ABOVE MAKES A DIFFERENCE!!! // grades[pos]=temp; // cout << "Please input a grade from 1 to 100, (or -99 to stop)" // << endl;

// cin>> temp;

++pos;

}

infile.close();

numberOfGrades = pos; // Fill blank with appropriate identifier

// call to the function to find average

avgOfGrades = findAverage(grades, numberOfGrades);

cout << endl << "The average of all the grades is " << avgOfGrades << endl;

// Fill in the call to the function that calculates highest grade highestGrade = findHighest(grades, numberOfGrades);

cout << endl << "The highest grade is " << highestGrade << endl;

// Fill in the call to the function that calculates lowest grade lowestGrade = findLowest(grades, numberOfGrades); // Fill in code to write the lowest to the screen cout << endl << "The lowest grade is " << lowestGrade << endl;

return 0; } // end main

- AFTER calling the findAverage function in main, CALL the selectionSort function (passing the grades array and numberOfGrades), using the selectionSort function GIVEN BELOW (DON'T CHANGE ANYTHING, NOT EVEN PARAMETER NAMES in the selectionSort)!!!

/**************************************************************

// Definition of function selectionSort. *

// This function performs an ascending order selection sort on *

// array. size is the number of elements in the array. *

//**************************************************************

void selectionSort(int array[], int size)

{

int startScan, minIndex, minValue;

for (startScan = 0; startScan < (size - 1); startScan++)

{

minIndex = startScan;

minValue = array[startScan];

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

{

if (array[index] < minValue)

{

minValue = array[index];

minIndex = index;

}

}

array[minIndex] = array[startScan];

array[startScan] = minValue;

}

}

- ASSIGN to a NEW local variable (call it countAboveAvg, an int) the return value of a function THAT YOU WRITE that returns how many elements in the grades are above the average (see function heading below, that WE will write... so find it)

- Call the showArray() function from Pr8-5 GIVEN BELOW

//Pr8-5 This program uses the selection sort algorithm to sort an // array in ascending order. #include  using namespace std; // Function prototypes void selectionSort(int [], int); void showArray(int [], int); int main() { // Define an array with unsorted values const int SIZE = 6; int values[SIZE] = {5, 7, 2, 8, 9, 1}; // Display the values. cout << "The unsorted values are "; showArray(values, SIZE); // Sort the values. selectionSort(values, SIZE); // Display the values again. cout << "The sorted values are "; showArray(values, SIZE); return 0; } //************************************************************** // Definition of function selectionSort. * // This function performs an ascending order selection sort on * // array. size is the number of elements in the array. * //************************************************************** void selectionSort(int array[], int size) { int startScan, minIndex, minValue; for (startScan = 0; startScan < (size - 1); startScan++) { minIndex = startScan; minValue = array[startScan]; for(int index = startScan + 1; index < size; index++) { if (array[index] < minValue) { minValue = array[index]; minIndex = index; } } array[minIndex] = array[startScan]; array[startScan] = minValue; } } //************************************************************** // Definition of function showArray. * // This function displays the contents of array. size is the * // number of elements. * //************************************************************** void showArray(int array[], int size) { for (int count = 0; count < size; count++) cout << array[count] << " "; cout << endl; } 

- Display numAboveAvg and " are above the average"

- Put before main, the function prototype declarations for the selectionSort, showArray and the NEW function that you write

- If time, try to run this!

WRITE the UPDATED Lab 7.2 part B in C++

Module Specs:

countAboveAvg

Return Value: how many elements > average (int)

Reference Parameters: (none)

Receives: intArray (array of ints), numElems (int), average (float)

Preconditions: intArray has been populated (but no necessarily sorted), numElems indicates how many elements are used in the array, average has been calculated and assigned

Logic: In a for loop, check if each element > average, and if so, increment the counter. At the end, return the counter.

DON'T FORGET TO INITIALIZE THE COUNTER!

FUNCTION HEADING (based on the module specs):

int countAboveAvg( int array[], int numElems, float average)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Power Of Numbers In Health Care A Students Journey In Data Analysis

Authors: Kaiden

1st Edition

8119747887, 978-8119747887

More Books

Students also viewed these Databases questions

Question

How will the organization compete? P-968

Answered: 1 week ago

Question

Consistently develop management talent.

Answered: 1 week ago

Question

Create a refreshed and common vision and values across Europe.

Answered: 1 week ago

Question

Provide the best employee relations environment.

Answered: 1 week ago