# HELP Printing out Sort array PLEASE \[C++\]

**URL:** <https://forum.freecodecamp.org/t/help-printing-out-sort-array-please-c/55476>\
**Category:** General\
**Created:** [November 15, 2016, 3:53am UTC](https://forum.freecodecamp.org/t/help-printing-out-sort-array-please-c/55476 "2016-11-15T03:53:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![clopez0201](https://avatars.discourse-cdn.com/v4/letter/c/dec6dc/32.png) [@clopez0201](https://forum.freecodecamp.org/u/clopez0201)\
**Post date:** [November 15, 2016, 3:53am UTC](https://forum.freecodecamp.org/t/help-printing-out-sort-array-please-c/55476/1 "2016-11-15T03:53:58Z")

</div>

```auto
#include <iostream>
#include <iomanip>
using namespace std;

// Function prototype
void printArray(int *, int);
void sortArray(int *, int);
double getAverage(int *, int);
int getMaximum(int *, int);

int main()
{
	int *Movies, Stud, Max;
	double Avg;

	// Ask user how many students were surveyed.
	cout << "How many students were surveyed? ";
	cin >> Stud;
	while (Stud < 0)
	{
		cout << "Number cannot be negative: Please enter a nonnegative value: ";
		cin >> Stud;
	}

	Movies = new int[Stud];

	for (int i = 0; i < Stud; i++)
	{
		cout << "Student " << (i + 1) << ": ";
		cin >> Movies[i];
		while (Movies[i] < 0)
		{
			cout << "Number cannot be negative: Please enter a nonnegative value: ";
			cin >> Movies[i];
		}
	}

	Avg = getAverage(Movies, Stud);
	Max = getMaximum(Movies, Stud);

	cout << "------------- before sort --------\n";
	printArray(Movies, Stud);
	cout << "\n------------- after sort --------\n";
	sortArray(Movies, Stud);
	cout << fixed << showpoint << setprecision(2);
	cout << "\nThe Average number of movies seen is " << Avg << endl;
	cout << "The maximum value is " << Max << endl;

	delete[] Movies;
	Movies = 0;

	return 0;
}

// *******************************************************************************
// Printing Array *
// The function prints the array of the user enteries and stores it in an *
// array. *
// *******************************************************************************
void printArray(int *array, int size)
{
	int Stud;
	for (Stud = 0; Stud < size; Stud++)
		cout << array[Stud] << '\t';
}

// *******************************************************************************
// Sort by Selection *
// This function performs an ascending-order selection sort on array. The *
// parameter size holds the number of elements in the array. *
// *******************************************************************************
void sortArray(int *array, int size)
{
	for (int curr_sub = 0; curr_sub <= (size - 2); curr_sub++)
	{
		int curr_value = array[curr_sub];
		int min_sub = curr_sub;
		int min_value = curr_value;

		for (int compare_sub = curr_sub + 1; compare_sub < size; compare_sub++)
		{
			int compare_value = array[compare_sub];
			if (compare_value < min_value)
			{
				min_value = compare_value;
				min_sub = compare_sub;
				
			}
		}
		if (curr_sub != min_sub)
			swap(array[curr_sub], array[min_sub]);
	}
}
/*{
	int i, j;
	for (i = 0; i < (size - 1); i++)
	{
		int min = i;
		for (j = i + 1; j < size; j++)
		{
			if (array[j] < array[min])
			{
				min = j;
				cout << array[min] << '\t';
			}
		}
		if (min != i)
			swap(array[i], array[min]);
	}
}*/
// *******************************************************************************
// Getting the Average *
// This function calculates and returns the average of the values in array. The *
// parameter size holds the number of elements in array. * 
// *******************************************************************************
double getAverage(int *array, int size)
{
	double Sum = 0;
	for (int i = 0; i < size; i++)
	{
		Sum += *(array + i);
	}
	return Sum / size;
}

// *******************************************************************************
// Getting the Maxium *
// This function calculates and returns the median of the values in the array. *
// The parameter size holds the number of elements in the array. *
// *******************************************************************************
int getMaximum(int *array, int size)
{
	int i = (size - 1);
	int Max;

	if (size % 2 == 0)
	{
		Max = (*(array + i) + *(array + (i + 1)));
	}
	else
		Max = *(array + i);

	return Max;
}

```

---

<div class="post-metadata">

**Author:** ![kevcomedia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevcomedia/32/125737_2.png) [@kevcomedia](https://forum.freecodecamp.org/u/kevcomedia)\
**Post date:** [November 15, 2016, 3:57am UTC](https://forum.freecodecamp.org/t/help-printing-out-sort-array-please-c/55476/2 "2016-11-15T03:57:56Z")

</div>

I cleaned up your code.  
You need to use triple backticks to post code to the forum.  
See [this post for details](http://forum.freecodecamp.com/t/forum-code-formatting/25574?u=jacksonbates).

---

<div class="post-metadata">

**Author:** ![jwilkins.oboe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jwilkins.oboe/32/179497_2.png) [@jwilkins.oboe](https://forum.freecodecamp.org/u/jwilkins.oboe)\
**Post date:** [January 16, 2021, 5:31am UTC](https://forum.freecodecamp.org/t/help-printing-out-sort-array-please-c/55476/3 "2021-01-16T05:31:39Z")

</div>

Closed on January 16, 2021, 5:31am UTC


