Problem 2: Test Scores (Chapter 9: Pointers & chapter 8: Searching and Sorting)
ID: 3912598 • Letter: P
Question
Problem 2: Test Scores (Chapter 9: Pointers & chapter 8: Searching and Sorting) 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, the array should be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score. The program should display the sorted list of scores and the average. Use pointer notation rather than array notation whenever possible. Input validation: Do not accept negative numbers for test scores. b. Drop Lowest Score Modify program (problem 2) so the lowest test score is dropped. This score should not be included in the calculation of the average.Explanation / Answer
Problem 2:
a.
#include <iostream>
using namespace std;
void sort(int*,int);
int main() {
int n,s;
cout<<"Enter the number of scores : ";
cin>>n;
int *scores = new int[n];
cout<<" Enter the "<<n<<" scores : ";
for(int i=0;i<n;i++)
{
cin>>s;
if(s < 0)
{
cout<<" Score cannot be negative. Enter positive value ";
i--;
}
else
*(scores+i) = s;
}
sort(scores,n);
double avg = 0;
cout<<" Sorted list of scores : ";
for(int i=0;i<n;i++)
{
cout<<*(scores+i)<<" ";
avg = avg+ *(scores+i);
}
cout<<" Average score : "<<avg/n;
return 0;
}
void sort(int *array,int size)
{
bool swap;
int 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);
}
Output:
Enter the number of scores :5
Enter the 5 scores :34 -56
Score cannot be negative. Enter positive value 56 48 72 58
Sorted list of scores : 34 48 56 58 72
Average score : 53.6
b.
#include <iostream>
using namespace std;
void sort(int*,int);
int minScore(int* ,int);
int main() {
int n,s;
cout<<"Enter the number of scores : ";
cin>>n;
int *scores = new int[n];
cout<<" Enter the "<<n<<" scores : ";
for(int i=0;i<n;i++)
{
cin>>s;
if(s < 0)
{
cout<<" Score cannot be negative. Enter positive value ";
i--;
}
else
*(scores+i) = s;
}
sort(scores,n);
double avg = 0;
cout<<" Sorted list of scores : ";
for(int i=0;i<n;i++)
{
cout<<*(scores+i)<<" ";
avg = avg+ *(scores+i);
}
int minimum = minScore(scores,n);
cout<<" Minimum score : "<<minimum;
cout<<" Average score after dropping minimum score : "<<(avg-minimum)/(n-1);
return 0;
}
void sort(int *array,int size)
{
bool swap;
int 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);
}
int minScore(int *array,int size)
{
int min = 1000;
for(int i=0;i<size;i++)
{
if(*(array+i) < min)
min = *(array+i);
}
return min;
}
Output:
Enter the number of scores :5
Enter the 5 scores :34 -56
Score cannot be negative. Enter positive value 56 48 72 58?
Sorted list of scores : 34 48 56 58 72
Minimum score : 34
Average score after dropping minimum score : 58.5
Do ask if any doubt. Please upvote.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.