Finding mean, median, mode (memory leak issue?)

So I've written my code to find mean, median, and mode from a user-created array, but my program keeps crashing before it even reaches the functions to find mean, median, and mode.

My code is as follows

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
  //Ask user for series of integers
//Input integers into a dynamic array
//Calculate mean, median, and mode

#include <iostream>
#include <cstdlib>

using namespace std;

void sortArray(int p[], int total);
double findMean(int p[], int total);
double findMedian(int p[], int total);
double findMode(int p[], int total);

int main(){
	int i, n, total;
	int* p;
	double mean, median, mode;
	cout << "How many numbers would you like to include? \n";
	cin >> i;
	p = new (nothrow) int[i];
	for (n = 0; n < i; n++){
		cout << "Enter a number: \t";
		cin >> p[n];
		}
	sortArray(&p[n], total);
	mean = findMean(&p[n], total);
	median = findMedian(&p[n], total);
	mode = findMode(&p[n], total);

	cout << "The mean of the integers is " << mean << endl;
	cout << "The median of the integers is " << median << endl;
	cout << "The mode of the integers is " << mode << endl;
	

system("PAUSE");
return 0;
}

void sortArray(int p[], int total){
	 int x, y, temp;
	 for (x = 0; x < total; x++){
	 	 for (y = 0; y < total-1; y++){
		 	 if(p[y] > p[y+1]){
			 		 temp = p[y+1];
					 p[y+1] = p[y];
					 p[y] = temp;
					 }
		}
	}
}

double findMean(int p[], int total){
	 int i;
	 double dsum = 0.0;
	 for (i = 0; i < total; i++){
	 	 dsum += p[i];
		 }
return (dsum/total);
}

double findMedian(int p[], int total){
	 double* dsorted = new (nothrow) double[total];
	 int x, y;
	 double temp;
	 for (x = 0; x < total; x++){
	 	 dsorted[x] = p[x];
	 	 for (y = 0; y < total-1; y++){
		 	 if(dsorted[y] > dsorted[y+1]){
			 		 temp = dsorted[y+1];
					 dsorted[y+1] = dsorted[y];
					 dsorted[y] = temp;
					 }
		}
	}
	double dmedian = 0.0;
	if ((total % 2) == 0){
	   dmedian = (dsorted[total/2] + dsorted[(total/2) - 1])/2.0;
	   }
	else{
		dmedian = dsorted[total/2];
		}
	delete [] dsorted;
return dmedian;
}

double findMode(int p[], int total){
	int* repetition = new (nothrow) int[total];
	int x, y;
    for (x = 0; x < total; x++) {
        repetition[x] = 0;
        while ((y < x) && (p[x] != p[y])) {
            if (p[x] != p[y]) {
                y++;
            }
        }
        (repetition[y])++;
    }
    int maxRepeat = 0;
    for (int x = 1; x < total; x++) {
        if (&repetition[x] > &repetition[maxRepeat]) {
            maxRepeat = x;
        }
    }
    delete [] repetition;
    return p[maxRepeat];
}


I can print out prompting the user to input numbers, but for some reason, the program crashes and stops running after that? Not sure what the problem is - memory leak? Help would be appreciated.
Last edited on
line 26
sortArray(&p[n], total);
n is now equal to i, so you're passing the address at the end of the array p instead of the beginning. Also total is never initialized so you pass garbage into sortArray().
Agreed, it should be simply sortArray(p, i);
That fixed my segfault error, but now I get the wrong values for mean, median, and mode.

I'll step through the code again to make sure those functions make sense, but since total is never initialized, should I change all of the totals to i?
Yes, change total to i, it makes more sense ;)
Topic archived. No new replies allowed.