Having some trouble with my programing lab. Here is the assignment: Write a prog
ID: 3736426 • Letter: H
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 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<iostream>
#include<string>
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;
}
Explanation / Answer
#include<iostream>
#include<string>
using namespace std;
//function prototypes
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()
{
int number;
double scores[10];
cout << "How many test scores do you have?: ";
cin >> number;
cout << " Enter " << number << " Scores:" << endl;
for ( int i = 0; i < number; i++ )
{
cout << " Enter Test score" << (i + 1) << ": ";
cin >> scores[i];
while ( scores[i] < 0 )
{
cout << "No negative scores. " << endl;
cout << "Test " << (i + 1) << ": ";
cin >> scores[i];
}
}
cout << " Test scores :" ;
printscores(scores, number);
cout<<" Average score: "<<average(scores,number);
cout<<" highest score: "<<highest(scores,number);
cout<<" lowest score: "<<lowest(scores,number);
sortScores(scores,number);
cout<<" Sorted scores : ";
printscores(scores, number);
return 0;
}
void printscores(double *testscores, int number)
{
for (int i = 0; i < number; i++)
{
cout << *(testscores+i) << " ";// value at address (testscores+i), testscores holds the base address of score array
}
cout << endl;
}
void sortScores(double *array,int size) // *array denotes the address of values
{
bool swap;
double temp;
do
{
swap = false;
for(int count = 0;count<(size-1);count++)
{
if(*(array+count) > *(array+count+1)) // *(array+count) is for value at address (array+count)
{
temp = *(array+count);
*(array+count) = *(array+count+1);
*(array+count+1) = temp;
swap = true;
}
}
}while(swap);
}
double average(double* scores, int number)
{
double sum = 0;
double avg;
for (int i = 0; i < number; i++)
{
sum += *(scores+i); // add all scores
}
avg = (double)sum / number;
return avg;
}
double highest(double* scores, int number)
{
double temp = 0;
for (int i = 0; i < number; i++)
{
if (*(scores+i) > temp)
{
temp = *(scores+i);
}
}
return temp;
}
double lowest(double* scores, int number)
{
double temp = 999;
for (int i = 0; i < number; i++)
{
if (*(scores+i) < temp)
{
temp = *(scores+i);
}
}
return temp;
}
Output:
How many test scores do you have?:5
Enter 5 Scores:
Enter Test score1:5.6
Enter Test score2:7.8
Enter Test score3:4.8
Enter Test score4:7.9
Enter Test score5:6.8
Test scores :5.6 7.8 4.8 7.9 6.8
Average score: 6.58
highest score: 7.9
lowest score: 4.8
Sorted scores : 4.8 5.6 6.8 7.8 7.9
Do ask if any query. Please upvote
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.