Point me to the right direction (no pun intended)

I am trying to create a program that would collect the number of students and movies for each student. The program will then dynamically allocate it and calculate the average, median, and the mode(may ask later). I know I am not too far away from completing the program, but the program uses pointers and may use arrays which I am not used to it yet. Would someone point me to the right direction? (Please try to explain in Layman's term thank you.)

Here is the code:

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
//main.cpp
//I also use function because it was good practice
//for me, yet I don't know if I am doing things
//properly.

#include<iostream>
#include<cstdlib>
using namespace std;


int getInput(int *, int, int);//Should this be a void or int?

double getAverage(int *, int, int);//Is this correct? I used my book's ex.
double getMedian(int *, int, int);

int main()
{

	int students1 = 0; int movies1 = 0;  int number = 0;
	
	getInput(&students1, movies1, number);
	getAverage(&students1, movies1, number);
	getMedian(&students1,movies1, number);
	
	cout << getAverage(&students1,movies1, number);
	cout << getMedian(&students1,movies1, number);
	
  
 }



int getInput(int *point, int input, int num)
{	
	cout << "How many students are there to enter? ";
	cin  >> input;// The user enters # of students. 
	cout << endl;
	point = new int[input]; //Dynamically allocate it. Am I correct?
	
	if (point == 0)
        
        cout << "Error: memory could not be allocated"; //Safety feature.
	
	else
{
	cout << "Enter the number of movies watched for: "<<endl; 
        cout << endl;
    
	for (num = 0; num< input; num++) //counter that seems to work.
	 {  cout << "Student "<< num+1 <<": ";
		cin >> point[num]; //User enters the # of movies watched and
                                   //stores it??
	 }
	
	return num; //Should return the number of movies? or if void
                    // remove this?
}
}

double getAverage(int * point, int input, int num)//Correct?
{
double total = 0.0; double average = 0.0;
	cout << "The average of movies watched in one month is: "<<endl; 
        cout <<endl;
    for (num = 0; num < input; num++)
	 	total += point[num];
		average = (total / input);
		return average;
		
}

double getMedian(int * point, int input, int num)
{	
	double total = 0.0; double median = 0.0;
	cout << "The median of movies watched in one month is: "<<endl;
	for (num = 0; num<input; num++)
		total += point[num];
		median = ((input + 1) / 2);
		return median;
	delete[] point; //This may be in the wrong place.
}






/*
Output so far:

How many students are there to enter? 2

Enter the number of movies watched for:

Student 1: 3
Student 2: 2
The average of movies watched in one month is:

The median of movies watched in one month is:
The average of movies watched in one month is:

-1.#INDThe median of movies watched in one month is:
0

Press any key to continue . . .
*/
You're passing movies1 and average by value into the functions. This means when they're changed within the function the change is only within the scope of that function. Pass them by reference.
So I use the "&" correct?
Yep :)
Ok, let me see what happens.
Maybe I got confused in getAverage function.
1
2
3
4
5
6
7
8
9
10
11
double getAverage(int * point, int input, int num)
{
double total = 0.0; double *average;//This would be a pointer?
	cout << "The average of movies watched in one month is: "<<endl; cout <<endl;
    for (num = 0; num < input; num++)
	 	total += point[num];
		&average = (total / input);//&?
		return &average;
		
}

Well, I have to leave to work, but I will be back later on at night or so. Thanks Zaita.
double getAverage(int *point, int &input, int &num)

http://www.cplusplus.com/doc/tutorial/functions2/ will show you about reference parameters.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int getInput(int *point, int input, int num)
{	
	cout << "How many students are there to enter? ";
	cin  >> input;// The user enters # of students. 
	cout << endl;
	point = new int[input]; //memory leak (you will lose the address at the end of the function)
	//...
}


//getMedian(&students1,movies1, number); //caller
double getMedian(int * point, int input, int num)
{	
	double total = 0.0; double median = 0.0;
	cout << "The median of movies watched in one month is: "<<endl;
//	for (num = 0; num<input; num++) //these lines are worthless 
//		total += point[num];
		median = ((input + 1) / 2); // outside the loop 
		return median;
	delete[] point; //Trying to delete an static allocated variable (that is not an array, also)
//it didn't crash because you returned before
}
Think about the responsibilities of your functions.
Last edited on
Thank you guys, I am back. I'll post if I have any questions.
So, after many trials and errors I decided not to use functions until I study functions a bit more, but I have used a little from the dynamic memory allocation tutorial. But, now I am having issues with the median. Thank you for your time. ^^

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
#include<iostream>
#include<new>
using namespace std;

int main()
{

  int movie, students, count; 
  movie = 0; 
  int * point; 
  double avg = 0, total = 0; int med = 0;
  
  
        cout << "How many students would you like to enter? ";
        cin  >> students;
        cout << endl;
  point = new (nothrow) int[students];
  
  if (point == 0)
        cout << "Error: memory could not be allocated";
  else
  {
	cout << "Enter the number of movies watched for each student: "<<endl;;
	cout << endl;
    for (count = 0; count < students; count++)
    {
	cout << "Student "<< count+1 <<": ";
	 
        cin >> point[movie];
        total += point[movie];
	avg = (total / students);
     }
     
	cout << "The average of movies watched in a month is: "<<endl;
        cout << avg;//Checked and correct...funny this works, but not the median.
	cout << endl;
	
	cout << "The median of movies watched in a month is: " <<endl;
  	cout << endl;
    
	 
	med = (point[movie]+1)/2;//I'm sure that this is wrong, but I can't seem find a way to pull the middle number. Any suggestions?
	cout << med; // (see output).
	cout << endl;

	 //Would like to know how to get the mode as well. 
	 //(number occuring the most often and if not the program will display 
	 //"No mode found" or something like that).

	
}


  return 0;
}
Last edited on
Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
How many students would you like to enter? 5

Enter the number of movies watched for each student:

Student 1: 4
Student 2: 6
Student 3: 8
Student 4: 2
Student 5: 2
The average of movies watched in a month is:
4.4
The median of movies watched in a month is:

1
Press any key to continue . . . 
*/
point[ (students+1)/2];
Thank you :)
Topic archived. No new replies allowed.