help with my median function

Write your question here.
my professor gave us function prtotypes to work with and we have to use those and I don't get any errors with my code but it keeps on saying that i have an exception error and i have no clue what to do I've just been staring at my code for hours
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  
#include "stdafx.h"
#include <iostream>
using namespace std;

double calcmedian(int*, int);
int calcmode(int*, int);
int *makeArray(int);
void getMovieData(int*, int);
void selectionsort(int[], int);
double calcavg(int*, int);

int main()
{
	int *nums = 0;
	int num_student = 0;
	
	getMovieData(nums, num_student);
	*makeArray(num_student);
	selectionsort(nums, num_student);

	// display the results.
	cout << "\nthe mean is: ";
	cout << calcavg(nums, num_student) << endl;

	cout << "\nthe mode is: ";
	cout << calcmode(nums, num_student) << endl;

	cout << "\nthe median is: ";
	cout << calcmedian(nums, num_student) << endl;

	delete[] nums;
	nums = 0;

	system("pause");
    return 0;
}

double calcmedian(int *nums, int num_student)
{
	double median;
		if (num_student % 2 == 0)
		{
				median = (nums[num_student / 2] + nums[(num_student / 2) + 1]) / 2;
		}
		else
			median = nums[num_student / 2];
		return median;
}

int calcmode(int *nums, int num_student)
{
	int mode = 0;
	int val = 0;
	int index;
		for (index = 0; index < num_student; index++)
		{
				if (*(nums + index) == *(nums + (index + 1)))
				{
						mode++;
						val = *(nums + index);
				}
		}
		if (val > 0)
			return val;
		else
			return -1;
}

int * makeArray(int num_student)
{
	int count;
	int *nums;
	nums = new int[num_student];
	if (nums == NULL) {
		cout << "Error allocating memory!\n";
	}
	cout << "Enter the how many movies each student saw: ";
	for (count = 0; count < num_student; count++) {
		cout << "Student " << (count + 1) << ": ";
		cin >> nums[count];
	}
	return nums;
}

void getMovieData(int *array, int size)
{
	cout << "How many students were surveyed? ";
	cin >> size;
	if (size < 0)
	{
			cout << "Invalid number of students!\n";
	}
}


void selectionsort(int array[], int size)
{
	int startScan, minIndex, minValue;

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

double calcavg(int *array, int size)
{
	int total = 0;
	double avg = 0.0;

	for (int i = 0; i < size; ++i) {
		total += array[i];
	}
	avg = total / size;
	return avg;
}
A couple of points: the way you're using getMovieData (line 18) is completely useless. You're only modifying the local copy of size, rather than the value you passed in.

Also, line 19 is useless. You're just throwing away the information you got. Incidentally, this would be where your exception is occurring; you can't make an array of size 0.

The way it's set up, you should be getting the value of num_students in main, assigning nums to the return value of makeArray, and using getMovieData for something completely different than what it currently is.

thanks for the help I tried making the improvements but I keep getting this exception error

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "stdafx.h"
#include <iostream>
using namespace std;

double calcmedian(int*, int);
int calcmode(int*, int);
int *makeArray(int);
void getMovieData(int*, int);
void selectionsort(int[], int);
double calcavg(int*, int);

int main()
{
	int *nums;
	int num_student = 0;
	nums = &num_student;

	getMovieData(nums, num_student);
	*makeArray(num_student);
	nums = nums;
	selectionsort(nums, num_student);

	// display the results.
	cout << "\nthe mean is: ";
	cout << calcavg(nums, num_student) << endl;

	cout << "\nthe mode is: ";
	cout << calcmode(nums, num_student) << endl;

	cout << "\nthe median is: ";
	cout << calcmedian(nums, num_student) << endl;

	delete[] nums;
	nums = 0;

	system("pause");
    return 0;
}

double calcmedian(int *nums, int num_student)
{
	double median;
		if (num_student % 2 == 0)
		{
				median = (nums[num_student / 2] + nums[(num_student / 2) + 1]) / 2;
		}
		else
			median = nums[num_student / 2];
		return median;
}

int calcmode(int *nums, int num_student)
{
	int mode = 0;
	int val = 0;
	int index;
		for (index = 0; index < num_student; index++)
		{
				if (*(nums + index) == *(nums + (index + 1)))
				{
						mode++;
						val = *(nums + index);
				}
		}
		if (val > 0)
			return val;
		else
			return -1;
}

int * makeArray(int num_student)
{
	int count;
	int *nums;
	nums = new int[num_student];
	if (nums == NULL) {
		cout << "Error allocating memory!\n";
	}
	cout << "Enter the how many movies each student saw: ";
	for (count = 0; count < num_student; count++) {
		cout << "Student " << (count + 1) << ": ";
		cin >> nums[count];
	}
	return nums;
}

void getMovieData(int *array, int num_students)
{
	cout << "How many students were surveyed? ";
	cin >> num_students;
	if (num_students < 0)
	{
			cout << "Invalid number of students!\n";
	}

}


void selectionsort(int array[], int size)
{
	int startScan, minIndex, minValue;

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

double calcavg(int *array, int size)
{
	int total = 0;
	double avg = 0.0;

	for (int i = 0; i < size; ++i) {
		total += array[i];
	}
	avg = total / size;
	return avg;
}
Last edited on
First you need to get your array in main, before you can do anything with it.
Normally you would ask the size of the array in main and pass it to make_array.
When you have your array you can get the input and do your calculations.
In other words, why are you doing this?
*makeArray(num_student);

You're doing nothing with your return value. You should be doing something like
int* nums = makeArray(num_students);
Topic archived. No new replies allowed.