For this question, you will have to write a main program and three functions. (Y
ID: 3843524 • Letter: F
Question
For this question, you will have to write a main program and three functions. (You can assume all output goes to the screen.) Write a complete C++ program, including least one comment in the main program and one in each function, to do the following. The main will declare an array scores to hold more than 50 scores. Assume there will be no more than 50 scores. It will call three functions to read in the scores and process them.. First, the main program will call a function readTheData to read in a header value and then read in a set of data consisting of integer test scores into an array (details below). Then the main program will call a function sortVals which will sort the values in the array into descending numerical order. When the function returns control to the main program, the main program will print the new values in the array. Finally, the main program will call a function twoBest which will determine the sum of the two best scores in the array and return that sum to the main program. The main program will print the answer returned by the function. Details: The function readTheData will receive two parameters: an array of integers which it calls nums and an integer k giving the number of values to be stored in the array. The function will read a value into k and then will read k data values into the array nums. Each data value is a number like 83 or 12. The function will print all data values (including k) as they are read in. The function sortVals will receive as parameters an array of integers which it calls vals and an integer n giving the number of values in the array. The function will sort the array into descending order: For example, if n is 5 and the array starts by holding these values: 84 4 96 95 70, then after the function is called, the array will hold these values: 96 95 84 70 4 You can use any sorting method of your choice; be sure to say (in a comment) which one you are choosing. The function twoBest will receive two parameters: an array of integers (which the function calls arr) sorted in descending order, and an integer count giving the number values in the array. The function will return the sum of the two largest values in the For example, if the sorted array holds 96 95 84 70 4, then the function should return (the sum of 96 and 95).Explanation / Answer
Here is your C++ program: -
#include<iostream>
using namespace std;
//method to read the data from user input
void readTheData(int& k,int* nums) {
cout<<"Enter the number of elements you want your array to have:";
cin>>k; // Number of elements
cout<<"Enter numbers in the array: ";
for(int i = 0; i < k;i++) {
cin>> nums[i]; // reading input from user
}
cout<<"Array numbers are:";
for(int i = 0; i < k;i++) {
cout<< nums[i] <<" "; // printing the array
}
}
// function to sort the array using insertion sort
void sortVals(int n,int* vals) {
for (int i = 1; i < n; i++)
{
for (int j = i; j >= 1; j--)
{
if (vals[j] > vals[j-1]) // searching for largest element
{
int temp = vals[j]; // inserting the element at first place
vals[j] = vals[j-1];
vals[j-1] = temp;
}
else
break;
}
}
cout<<" sorted array "<<endl; // printing the array
for (int k = 0; k < n; k++)
{
cout<<vals[k]<<endl;
}
}
//method to print the two largest values
void twoBest(int count,int* arr) {
if(count >= 2) { // if two or more elements are there
cout<<"Largest value:"<<arr[0]<<endl;
cout<<"Second Largest value:"<<arr[1]<<endl;
} else if(count == 1) { // if single element is there
cout<<"Largest and only value:"<<arr[0]<<endl;
} else { // if array is empty
cout<<"Array is empty."<<endl;
}
}
//main method to run the program.
int main() {
int k;
int scores[50]; // initializing the scores array
//calling the three methods
readTheData(k,scores);
sortVals(k,scores);
twoBest(k,scores);
return 0;
}
Sample Run:-
Enter the number of elements you want your array to have:5
Enter numbers in the array:
12
1
24
5
35
Array numbers are:12 1 24 5 35
sorted array
35
24
12
5
1
Largest value:35
Second Largest value:24
--------------------------------
Process exited after 13.35 seconds with return value 0
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.