Question

In: Computer Science

C++ Task 1 Write a program that allocates an array large enough to hold 5 student...

C++

Task 1

Write a program that allocates an array large enough to hold 5 student test scores. Once all the scores are entered, the array should be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score. The program should display the sorted list of scores and averages with appropriate headings.

Input Validation: Do not accept negative numbers for test scores.

Task 2

Modify the program so the lowest test score is dropped. This score should not be included in the calculation of the average.

Task 3

Modify the program to allow the user to enter name-score pairs. For each student taking a test, the user types the student’s name followed by the student’s integer test score. Modify the sorting function so it takes an array holding the student names and an array holding the student test scores. When the sorted list of scores is displayed, each student’s name should be displayed along with his or her score.

C++

Solutions

Expert Solution

Code for task 1:

#include <iostream>

using namespace std;

int average(double arr1[5]){
double scoreAverage=(arr1[0]+arr1[1]+arr1[2]+arr1[3]+arr1[4])/5;
cout<<"Average: "<<scoreAverage<<endl;
}

int sort(double arr[5]){
int i,j,tmp;
for(i=0; i<4; i++)
{
for(j=i+1; j<5; j++)
{
if(arr[j] <arr[i])
{
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
cout<<"Sorted Array: "<<arr[0]<<" "<<arr[1]<<" "<<arr[2]<<" "<<arr[3]<<" "<<arr[4]<<endl;
average(arr);
}

int main()
{
double testScores[5];
for(int i=0;i<5;i++){
do{ cout<<"Enter the score of student "<<i+1<<": ";
cin>>testScores[i];}while(testScores[i]<0);
}
sort(testScores);

return 0;
}

Code for task 2:

#include <iostream>

using namespace std;

int average(double arr1[5]){
double scoreAverage=(arr1[1]+arr1[2]+arr1[3]+arr1[4])/4;
cout<<"Average without the least score: "<<scoreAverage<<endl;
}

int sort(double arr[5]){
int i,j,tmp;
for(i=0; i<4; i++)
{
for(j=i+1; j<5; j++)
{
if(arr[j] <arr[i])
{
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
cout<<"Sorted Array: "<<arr[0]<<" "<<arr[1]<<" "<<arr[2]<<" "<<arr[3]<<" "<<arr[4]<<endl;
average(arr);
}

int main()
{
double testScores[5];
for(int i=0;i<5;i++){
do{ cout<<"Enter the score of student "<<i+1<<": ";
cin>>testScores[i];}while(testScores[i]<0);
}
sort(testScores);

return 0;
}

Code for Task 3:

#include <iostream>
#include<string>

using namespace std;

int average(double arr1[5]){
double scoreAverage=(arr1[1]+arr1[2]+arr1[3]+arr1[4])/4;
cout<<"Average without the least score: "<<scoreAverage<<endl;
}

int sort(double arr[5],string str[5]){
int i,j,tmp;
string tmp2;
for(i=0; i<4; i++)
{
for(j=i+1; j<5; j++)
{
if(arr[j] <arr[i])
{
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
  
tmp2 = str[i];
str[i] = str[j];
str[j] = tmp2;
}
}
}
cout<<"Sorted Array: "<<endl;
cout<<"Score of "<<str[0]<<" is "<<arr[0]<<endl;
cout<<"Score of "<<str[1]<<" is "<<arr[1]<<endl;
cout<<"Score of "<<str[2]<<" is "<<arr[2]<<endl;
cout<<"Score of "<<str[3]<<" is "<<arr[3]<<endl;
cout<<"Score of "<<str[4]<<" is "<<arr[4]<<endl;
average(arr);
}

int main()
{
string studentName[5];
double testScores[5];
for(int i=0;i<5;i++){
cout<<"Enter the student's name: ";
cin>>studentName[i];
do{ cout<<"Enter the score of "<<studentName[i]<<": ";
cin>>testScores[i];}while(testScores[i]<0);
}
sort(testScores,studentName);

return 0;
}


Related Solutions

C++ Task 1 Write a program that allocates an array large enough to hold 5 student...
C++ Task 1 Write a program that allocates an array large enough to hold 5 student test scores. Once all the scores are entered, the array should be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score. The program should display the sorted list of scores and averages with appropriate headings. Input Validation: Do not accept negative numbers for test scores. Task 2 Modify the program so the lowest...
In C++ Write a program that dynamically allocates a built-in array large enough to hold a...
In C++ Write a program that dynamically allocates a built-in array large enough to hold a user-defined number of test scores. (Ask the user how many grades will be entered and use a dynamic array to store the numbers.) Once all the scores are entered, the array should be passed to a function that calculates the average score. The program should display the scores and average. Use pointer notation rather than array notation whenever possible. (Input Validation: Do not accept...
Write a program in c++ to do the following: 2. Create an array to hold up...
Write a program in c++ to do the following: 2. Create an array to hold up to 20 integers. 3. Create a data file or download attached text file (twenty_numbers.txt) that contains UP TO 20 integers. 4. Request the input and output file names from the user. Open the files being sure to check the file state. 5. Request from the user HOW MANY numbers to read from the data file, up to twenty. Request the number until the user...
In C++, write a program that uses array to calculate the factorial of a reasonable large...
In C++, write a program that uses array to calculate the factorial of a reasonable large number (say, up to 2,000).
In C++. Write a program that uses array to calculate the factorial of a reasonable large...
In C++. Write a program that uses array to calculate the factorial of a reasonable large number (say, up to 2,000).  
Program in C: Write a program in C that reorders the elements in an array in...
Program in C: Write a program in C that reorders the elements in an array in ascending order from least to greatest. The array is {1,4,3,2,6,5,9,8,7,10}. You must use a swap function and a main function in the code. (Hint: Use void swap and swap)
Write a program in C that declares the following array: int. array[] = { 1, 2,...
Write a program in C that declares the following array: int. array[] = { 1, 2, 4, 8, 16, 32 } Then write some code that accepts a number between 0 and 5 from the user and stores it in a variable called "index". Write some code that retrieves the item specified by the index, like this: int item = array[index]; Then write code that outputs the corresponding array entry based on the number the user entered. Example output: The...
1- Write it with C++ program §Write a function Rotate that rotates an array of size...
1- Write it with C++ program §Write a function Rotate that rotates an array of size n by d elements to the left §Use array as argument §In the main function, call the function Rotate and show the rotated array §Test your code For example: Input: [1 2 3 4 5 6 7], n = 7, d = 2 Output: [3 4 5 6 7 1 2] 2- Write it in C++ §Search Insert Position •Given a sorted array in...
C++ Memory Allocation: 1) Write a C++ program that allocates static, stack, & heap memory. Your...
C++ Memory Allocation: 1) Write a C++ program that allocates static, stack, & heap memory. Your program does not need to do anything else.  Indicate via comments where memory for at least one variable in each memory area is allocated. a) Write code that allocates static memory (only include one declaration): b) Write code that allocates stack memory (only include one declaration): c) Write code that allocates heap memory (only include one declaration): 2) Edit the C++ program below to include...
C++ Program: Write another program (in C++) that will allocate a local static array of integers...
C++ Program: Write another program (in C++) that will allocate a local static array of integers and then a dynamic array of integers. Are they stored next to each other? You can examine this by examining the memory addresses where they are located. As described in class, on some systems the size of a dynamic array is actually stored in the bytes previous to a dynamically allocated array. Through some experiments on your own, try to see if this is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT