Need help outputting correct median in an array

When I input the numbers 1-10 it gives me a median of 5.50 instead of 4.50

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//This program is used to test the options used in Assignment 4 before inputting them in

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <bits/stdc++.h>

using namespace std;

void enterValues(int[], int);
void displayNumbers(int[], int);
void readNumbers(/*inout*/ifstream &, /*out*/int [], /*in*/int);
void initNumbers(/*out*/ int [], /*in*/int);
void findMedian(int [],const int);
void selectionSort(int [], int);
void swap(double&, double&);

int main()
{

    const int ARRAY_SIZE = 10;
    int integers[ARRAY_SIZE], tracker, choice, median;

    do
    {


    cout<<fixed<<showpoint<<setprecision(2); //manipulators set for output format

	// File stream object
	ifstream inFile;

    cout << "---------------------------------------------------------------------" << endl;
    cout << "1-D ARRAY PROCESSING MENU OPTIONS - ARRAY BASED LIST WITH 10 ELEMENTS" << endl;
    cout << "---------------------------------------------------------------------" << endl;
    cout << "1. New data set: Keyboard input(10 numbers from user)" << endl;
    cout << "2. New data set: File data read(10 numbers from the file, numbers.txt.)" << endl;
    cout << "3. Data set: Display current data set." << endl;
    cout << "4. Data set: Display median." << endl;
    cout << "5. Data set: Display mode." << endl;
    cout << "6. Data set: Check array elements - sorted or unsorted." << endl;
    cout << "7. Data set: Ascending sort - Selection Sort." << endl;
    cout << "8. New data set: Random numbers(10 numbers in range of values 1 - 200)." << endl;
    cout << "9. Data set: Binary Search for data item - found, not found and position" << endl;
    cout << "10.Exit program" << endl;

    cout << endl;

    cout << "Enter your choice: ";
    cin >> choice;

    switch(choice)
    {
        case 1: enterValues(integers, tracker);
                cout << endl;
                displayNumbers(integers, tracker);
            break;
        case 2: readNumbers(inFile, integers, tracker);
                cout << endl;
                displayNumbers(integers, tracker);
            break;
        case 3:
            break;
        case 4: selectionSort(integers, ARRAY_SIZE);
                findMedian(integers, ARRAY_SIZE);
            break;
        case 5:
            break;
        case 6:
            break;
        case 7:
            break;
        case 8:
            break;
        case 9:
            break;
        case 10:cout<<endl;
                cout<<"Array processing test now concluded. Exiting program ....."<<endl;
            break;
        default:  cout<<"INVALID CHOICE ...please retype"<<endl;
            break;
    }
    }while(choice != 10);


    return 0;
}

void enterValues(int integers[], int numCount)
{
    const int amount  = 10;

    cout << "Please enter ten values!" << endl;
    cout << endl;

    for(int numCount = 0; numCount < amount; numCount++)
    {
        cout << "Value #" << (numCount + 1) << ": ";
        cin >> integers[numCount];
    }
}

void displayNumbers(int integers[], int numCount)
{
    const int amount = 10;

   cout << " *** Current elements in the array based list *** \n";
   cout << "-------------------------------------------------------\n";

   for (numCount = 0; numCount < amount; numCount++)
    {
      cout<<"Element#"<< (numCount + 1) <<":";
      if(numCount==amount-1)
        cout<<setw (4) <<integers[numCount]<<endl;
      else
        cout<<setw (5) <<integers[numCount]<<endl;
   }
}

void readNumbers(/*inout*/ifstream &inFile, /*out*/int integers[], /*in*/int numCount)
{

    string fileName;
    const int amount = 10;

    cout<<"Please enter filename: ";
    cin>>fileName;
    inFile.open(fileName.c_str() );// Open the file

    //File not found validation routine
	while (!inFile) {
		cin.clear();
		cin.ignore(200, '\n');
		cout << "Error opening the file.\n";
        cout<<"Please enter filename: ";
        cin>>fileName;

        inFile.open(fileName.c_str());    // Open the file.
	}

        for (numCount = 0; numCount < amount; numCount++)
        inFile >> integers[numCount]; //file data read...note no validation for correct data items

        cout<<"File has successfully opened and "<< numCount <<" values have been read and stored."<<endl;
    inFile.close();
    inFile.clear();
}

void initNumbers(/*out*/ int integers[], /*in*/int numCount)
{
    const int amount = 10;

    for(numCount = 0; numCount < amount ; numCount++)
    {
        integers[numCount] = 0;
    }
}

void findMedian(int array[], const int size)
{

    double median;

    if (size % 2 == 0)
        {
            median = (array[size/2] + array[size/2-1])/2.0;
        }
    else
        median = array[size/2];

   cout << median << endl;

}

void selectionSort(int array[], int size)
{
   int minIndex, minValue;

   for(int start = 0; start < (size -1); start++)
   {
       minIndex = start;
       minValue = array[start];
       for(int index = start + 1; index < size; index++)
       {
           if(array[index] < minValue)
           {
               minValue = array[index];
               minIndex = index;
           }
       }
       swap(array[minIndex], array[start]);
   }
}

void swap(double &a, double &b)
{
    double temp = a;
    a = b;
    b = temp;
}

Last edited on
(1) Make median a double, since it can take values that end in .5
(2) Make sure that your array is sorted before you try to pick out the median.
Now i am having trouble with it not outputting 4.5 when inputting numbers 1-10
Depressed wrote:
Now i am having trouble with it not outputting 4.5 when inputting numbers 1-10

If you input the numbers 1-10 then the median is ... 5.5

There are 5 numbers less than it and 5 numbers greater than it. Now that's what I would call the middle ground!
Last edited on
I must be tired lmaooooo, thank you for making me laugh with that pun XD
Topic archived. No new replies allowed.