Question

In: Computer Science

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

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


#include <bits/stdc++.h>
using namespace std;

void sort_task1(int arr[5])
{
    int n = 5;
    sort(arr, arr+n);
    cout<<"Sorted list of Marks: ";
    for(int i=0;i<n;i++)
    {
        cout<<arr[i]<<"\t";
    }
}
void avg_task1(int arr[5])
{
    int sum=0;
    int n = 5;
    for(int i=0;i<n;i++)
    {
        sum+=arr[i];
    }
    int avg;
    avg=sum/n;
    cout<<"Avg :"<<avg<<endl;
}

void sort_task2(int arr[])
{
    int n = 5;
    sort(arr, arr+n);
    for(int i=0;i<n-1;i++)
    {
        arr[i]=arr[i+1];
    }
    arr[5]=0;
    cout<<"Sorted list of Marks: ";
    for(int i=0;i<n-1;i++)
    {
        cout<<arr[i]<<"\t";
    }
}
void avg_task2(int arr[])
{
    int sum=0;
    int n = 4;
    for(int i=0;i<n;i++)
    {
        sum+=arr[i];
    }
    int avg;
    avg=sum/n;
    cout<<"Avg :"<<avg<<endl;
}


void sort_task3(vector<pair<int,string>> vp)
{
    int n = 5;
    sort(vp.begin(), vp.end());
    cout<<"Sorted list of Marks: "<<endl;
    for(int i=0;i<n;i++)
    {
        cout<<vp[i].second<<" "<<vp[i].first<<endl;
    }
}
void avg_task3(vector<pair<int,string>> vp)
{
    int sum=0;
    int n = 5;
    for(int i=0;i<n;i++)
    {
        sum+=vp[i].first;
    }
    int avg;
    avg=sum/n;
    cout<<"Avg :"<<avg<<endl;
}
int main()
{

    int arr[5];
    int x;
    for(int i=0;i<5;i++)
    {
        cin>>x;
        while(x<0)
        {
            cout<<"Invalid number,Enter again"<<endl;
            cin>>x;
        }
        arr[i]=x;
    }
    //Task 1
    sort_task1(arr);
    avg_task1(arr);
    
    //Task 2
    sort_task2(arr);
    avg_task2(arr);
    
    //Task 3
    vector<pair<int,string>> v;
    for(int i=0;i<5;i++)
    {
        string name;
        int marks;
        cin>>name>>marks;
        while(marks<0)
        {
            cout<<"Invalid number,Enter again"<<endl;
            cin>>name>>marks;
        }
        v.push_back({marks,name});
    }
    sort_task3(v);
    avg_task3(v);
    return 0;
}

Note: I have taken avg in all avg functions as int.

You can type cast them as float if you want.


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...
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...
(JAVA) Write a program that maintains student test scores in a two-dimesnional array, with the students...
(JAVA) Write a program that maintains student test scores in a two-dimesnional array, with the students identified by rows and test scores identified by columns. Ask the user for the number of students and for the number of tests (which will be the size of the two-dimensional array). Populate the array with user input (with numbers in {0, 100} for test scores). Assume that a score >= 60 is a pass for the below. Then, write methods for: Computing the...
Write a Java program that sorts an array of “Student” in an aescending order of their...
Write a Java program that sorts an array of “Student” in an aescending order of their “last names”. The program should be able to apply (Insertion sort): Student [] studs = new Student[8]; s[0] = new Student("Saoud", "Mohamed", 3.61); s[1] = new Student("Abdelkader", "Farouk", 2.83); s[2] = new Student("Beshr" , "Alsharqawy", 1.99); s[3] = new Student("Nader", "Salah", 3.02); s[4] = new Student("Basem", "Hawary", 2.65); s[5] = new Student("Abdullah", "Babaker", 2.88); s[6] = new Student("Abdelaal", "Khairy", 3.13); s[7] = new Student("Mohamedain",...
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...
Write a program that uses Python List of strings to hold the five student names, a...
Write a program that uses Python List of strings to hold the five student names, a Python List of five characters to hold the five students’ letter grades, and a Python List of four floats to hold each student’s set of test scores. The program should allow the user to enter each student’s name and his or her four test scores. It should then calculate and display each student’s average test score and a letter grade based on the average....
Part 1:Write a program in Java that declares an array of 5 elements and displays the...
Part 1:Write a program in Java that declares an array of 5 elements and displays the contents of the array. Your program should attempt to access the 6th element in the array (which does not exist) and using try. catch your program should prevent the run-time error and display your error message to the user. The sample output including the error message is provided below. Part (1) Printing an element out of bounds 5 7 11 3 0 You went...
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).  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT