Question

In: Computer Science

Write a working C++ program to ask the user for a set of grades which are...

Write a working C++ program to ask the user for a set of grades which are to be stored in an array in increasing sequence. The user must first provide the number which represents the count of all grades to be provided. For example, if there will be 10 grades, the user is first prompted to provide the number 10. Subsequently, each grade is provided, one at a time. Allow for a maximum of 30 grades in defining the array. When all the grades have been sorted properly, print them all, along with the average.

Process the array using a function which is invoked from the main body of the program. The main body asks for the count of grades and then invokes the grade sequencing function.

You must write in proper C++ syntax and actually execute the program to ensure it works properly.

Solutions

Expert Solution

C++ CODE:

#include <iostream>

using namespace std;

//Grade sequencing function
void getGrades(int grades[], int count) {
    for(int i = 0; i < count; i++){
        cout << "Enter grade " << (i + 1) << ": ";
        cin >> grades[i];
    }
    //Sort grades in increasing sequence
    for(int i = 0; i < count - 1; i++){
        for(int j = 0; j < count - i - 1; j++){
            if(grades[j] > grades[j + 1]){
                //Swap grades
                int temp = grades[j];
                grades[j] = grades[j + 1];
                grades[j + 1] = temp;
            }
        }
    }
}

//Function to return average grade
double getAverage(int grades[], int count){
    double avg = 0.0;
    for(int i = 0; i < count; i++)
        avg += grades[i];
    avg /= count;
    return avg;
}

int main(){
    const int MAX = 30;
    int grades[MAX];
    int count;
    cout << "Enter number of grades: ";
    cin >> count;
    getGrades(grades, count);
    cout << "Sorted grades:\n";
    for(int i = 0; i < count; i++)
        cout << (i + 1) << ". " << grades[i] << endl;
    cout << "Average grade: " << getAverage(grades, count) << endl;
    return 0;
}

SAMPLE OUTPUT:


Related Solutions

Write a Java program that will first ask the user how many grades they want to...
Write a Java program that will first ask the user how many grades they want to enter. Then use a do…while loop to populate an array of that size with grades entered by the user. Then sort the array. In a for loop read through that array, display the grades and total the grades. After the loop, calculate the average of those grades and display that average. Specifications Prompt the user for the number of grades they would like to...
Write in C# please! Ask the user to enter all of their exam grades. Once they...
Write in C# please! Ask the user to enter all of their exam grades. Once they are done, calculate the minimum score, the maximum score and the average score for all of their scores. Perform this using at least 2 Loops (can be the same type of loop) and not any built in functions.
Please code in c# (C-Sharp) Write a program that will ask the user for their name....
Please code in c# (C-Sharp) Write a program that will ask the user for their name. If the user does not input anything, display a warning before continuing. The program will then ask the user whether they want to have an addition, subtraction, multiplication, or division problem. Once the user indicates their choice, the program will display 2 randomly generated numbers from 1 to 9 in a math problem matching the user’s choice. Example: user selects addition, the equation presented...
Write a C++ program, ask the user about which sorting algorithm they would like to use...
Write a C++ program, ask the user about which sorting algorithm they would like to use in order to sort the vector. The four sorting algorithms your program should implement are: selection sort, merge sort, quick sort, and insertion sort. Once a sorting algorithm is selected, your program should display step by step every change that the sorting algorithm does on its journey to completely sorting the integer values of the vector Your output file should then contain the sorted...
Write a program in C, that uses standard input and output to ask the user to...
Write a program in C, that uses standard input and output to ask the user to enter a sentence of up to 50 characters, the ask the user for a number between 1 & 10. Count the number of characters in the sentence and multiple the number of characters by the input number and print out the answer. Code so far: char sentence[50]; int count = 0; int c; printf("\nEnter a sentence: "); fgets(sentence, 50, stdin); sscanf(sentence, %s;    for(c=0;...
Ask the user to input a series of numbers, write a C# program to output the...
Ask the user to input a series of numbers, write a C# program to output the sum, max, and min. Be sure to do error checking if the user input is not a number.
c++ Write a program that will ask the user for three pairs of integer values. The...
c++ Write a program that will ask the user for three pairs of integer values. The program will then display whether the first number of the pair is multiple of the second. The actual work of making the determination will be performed by a function called IsMultiple that takes two integer arguments (say, x and y). The function will return a Boolean result of whether x is a multiple of y.
In C: Write a complete program that performs the following task: Ask the user for the...
In C: Write a complete program that performs the following task: Ask the user for the number of sequences to display. For each sequence, Ask the user for a starting value Print out the value and double it (multiply by 2). Continue printing and doubling (all on the same line, separated by one space each) as long as the current number is less than 1000, or until 8 numbers have been printed on the line. You may assume that the...
Write a C program that demonstrate tree traversal. A. first is to ask the user to...
Write a C program that demonstrate tree traversal. A. first is to ask the user to enter data B. ask how many nodes THIS IS JUST A SAMPLE. PLEASE ANSWER IT IN COMPLETE CODE. See for the sample output. Sample Output: How many node do you have in your tree : 5 Enter root: 20 if you want to add to the left press 0 otherwise press 1 answer: 0 Enter next node: 10 if you want to add to...
Write a program that does the following things: in C++ 1 ) ask the user for...
Write a program that does the following things: in C++ 1 ) ask the user for 2 primary colors (red, blue, yellow) [should be in main] 2 ) determine the secondary color the 2 primarily colors make [should be in newColor function] red + blue = purple red + yellow = orange blue + yellow = green 3 ) Print the following message <color1> and <color2> makes <color3>
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT