ComputerScienceExpert

(11)

$18/per page/

About ComputerScienceExpert

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Applied Sciences,Calculus See all
Applied Sciences,Calculus,Chemistry,Computer Science,Environmental science,Information Systems,Science Hide all
Teaching Since: Apr 2017
Last Sign in: 11 Weeks Ago
Questions Answered: 4870
Tutorials Posted: 4863

Education

  • MBA IT, Mater in Science and Technology
    Devry
    Jul-1996 - Jul-2000

Experience

  • Professor
    Devry University
    Mar-2010 - Oct-2016

Category > Programming Posted 27 Apr 2017 My Price 9.00

Write a program that dynamically allocates an array

My code continues to give me an error.  I can't find the problem.
Assignment 1

Write a program that dynamically allocates an array large enough to hold a user-defined number of test scores. Once all the scores are entered by the user, the array must be passed to a function that sorts them in ascending order. It must use another function that calculates the average score. The program should display the sorted list of scores and average with appropriate headings. The program must use pointer notation instead of array notation. Validation: Do not accept negative numbers for test scores; keep prompting the user for a new grade. Do not accept negative numbers for the number of scores the user wants to enter. The output should look like this:

Score

67.40

67.80

77.60

99.60

Average Score: 78.10

Program must have the following functions

  • void getGrades(double* score, int size)
  • void displayGrades(double* score, int size, double avg)
  • void sort(double* score, int size)
  • double average(double* score, int numScores)

 

 

// Week 2 Assignment 1
// Anthony J. Bamber
/*
Write a program that dynamically allocates an array large enough to hold a
user-defined number of test scores. Once all the scores are entered by the
user, the array must be passed to a function that sorts them in ascending
order. It must use another function that calculates the average score. The
program should display the sorted list of scores and average with appropriate
headings. The program must use pointer notation instead of array notation.
Validation: Do not accept negative numbers for test scores; keep prompting
the user for a new grade. Do not accept negative numbers for the number of
scores the user wants to enter. The output should look like this:
Score
67.40
67.80
77.60
99.60
Average Score: 78.10
Program must have the following functions
void getGrades(double* score, int size)
void displayGrades(double* score, int size, double avg)
void sort(double* score, int size)
double average(double* score, int numScores)
*/
#include<iostream>
#include<iomanip>
using namespace std;
//Function Prototypes
void getGrades(double* score, int size);
void displayGrades(double* score, int size, double avg);
void sort(double* score, int size);
double average(double* score, int numScores);
//Variables
double* score;
int numTests;
int main()
{
cout << " Test Score Calculator" << endl;
cout << " Please enter the number of tests to calculate? ";
cin >> numTests;
cout << endl;
//Check entry
while (numTests <= 0)
{
cout << " Please enter a valid number of tests!\n\n";
cout << " How many scores do you want to enter? ";
cin >> numTests;
cout << endl;
}
score = new double[numTests]; } getGrades(score, numTests);
sort(score, numTests);
displayGrades(score, numTests, average(score, numTests));
system("pause");
return 0; // Input the grades
void getGrades(double* score, int size)
{
for (int count = 0; count < size; count++)
{
cout << " Score: " << (count + 1) << " : ";
cin >> score[count];
cout << endl; } if (score[count]<0)
{
cout << " Please enter a valid test score!\n\n";
count--;
} }
// Show average
void displayGrades(double* score, int size, double avg)
{
cout << " Score: " << endl;
for (int count = 0; count < size; count++)
cout << " " << fixed << setprecision(2) << score[count] << endl;
cout << endl;
cout << " Average Score: " << fixed << setprecision(2) << avg << "\n" <<
endl;
}
// Sort
void sort(double* score, int size)
{
for (int newHigh = 0; newHigh <= size - 2; newHigh++)
{
int low = newHigh;
for (int position = newHigh; position <= size - 1; position++)
{
if (*(score + position) <*(score + low))
low = position;
}
if (low != newHigh)
{
double temp = *(score + low);
*(score + low) = *(score + newHigh);
*(score + newHigh) = temp;
}
}
}
// Determine average
double average(double* score, int numTests)
{
double sum = 0;
for (int count = 0; count < numTests; count++)
sum = sum + *(score + count);
return sum / numTests; system("pause");
return 0;
}

Answers

(11)
Status NEW Posted 27 Apr 2017 03:04 AM My Price 9.00

-----------

Not Rated(0)