Expression Must have Constant Value??

line 27 and line 88 Im having a hard time figuring it out what the error is.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include<iostream>
#include <cmath>
#include<algorithm>

using namespace 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 = new int[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);
}
Last edited on
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.
From Line 1 through 25 my instructor gave us the code from there on we would have to finish the program ourselves .

For line 88 would i copy line 25 and paste it above line 88?

I havent been to class in about 9 days lol and Im currently lost. Can you guide me with further assistance
Topic archived. No new replies allowed.