Pointers (Average Median)

Hi, this is the exercise:
write a program that can be used to gather statistical data about the number of movies college students see in a month. The program should ask the user how many students were surveyed and dynamically allocate an array of that size. The program should then allow the user to enter the number of movies each student has seen. The program should then calculate the average, and median of the values entered.

My problem is that i keep getting a error box that says:
Run-Time Check Failure #2 - Stack around the variable 'sNUM' was corrupted.
and then my program shuts down! Please help :)

This is what i have so far

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
#include <iostream>
#include <iomanip>

using namespace std;

void movAVG(int *, int);
void movMED(int *, int);

int main()
{
int sNUM,
sARRAY,
*sARRAYptr;

cout << "How many students were surveyed?";
cin >> sNUM;

sARRAYptr = new int[sNUM];

cout << "\n";
cout << "Now please enter the number of movies each student saw.";

for (int count = 0; count < sNUM; count++)
cin >> sARRAYptr[count];

sARRAY = *sARRAYptr;

movAVG(&sARRAY, sNUM);
movMED(&sARRAY, sNUM);

return 0;
}

void movAVG(int *array, int num)
{
double total = 0.0,
average = 0.0;

for (int count = 0; count < num; count++)
{
total += *array;
array++;
}

average = total / num;

cout << "The " << num << " college students watched on average about " << average << " movies.\n";
}

void movMED(int *array, int num)
{
int startScan, minIndex, minValue;

for (startScan = 0; startScan < (num - 1); startScan++)
{
minIndex = startScan;
minValue = array[startScan];
for(int index = startScan + 1; index < num; index++)
{
if (array[index] < minValue)
{
minValue = array[index];
minIndex = index;
}
}

array[minIndex] = array[startScan];
array[startScan] = minValue;
}

int first = 0,
middle,
last = num;
int uMIDDLE,
lMIDDLE;
double middle2;

if (num % 2 == 0)
{
lMIDDLE = ((first + last) / 2) - 1;
uMIDDLE = (first + last) / 2;

middle2 = (array[uMIDDLE] + array[lMIDDLE]) / 2;

cout << fixed << showpoint << setprecision(2);
cout << "The median value is: " << middle2 << endl;
}
else
{
middle = (first + last) / 2;
cout << "The median value is: " << array[middle] << endl;
}

}
1
2
3
4
sARRAY = *sARRAYptr;

movAVG(&sARRAY, sNUM);
movMED(&sARRAY, sNUM);


What is the point in dereferencing a pointer then taking the address of it again before passing? Finally you are just passing address of the variable sARRAY to these functions. Pass sARRAYptr directly
Topic archived. No new replies allowed.