Answer to Question #345854 in C++ for Zee

Question #345854

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 averages with appropriate headings. Use pointer notation



rather than array notation whenever possible.



Input Validation: Do not accept negative numbers for test scores.



Example



Enter the number of test scores: 4



Enter each test score:



Test#1: 34



Test #2: 73



Test #3: 22



Test #4: 89



Sorted test scores:



Test #1: 22



Test #2: 34



Test #3: 73



Test #4: 89



Average = 54.5

1
Expert's answer
2022-05-30T15:15:39-0400
#include <iostream>
void sort(int* array, int count)
{
	int i, j, temp;
	for (i = 0; i < count - 1; i++)
		for (j = 0; j < count - 1 - i; j++)
			if (*(array + j) > *(array + j + 1))
			{
				temp = *(array + j + 1);
				*(array + j + 1) = *(array + j);
				*(array + j) = temp;
			}
}
float average(int* array, int count)
{
	int i;
	float result = 0;
	for (i = 0; i < count; i++)
		result = result + *(array + i);
	return result / count;
}
int main()
{
	int* array; int i; int count = -1; int value = -1;
	float averagescore;
	while (count <= 0)
	{
		std::cout << "Enter the number of test scores: ";
		std::cin >> count;
	}
	array = new int[count];
	std::cout << "Enter each test score: \n";
	for (i = 0; i < count; i++)
	{
		while (value <= 0)
		{
			std::cout << "Test #" << i + 1 << ": ";
			std::cin >> value;
		}
		*(array + i) = value;
		value = -1;
	}
	sort(array, count);
	std::cout << "Sorted test scores: \n";
	for (i = 0; i < count; i++)
		std::cout << "Test #" << i + 1 << ": " << *(array + i) << "\n";
	averagescore = average(array, count);
	std::cout << "Average = " << averagescore;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Nabila
09.04.23, 20:48

thank you soo much for helping me in my studies this is really very helpful

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS