Question

In: Computer Science

C++ Programming-Need getGrades function (I keep getting errors). Write a function called getGrades that does the...

C++ Programming-Need getGrades function (I keep getting errors).

  1. Write a function called getGrades that does the following:

  • Take an array of integers and an integer representing the size of the array as parameters.

  • Prompt the user to enter up to 20 grades.

  • Store the grades in the array.

  • Return the number of grades entered.

  1. Write another function called calcStats that does the following:

  • Take an array of integers and an integer representing the size of the array as normal parameters.

  • Use three reference variables to return the lowest, highest, and average grades.

  1. In your main function do the following:

  • Create an array of 20 elements.

  • Use the getGrades function to get the grades from the user.

  • Use the calcStats function to calculate the lowest, highest, and average grades.

  • Display the results.

  • Sample output:

  • Please enter up to 20 grades followed by a -1 when you are done.
    67
    87
    92
    78
    82
    -1
    Lowest grade: 67
    Highest grade: 92
    Average grade: 81.2

Solutions

Expert Solution

#include<iostream>
using namespace std;
//the below function stores the grades in array upto size or till -1 is encountered and returns the number of grades entered
int getGrades(int grades[],int size)
{
int gradeCount=0,grade;
cout<<"Please enter up to 20 grades followed by a -1 when you are done."<<endl;
//read the grade from user
cin>>grade;
//check the grade if it is -1 stop the process otherwise continue reading next grade
while(1)
{
if(grade==-1 || gradeCount>=size)
break;
grades[gradeCount]=grade; //store the grade in array
++gradeCount;
cin>>grade; //read the next grade from the user
}
return gradeCount;
}
//calculate lowest,highest and average of grades
void calcStats(int grades[],int size,int &lowest,int &highest,double &average)
{
int i;
double sum=0.0;
lowest=grades[0];
highest=grades[0];
for(i=1;i<size;++i)
{
if(lowest>grades[i])
lowest=grades[i];
if(highest<grades[i])
highest=grades[i];
}
//calculate sum
for(i=0;i<size;++i)
sum+=grades[i];
average=sum/size;
}
int main()
{
//create array of 20 elements
int grades[20];
//call getGrades() to return the total number of grades
int total=getGrades(grades,20);
int lowest,highest;
double average;
//call calcStats() to calculate lowest,highest and average of grades
calcStats(grades,total,lowest,highest,average);
cout<<"Lowest grade: "<< lowest<<endl;
cout<<"Highest grade: "<< highest<<endl;
cout<<"Average grade: "<< average<<endl;
}

Output


Related Solutions

I working on this program in C++ and I keep getting 20 errors of the same...
I working on this program in C++ and I keep getting 20 errors of the same type again.cpp:36:11: error: use of undeclared identifier 'Polynomial' int main() { // create a list of polinomials vector<Polynomial> polynomials; // welcome message cout << "Welcome to Polynomial Calculator" << endl; int option = 0; while (option != 6) { // display menu displayMenu(); // get user input; cin >> option; if (option == 1) { cout << "Enter a polynomial :" << endl; string...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes one doyble argument called "num". The function will declare, ask, and get another double from the user. Compare the double entered by the user to "num" and return a 0 if they are the same, a -1 num is less than the double entered by the user and 1 if it is greater.
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that...
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that will use a while loop. The function will prompt the user to enter integers ine by one, until the user enters a negative value to stop. The function will display any integer that is less than 25. Declare and initialize any variables needed. The function takes no arguments and has a void return type.
Syntax error in C. I am not familiar with C at all and I keep getting...
Syntax error in C. I am not familiar with C at all and I keep getting this one error "c error expected identifier or '(' before } token" Please show me where I made the error. The error is said to be on the very last line, so the very last bracket #include #include #include #include   int main(int argc, char*_argv[]) {     int input;     if (argc < 2)     {         input = promptUserInput();     }     else     {         input = (int)strtol(_argv[1],NULL, 10);     }     printResult(input);...
Programming in C language (not C++) Write a function definition called PhoneType that takes one character...
Programming in C language (not C++) Write a function definition called PhoneType that takes one character argument/ parameter called "phone" and returns a double. When the variable argument phone contains the caracter a or A, print the word Apple and return 1099.99. When phone contains the caracter s or S print the word Samsung and return 999.99. When phone contains anything else, return 0.0.
I keep getting minor errors I can't figure out and I don't know how to convert...
I keep getting minor errors I can't figure out and I don't know how to convert decimal .10 to percentage 10% either.   With these functions defined now expand the program for a company who gives discounts on items bought in bulk. Create a main function and inside of it ask the user how many different items they are buying. For each item have the user input a price and quantity, validating them with the functions that you wrote. Use your...
C programming Write a function called string in() that takes two string pointers as arguments. If...
C programming Write a function called string in() that takes two string pointers as arguments. If the second string is contained in the first string, have the function return the address at which the contained string begins. For instance, string in(“hats”, “at”) would return the address of the a in hats. Otherwise, have the function return the null pointer. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
c++ programming: Write a function called baseConverter(string number, int startBase, int endBase) in c++ which converts...
c++ programming: Write a function called baseConverter(string number, int startBase, int endBase) in c++ which converts any base(startBase) to another (endBase) by first converting the start base to decimal then to the end base. Do not use library. (bases 1-36 only)
Need C++ code to be able to run, keep getting a constant value error #include #include...
Need C++ code to be able to run, keep getting a constant value error #include #include #include #include #include #include using namespace std; using namespace std::chrono; int c; void insertionSort(int* arr, int n) { for (int i = 1;i < n;i++) { int v = arr[i]; int j; for (j = i - 1;j > -1;j--) { c++; if (arr[j] > v) { arr[j + 1] = arr[j]; } else { break; } } arr[j + 1] = v; }...
C Programming build a function that is outside of main domain but can be called Build...
C Programming build a function that is outside of main domain but can be called Build a function that will : - Take 3 arrays and their lengths as input: Array 1 for even numbers, Array 2 for odd numbers. Both 1 and 2 have the same size - put all elements in array 3 elements from array 1 should be placed in the even indexes and elements of array 2 should be placed in the odd indexes, in increasing...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT