Question
Having some trouble with my programing lab. Here is the assignment: Write a program that dynamically allocates an array in the freestore large enough to
Having some trouble with my programing lab. Here is the assignment:
Write a program that dynamically allocates an array in the freestore large enough to hold a user defined number of test scores as doubles. Once all the scores are entered, the array should be passed to a function that finds the highest score. Another function the array should be passed to a function that finds the lowest score. Another function should be called that calculates the average score. The program should show the highest, lowest and average scores. Must use pointer notation in the functions to pass the array. Add some user input protection to prevent negative numbers.
Required Prototypes:
// Function prototypes
//********************************************
// Function average *
// This function calculates and returns the *
// average of the values stored in the array *
// passed into the scores parameter. The *
// parameter numScores holds the number of *
// elements in the array. *
//********************************************
double average(double* score, int numScores);
double highest(double* score, int numScores);
double lowest(double* score, int numScores);
Example result:
How many test scores will you enter? -5
The number cannot be negative.
Enter another number: 5
Enter test score 1: 78
Enter test score 2: -10
Negative scores are not allowed.
Enter another score for this test: 10
Enter test score 3: 88
Enter test score 4: 98
Enter test score 5: 75
Average score: 69.80
highest score: 98.00
lowest score: 10.00
Still very new to programming, and my first time using pointers, so it might just be notation. Here is my code so far:
#include
#include
using namespace std;
double *testscores;
double *scores;
int i;
int number;
void printscores(double *testscores, int number);
void sortscores(double *testscores, int number);
double average(double* scores, int number);
double highest(double* scores, int number);
double lowest(double* scores, int number);
int main()
{
cout << "How many test scores do you have?: ";
cin >> number;
cout << "Enter " << number << " Scores:" << endl;
for ( double i = 0; i < number; i++ );
{
cout << "Test " << (i + 1) << ": ";
cin >> scores;
while ( scores < 0 )
{
cout << "No negative scores. " << endl;
cout << "Test " << (i + 1) << ": ";
cin >> scores;
}
testscores = scores;
}
cout << "Test scores:" << endl;
printscores(testscores, number);
}
void printscores(double *testscores, int number)
{
double current;
current = *testscores;
for (int i = 0; i < number; i++)
{
cout << current << " ";
current++;
}
cout << endl;
}
void sortscores(double *testscores, int number)
{
double *last;
double *start;
double *next = 0;
last = testscores + number;
for(double *start = testscores; start < last - 1; start++)
{
for (double *next = start + 1; next < last; next++);
{
if (*next < *start)
{
double *temp = start;
start = next;
*next = *temp;
}
}
}
}
double average(double* scores, int number)
{
double sum = 0;
double avg;
double *current = testscores;
for (int i = 0; i < number; i++)
{
sum += *current;
current++;
}
avg = (double)sum / number;
return avg;
}
double highest(double* scores, int number)
{
double *temp = 0;
for (int i = 0; i < number; i++)
{
if (scores > temp)
{
temp = scores;
}
}
return *scores;
}
double lowest(double* scores, int number)
{
double *temp = 0;
for (int i = 0; i < number; i++)
{
if (scores < temp)
{
temp = scores;
}
}
return *scores;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started