Help with Vector Median

Hi there, I am pretty new here. I need some help with this statistics program i am writing for a c++ class I am taking. We are using Code Blocks 13.12. I am stuck on the median function. I wrote it and am not getting any errors or warnings.


For some reason the median always returns the same thing no matter the input:
6.9531e-308

What does this mean? Is this an error? What did I do wrong writing the median function?




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
117
118
119
120
121
122
#include <iostream>
#include <vector>


using namespace std;
void mysort(vector<int> &v);
double average (vector<int> v);
double median (vector<int>v);
int main(){

    int size, i, data, n;
    double mean, median;



    cout << "Please Enter the desired size of your array: ";
    cin >> size;

    vector<int>name(size);
    for (i=0; i < name.size(); i++) {
        cout << "array[" << i << "]: ";
        cin >> data;
        name[i] = data;

    }



    cout << "Sort: "; //sorts the array from least to greatest

    for (i=0; i < name.size(); i++) {
       mysort (name);

        cout << name[i] << ", ";
    }
    cout << '\n';
    cout << '\n';

    cout << "n: " << name.size() << '\n'; //returns size of array

    cout << '\n';

    mean = average (name);
    cout << "mean: " << mean << '\n'; //returns average



    cout << '\n';

    cout << "variance: " << '\n'; //sample variance

    cout << '\n';

    cout << "min: " << name[0] << '\n'; //returns min

    cout << '\n';

    cout << "q1: " << '\n'; //q1

    cout << '\n';

    cout << "median: " << median << '\n'; //returns median

    cout << '\n';

    cout << "q3: " << '\n'; //q3

    cout << '\n';

    cout << "max: " << name[name.size() - 1] << '\n'; //returns max



return 0;
}


void mysort (vector<int> &v) {
    int i, j, k;

 for (i = v.size(); i > 0; i--) {

  for (j = 0, k = 1; k < i; j++, k++) {

   if (v[j] > v[k]) {

    int temp = v[j];
    v[j] = v[k];
    v[k] = temp;
   }
  }
 }
}



double average (vector<int> v) {
    int sum = 0;
    for (int i = 0; i < v.size(); i++){
        sum += v[i];
    }
    //double average =
    return sum / v.size();

}






double median (vector<int>v) {
    double median;
    if ((v.size() % 2) == 0) {
        median = (v[(v.size() / 2) - 1] + v[v.size() / 2]) / 2;
    }
    else {
        median = v[v.size() / 2];
    }
    return median;

}
Last edited on
Here's a nice hint from my compiler:
(62): error C4700: uninitialized local variable 'median' used
could you tell me a bit more about what that means? does that mean i forgot to put something in or i put too many things in?
you never assign anything to your median variable, and you didnt initialise it so you're always gonna get garbage. effectively you've done this:

1
2
3
4
5
6
7
8
9
10
#include<iostream>

int main()
{
	double median;

	std::cout << median << std::endl;

	return 0;
}



You've written a function to calculate it, but you never call it.
Last edited on
what i changed is this:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>

using namespace std;

double middle;
int main() {
middle = median (name);
cout << "median: " << name[middle] << '\n';
 
}

double median (vector<int>v) {

if ((v.size() % 2) == 0) {
        median = (v[v.size() / 2 - 1] + v[v.size() / 2] ) / 2;
}
else {
median = v[v.size() / 2];
}
return median;
}


how does look? its now returning like 3,714,000......
how does look?

No offence, but it's terrible.
You're making the same mistake with your name variable (not even declaring it, let alone initialising it).

its now returning like 3,714,000


Your code does not even compile, so how on earth are you getting any results?
I suggest going through some of the tutorials on this site.
Topic archived. No new replies allowed.