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

Task 1:

#include <iostream>
#include<iomanip>
using namespace std;

void arrSelectSort(double *, int);
double showAverage(double *, int);

int main()
{
        double *scores,total = 0.0, average;                           
        int numScores;                  
                                                                    
        cout << "How many test scores would you like to process?";
        cin >> numScores;

         //Dynamically allocate an array large enough to hold that many test scores
        scores = new double[numScores];
        if (scores == NULL)
                return 0;

        //Get the test score for each test
        cout << "Enter the test scores below.\n";
        
        for (int count = 0; count < numScores; count++)
        {
                cout << "Test score #" << (count + 1) << ": ";
                cin >> scores[count];
                while (scores[count] <= 0)
                {
                        cout << "Zero or negative numbers not accepted.\n";
                        cout << "Test Score #" << (count + 1) << ": ";
                        cin >> scores[count];
                }
    }
        //sort the elements of the array pointers
        arrSelectSort(scores, numScores);

        cout << "The test scores in ascending order are: \n";

        average=showAverage(scores, numScores);
        for (int count = 0; count< numScores; count++)
                cout << scores[count] << " ";
        cout << endl;
        cout << fixed << showpoint << setprecision(2);
        cout << "Average Score: " << average << endl;
        return 0;

}

void arrSelectSort(double *array, int size)
{
        int startScan, minIndex;
        double  minElem;
        for (startScan = 0; startScan < (size - 1); startScan++)
        {
                minIndex = startScan;
                minElem = array[startScan];
                for (int index = startScan + 1; index < size; index++)
                {
                        if (array[index]  < minElem)
                        {
                                minElem = array[index];
                                minIndex = index;
                        }
                }
                array[minIndex] = array[startScan];
                array[startScan] = minElem;
        }
}


double showAverage(double *scores, int numScores)
{
        double average,total;
        for (int count = 0; count < numScores; count++)
        {
                total += scores[count];
        }
        //Calculate the average
        average = total / numScores;
        return average;

}

OUTPUT:-

How many test scores would you like to process?5
Enter the test scores below.
Test score #1: 78
Test score #2: 52
Test score #3: 63
Test score #4: 25
Test score #5: 84
The test scores in ascending order are:
25 52 63 78 84
Average Score: 60.40

Task2:-

#include <iostream>
#include<iomanip>
using namespace std;

void arrSelectSort(double *, int);
double showAverage(double *, int);

int main()
{
        double *scores,total = 0.0, average;                           
        int numScores;                  
                                                                    
        cout << "How many test scores would you like to process?";
        cin >> numScores;

         //Dynamically allocate an array large enough to hold that many test scores
        scores = new double[numScores];
        if (scores == NULL)
                return 0;

        //Get the test score for each test
        cout << "Enter the test scores below.\n";
        
        for (int count = 0; count < numScores; count++)
        {
                cout << "Test score #" << (count + 1) << ": ";
                cin >> scores[count];
                while (scores[count] <= 0)
                {
                        cout << "Zero or negative numbers not accepted.\n";
                        cout << "Test Score #" << (count + 1) << ": ";
                        cin >> scores[count];
                }
    }
        //sort the elements of the array pointers
        arrSelectSort(scores, numScores);

        cout << "The test scores in ascending order are: \n";

        average=showAverage(scores, numScores);
        for (int count = 0; count< numScores; count++)
                cout << scores[count] << " ";
        cout << endl;
        cout << fixed << showpoint << setprecision(2);
        cout << "Average Score (Where lowest test score not included): " << average << endl;
        return 0;

}

void arrSelectSort(double *array, int size)
{
        int startScan, minIndex;
        double  minElem;
        for (startScan = 0; startScan < (size - 1); startScan++)
        {
                minIndex = startScan;
                minElem = array[startScan];
                for (int index = startScan + 1; index < size; index++)
                {
                        if (array[index]  < minElem)
                        {
                                minElem = array[index];
                                minIndex = index;
                        }
                }
                array[minIndex] = array[startScan];
                array[startScan] = minElem;
        }
}


double showAverage(double *scores, int numScores)
{
        double average,total;
        for (int count = 1; count < numScores; count++) //loweset test score not included 
        {
                total += scores[count];
        }
        //Calculate the average
        average = total / (numScores-1);
        return average;

}

OUTPUT:-

How many test scores would you like to process?5
Enter the test scores below.
Test score #1: 52
Test score #2: 41
Test score #3: 85
Test score #4: 63
Test score #5: 62
The test scores in ascending order are:
41 52 62 63 85
Average Score (Where lowest test score not included): 65.50

Task3:-

#include <iostream>
#include<iomanip>
using namespace std;

void arrSelectSort(double *,string *, int);

int main()
{
        double *scores,total = 0.0, average;    
        string *student;                       
        int numScores;                  
                                                                    
        cout << "How many test scores would you like to process?";
        cin >> numScores;

         //Dynamically allocate an array large enough to hold that many test scores
        scores = new double[numScores];
        student=new string[numScores];
        if (scores == NULL)
                return 0;

        //Get the test score for each test
        cout << "Enter the test scores below.\n";
        
        for (int count = 0; count < numScores; count++)
        {   
                cout << "Student name #" << (count + 1) << ": ";
                cin>>student[count];
                cout << "Test score #" << (count + 1) << ": ";
                cin >> scores[count];
                while (scores[count] <= 0)
                {
                        cout << "Zero or negative numbers not accepted.\n";
                        cout << "Test Score #" << (count + 1) << ": ";
                        cin >> scores[count];
                }
    }
        //sort the elements of the array pointers
        arrSelectSort(scores,student, numScores);

        cout << "The test scores in ascending order are: \n";

        //average=showAverage(scores, numScores);
        for (int count = 0; count< numScores; count++)
                cout << "Student name: "<<student[count]<<"   Student score: "<<scores[count] << " \n";
        cout << endl;

        return 0;

}

void arrSelectSort(double *array,string student[], int size)
{
        int startScan, minIndex;
        double  minElem;
        string s;
        for (startScan = 0; startScan < (size - 1); startScan++)
        {
                minIndex = startScan;
                minElem = array[startScan];
                s=student[startScan];
                for (int index = startScan + 1; index < size; index++)
                {
                        if (array[index]  < minElem)
                        {
                                minElem = array[index];
                                s=student[index];
                                minIndex = index;
                        }
                }
                array[minIndex] = array[startScan];
                student[minIndex]=student[startScan];
                array[startScan] = minElem;
                student[startScan]=s;
        }
}

OUTPUT:-

How many test scores would you like to process?5
Enter the test scores below.
Student name #1: ram
Test score #1: 42
Student name #2: sam
Test score #2: 45
Student name #3: jadu
Test score #3: 86
Student name #4: tapas
Test score #4: 54
Student name #5: amit
Test score #5: 63
The test scores in ascending order are:
Student name: ram   Student score: 42
Student name: sam   Student score: 45
Student name: tapas   Student score: 54
Student name: amit   Student score: 63
Student name: jadu   Student score: 86


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