#include<iostream>
#include <cmath>
#include<algorithm>
usingnamespace std;
//Prototypes
double Mean (int [], int);
double standardDeviation (int [], int, double);
int main() //Main Function
{
int n;
double avg = 0;
double median = 0;
double median2 = 0;
double sD;
int min=0;
int max = 0;
cout<<"How many numbers do you want to use? "; //Gets the size of the array
cin>>n;
int *ar = newint[n];
int tempAr[n];
cout<<endl<<"Please enter each number and press the enter key."<<endl; //Gets the contents of the array
for(int i = 0; i < n; ++i)
{
cin>>ar[i];
tempAr[i] = ar[i]; //a temp array
}
sort (tempAr, tempAr+n);//Sort into ascending order
min=tempAr[0]; //Traverses through the array and finds the smallest number
max=tempAr[n -1]; //Traverses through the array and finds the largest number
cout<<"\tThe smallest number is: "<<min<<endl;
cout<<"\tThe largest number is: "<<max<<endl;
avg = Mean(ar, n);
cout<<"\tThe mean is: "<<avg<<endl;
sD = standardDeviation(ar, n, avg);
cout<<"\tThe standard deviation is: "<< sD<<endl;
int med;
int med2;
if (n % 2 != 0)
{
med = (n/2);
median = ar[med];
median2 = tempAr[med];
}
else
{
med = (ar[n/2] + ar[(n/2) + 1])/2;
med2 = (tempAr[n/2] + tempAr[(n/2) + 1])/2;
median = med;
median2 = med2;
}
cout<<"\tThe median of your unsorted list is: "<<median<<endl;
cout<<"\tThe median of your sorted list is: "<<median2<<endl;
cout<<"\tThe numbers listed in ascending order are: ";
for(int k = 0; k < n; ++k)
cout<< tempAr[k]<<" ";
cout <<endl;
}
double Mean(int *ar, int n)
{
int sum = 0 ;
for ( int i=0; i<n; i++ )
sum += ar[i] ;
return sum / n ;
}
double standardDeviation (int ar[], int n, double avg)
{
double temp[n];
double sum2 = 0;
double sum3 = 0;
for(int i=0; i<n; i++) //Finds the standard deviation of the array
temp[i] = pow((ar[i] - avg), 2);
for (int a = 0; a < n; ++a)
sum2 += temp[a];
sum3 = sum2/(n-1);
return sqrt(sum3);
}
On line 26, you try to declare a static array of size n, which isn't a constant value.
Same issue on line 87.
To do that, you'd have to use dynamic allocation like you did on line 25.