how do you pass an array to main from a function?

Hello so i am having a problem making an array with in a function and it is causing my computer program to crash. the function i am having a problem with is the makeArray function within my code When you ask how many students were surveyed it will dynamically allocate array of the needed size is what it should do but mine keeps crashing so i added the line movies = new int[student]; after asking how many students in the makeArray function but it still crashes so the question is how do i make it so makeArray dynamically allocates the correct size of an array after asking how many students were surveyed then pass that to main as to work with the rest of my code i have been trying to put off asking but its due today and im at a real lost on what to do

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
128
129
130
131
132
133
  	
#include <iostream>
#include <iomanip>
using namespace std;

double median(int *, int);
int mode(int *, int);
int *makeArray(int);
void getMovieData(int *, int);
void selectionSort(int [], int);
double average(int *, int);

int main()
{
	double average1, median1;
	int *movies, students, mode1;


	*makeArray(students);
	getMovieData(movies, students);
	selectionSort(movies, students);
	
	average1 = average(movies, students);
	median1 = median(movies, students);
	mode1 = mode(movies, students);
	
	cout << "Here are the results for the number of movies college students watch in a month.";
	cout << fixed << showpoint << setprecision(2);
	cout << "The average is " << average1 << endl;
	cout << "The median is " << median1 << endl;
	cout << "The mode is " << mode1 << endl;
	
	delete [] movies;
	movies = 0;
	
	return 0;
}

int *makeArray(int students)
{
	int *movies;
	cout << "How many students were surveyed? ";
	cin	 >> students;
	movies = new int[students];
}

void getMovieData (int *array, int size)
{
	cout << "How many movies did each student see";
	for (int index = 0; index < size; index++)
	{
		cout << "Student " << (index + 1);
		cin >> *(array + 1);
	}
}

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 average(int *array, int size)
{
	double sum = 0;
	for (int index = 0; index < size; index++)
	{
		sum += *(array +index);
	}
	return sum / size;
}

double median(int *array, int size)
{
	int middle;
	double median;
	
	middle = size / 2;
	
	if(size % 2)
	{
		median = array[middle + 1];
	}
	
	else
	{
		median = (array[middle] + array[middle + 1]) / 2.0;	
	}
	return median;
}

int mode(int *array, int size)
{
	int num = array[0];
	int mode = num;
	int count = 1;
	int countMode = 1;
	
	for (int index = 1; index < size; index++)
	{
		if (array[index] == num)
		{
			count++;
		}
		else
		{
			if (count > countMode)
			{
				countMode = count;
				mode = num;
			}
			count = 1;
			num = array [index];
		}
	}
	return mode;
}
Line 16: students is an uninitialized variable.

Line 19: You don't save the pointer returned by makeArray(). You're passing an uninitialized variabled to makeArray. The * makes no sense.

Line 33: You're trying to delete an uninitialized pointer.

Line 39: students should be a local variable, not an argument.

Line 44: You never return the pointer you allocated. Your compiler should have warned you about no return statement.

Line 53: Why are you adding 1 to array? This is going to cause an out of bounds reference.

does it have to be initialized for this to work and if so what would i even initialize it as i understand it would changer later in the code anyways but thats the reason i saw no need for it.

the teacher said we have to use the * (well he said to use the functions as is at first i dint use it and the program was working smoothly but after adding it in im lost and really have no clue what to do "code wise" within)

yea i know but i use to have
cout << "How many students were surveyed? ";
cin >> students;
movies = new int[students];
in main an that seemed to work so i really didnt mind leaving it there as the program was supose to kinda just work as before.

yea there was no error and even if i was to try and return movies it would still crash

yea still learning on that one sorry (sorry editing this one it was so that when displaying the student it started with student 1 and then so on )

so how would you fix the problem (i thank you even looking and taking the time to help me but....) i still get the crashes and there is nothing addressing that. and that is my main concern my program keeps crashing after asking How many students were surveyed? and i see no way for me to fix it.
Last edited on
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 <iostream>
#include <iomanip>
using namespace std;

double median(int *, int);
int mode(int *, int);
void getMovieData(int *, int);
void selectionSort(int [], int);
double average(int *, int);

int main()
{
	double average1, median1;
	int *movies, students, mode1;


	cout << "How many students were surveyed? ";
	cin	 >> students;
	movies = new int[students];
	
	getMovieData(movies, students);
	selectionSort(movies, students);
	
	average1 = average(movies, students);
	median1 = median(movies, students);
	mode1 = mode(movies, students);
	
	cout << "Here are the results for the number of movies college students watch in a month.\n";
	cout << fixed << showpoint << setprecision(2);
	cout << "The average is " << average1 << endl;
	cout << "The median is " << median1 << endl;
	cout << "The mode is " << mode1 << endl;
	
	delete [] movies;
	movies = 0;
	
	return 0;
}


void getMovieData (int *array, int size)
{
	cout << "How many movies did each student see \n";
	for (int index = 0; index < size; index++)
	{
		cout << "Student " << (index + 1) << ": ";
		cin >> *(array + 1);
	}
}

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 average(int *array, int size)
{
	double sum = 0;
	for (int index = 0; index < size; index++)
	{
		sum += *(array +index);
	}
	return sum / size;
}

double median(int *array, int size)
{
	int middle;
	double median;
	
	middle = size / 2;
	
	if(size % 2)
	{
		median = array[middle + 1];
	}
	
	else
	{
		median = (array[middle] + array[middle + 1]) / 2.0;	
	}
	return median;
}

int mode(int *array, int size)
{
	int num = array[0];
	int mode = num;
	int count = 1;
	int countMode = 1;
	
	for (int index = 1; index < size; index++)
	{
		if (array[index] == num)
		{
			count++;
		}
		else
		{
			if (count > countMode)
			{
				countMode = count;
				mode = num;
			}
			count = 1;
			num = array [index];
		}
	}
	return mode;
}

this how it use to be also follow up question why does the median average and mode give ridiculous answers
Last edited on
1
2
3
4
5
for (int index = 0; index < size; index++)
{
    cout << "Student " << (index + 1) << ": ";
    cin >> *(array + 1);
}

In getMovieData(), you're constantly overwriting the second element(index 1) of array. As a result, every other element in array is uninitialised giving garbage values when you try to do calculations using array.
This is how you are meant to do it
1
2
3
4
5
for (int index = 0; index < size; index++)
{
    cout << "Student " << (index + 1) << ": ";
    cin >> array[index];
}


sum += *(array +index);
I'm not sure why you would write it in a longwinded fashion, when you can just use the subscript operator which does the same thing more concisely.
sum += array[index];
Topic archived. No new replies allowed.