#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;
}
I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.
1 Like