### 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

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