Help Median wont show

Cant get the median for this array to show, I would greatly appreciate anyone who can help me

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
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>

double GetMedian (double, int);
int* addToSize (int*, int);

using namespace std;


    int main()
{   

    int userSize=0; //Holds user size
    int userInts; //Holds user values
    int *memory; //dynamically allocate an array


    //int  *intptr;
    //int *arrayNew;
    //int newArray;

    //Gets array size
    cout << "User(s) Please enter array size\n";
    cin >> userSize; 

    //Memory array 
    memory = new int [userSize];

    //Grab values for the amount of user size 
    for (int count = 0; count < userSize; count ++)
    {
        cout << "Please enter the value for " << count+1 << endl;
        cin >> userInts;
    }


    for (int index = 0; index < userSize; index ++)
    {
        cin >> memory[index];
    }

    //Sets addToSize function to memory array
    memory = addToSize(memory, userSize);

   //Shows memory array
    for(int index=0;index< (userSize + 1);index++)
        cout<<memory[index]<<endl;
        
        double GetMedian (double userSize, int);


    delete[] memory;    //Used to delete memory array
    memory = 0; //sets memory array to 0

    return 0;

}


    int* addToSize(int* arrayNew, int newSize) 
{
    int* expandSize= new int [newSize +1];

    for (int index = 0; index < newSize; index++)
    {
        expandSize[index]= arrayNew[index];
    }
    for (int index = newSize; index < (newSize+1); index ++) 
    {
        expandSize[index]=0;
    }   
    return expandSize;
}

//------------------------
//GetMedian
//Purpose: Will output Median for array, and show it to user
//-----------------------


double GetMedian(double userSize[], int iSize) 
{
    // Allocate an array of the same size and sort it.
    double* dpSorted = new double[iSize];
    for (int i = 0; i < iSize; ++i) 
    {
        dpSorted[i] = userSize[i];
    }
    for (int i = iSize - 1; i > 0; --i) 
    {
        for (int j = 0; j < i; ++j) 
        {
            if (dpSorted[j] > dpSorted[j+1]) 
            {
                double dTemp = dpSorted[j];
                dpSorted[j] = dpSorted[j+1];
                dpSorted[j+1] = dTemp;
            }
        }
    }

    // Middle or average of middle values in the sorted array.
    double dMedian = 0.0;
    if ((iSize % 2) == 0) 
    {
        dMedian = (dpSorted[iSize/2] + dpSorted[(iSize/2) - 1])/2.0;
    } else 
        {
        dMedian = dpSorted[iSize/2];
        }
    delete [] dpSorted;
    return dMedian;
}
Looks to me like you're doing way too much memory allocation.

What is the purpose of the following snippet:
1
2
3
4
5
6
    //Grab values for the amount of user size
    for (int count = 0; count < userSize; count ++)
    {
        cout << "Please enter the value for " << count+1 << endl;
        cin >> userInts;
    }


This loop is really not doing anything since you never use userInts anywhere else and you never save these values anywhere.

Why are you trying to increase the size of your array in addToSize()? And by the way this function has several problems. First you are accessing one of your arrays out of bounds. Second you have a memory leak.

I really suggest you consider using std::vectors instead of the problematic dynamic memory allocation.

Now to your actual question. You are never calling your median function so of course the median won't print.


Last edited on
Topic archived. No new replies allowed.