Passing Paramaters as Pointers? I Don't Understand?

I keep getting funny numbers at the end and i am not sure why??

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

using namespace std;

int main()
{
	int *studentnum;
	int students_surveyed;
	int count;
	int total;
	double average;
	double median;

cout<<"How many student are surveyed?"<< endl;
cin>> students_surveyed;

studentnum = new int [students_surveyed];

cout<<"Write by order, the number of movies watched by each of the students" <<endl;
for (count = 0; count<students_surveyed ; count++)
{
	cout << "Student#"<< count +1 << ":" << endl;
	cin >> studentnum [count];
}

total += studentnum [count];

average = total/students_surveyed;

{
cout<< "The total of movies watched by all the students is:" << total << "movie." <<endl;
}

{
cout<< "The Average of movies watched is:" << average << "movie." << endl;
}

median = total / 2 ;

{
cout<< "The median of movies watched is:" << median << "movies." <<endl;
}

delete [] studentnum;
studentnum = 0;

return 0;
}



How many student are surveyed?
5
Write by order, the number of movies watched by each of the students
Student#1:
10
Student#2:
8
Student#3:
10
Student#4:
12
Student#5:
12
The total of movies watched by all the students is:1095516538movie.
The Average of movies watched is:2.19103e+008movie.
The median of movies watched is:5.47758e+008movies.

--------------------------------
Process exited with return value 0
Press any key to continue . . .


Here Is What I Am Suppose To Do.


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 perform the following steps:

1. Ask the user how many students were surveyed. Dynamically allocate an array with this number of elements.
2. Allow the user to enter the number of movies each student saw into the array. The question for the entry should read : "How many movies did student X see?" where you substitute the number of the student for X. Remember that your array index will start at zero but that you should be asking for student 1, student 2, etc. Do not allow negative numbers to be entered.
3. Write functions to do the following:
1. Sort the array from lowest to highest number of movies viewed.
2. Calculate the average number of movies viewed by the students. The average is the sum of all of the movies viewed divided by the number of students. Display the average number of movies viewed by the students.
3. Determine the median number of movies for all the students. The median is is the middle value of the array, For example, if 5 students were entered, the number of movies viewed by student 3 is the median value. If an even number of students is entered, the median is the average of the 2 values in the middle. Display the median value of the array.
4. Compute the mode of the array. The mode is the number of movies viewed most frequently. For example, in an array of 5 students who viewed 5, 3, 8, 5, and 2 movies, 5 will be the mode because 2 students viewed that number of movies. If the array has no mode (all of the numbers are different), display a message indicating that there is no mode.
4. The array should be passed to each of the functions as a pointer to the array.

Last edited on
Line 27 looks misplaced.
yes.

OP, the total is the sum off all the elements in your array, and
total += studentnum[count];
is not doing that. Just do it in your loop like pete said:

1
2
3
4
5
6
for (count = 0; count < students_surveyed; count++)
	{
		cout << "Student#" << count + 1 << ":" << endl;
		cin >> studentnum[count];
		total += studentnum[count];
	}


and get rid of the braces around your couts. they're not needed.
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
#include<iostream>
#include <iomanip>

using namespace std;

int main()
{
	int *studentnum;
	int students_surveyed;
	int count;
	int total;
	double average;
	double median;

cout<<"How many student are surveyed?"<< endl;
cin>> students_surveyed;

studentnum = new int [students_surveyed];

cout<<"Write by order, the number of movies watched by each of the students" <<endl;
for (count = 0; count<students_surveyed ; count++)
{
	cout << "Student#"<< count +1 << ":" << endl;
	cin >> studentnum [count];
	total += studentnum [count];
}

average = total/students_surveyed;

cout<< "The total of movies watched by all the students is:" << total << "movie." <<endl;

cout<< "The Average of movies watched is:" << average << "movie." << endl;

median = total / 2 ;

cout<< "The median of movies watched is:" << median << "movies." <<endl;

delete [] studentnum;
studentnum = 0;

return 0;
}



Ok Here Is The Output Of The Fixed Code.
But, The Median Is Still Calculating Wrong And How Would I Get The Mode?



How many student are surveyed?
10
Write by order, the number of movies watched by each of the students
Student#1:
10
Student#2:
12
Student#3:
10
Student#4:
9
Student#5:
8
Student#6:
3
Student#7:
6
Student#8:
10
Student#9:
13
Student#10:
20
The total of movies watched by all the students is:156movie.
The Average of movies watched is:15movie.
The median of movies watched is:78movies.

--------------------------------
Process exited with return value 0
Press any key to continue . . .
Although you've declared median and average to be doubles, the assignment statement are actually assigning integers. In this statement: average = total/students_surveyed;, the program does this:
- compute total/students_surveyed. Since both are integers, it computes an integer result.
- convert the integer result of the division to a double
- assign the double to average.

To fix this you need to convert total or students_surveyed to a double before the division:
average = static_cast<double>(total) / students_surveyed;

To compute the median change line 34 to:
median = total / 2.0.
But I'm not sure that you have the formula right there. To find the median of a list of numbers you need to sort the numbers and find the one in the middle (if there are an odd number) or the average of the two in the middle (if there are an odd number).
Topic archived. No new replies allowed.