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
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <set>

using namespace std;

int main()
{
char z;
float characters=0, dig, di=0, ca=0, cb=0, cc=0, cd=0, ce=0, cf=0, cg=0, ch=0, ci=0, cj=0, ck=0, cl=0, cm=0, cn=0, co=0, cp=0, cq=0, cr=0, cs=0, ct=0, cu=0, cv=0, cw=0, cx=0, cy=0, cz=0;
float a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, zz;
ifstream inData;
FILE *file1;
file1 = fopen("LAB5.dat", "r");

//counts specific characters and digits
while(!feof(file1))
{
fscanf(file1,"%c",&z);
characters++;
if(z=='A' || z=='a')
{ca++;
a=ca;}
else if(z=='B' || z=='b')
{cb++;
b=cb;}
else if(z=='C' || z=='c')
{cc++;
c=cc;}
else if(z=='D' || z=='d')
{cd++;
d=cd;}
else if(z=='E' || z=='e')
{ce++;
e=ce;}
else if(z=='F' || z=='f')
{cf++;
f=cf;}
else if(z=='G' || z=='g')
{cg++;
g=cg;}
else if(z=='H' || z=='h')
{ch++;
h=ch;}
else if(z=='I' || z=='i')
{ci++;
i=ci;}
else if(z=='J' || z=='j')
{cj++;
j=cj;}
else if(z=='K' || z=='k')
{ck++;
k=ck;}
else if(z=='L' || z=='l')
{cl++;
l=cl;}
else if(z=='M' || z=='m')
{cm++;
m=cm;}
else if(z=='N' || z=='n')
{cn++;
n=cn;}
else if(z=='O' || z=='o')
{co++;
o=co;}
else if(z=='P' || z=='p')
{cp++;
p=co;}
else if(z=='Q' || z=='q')
{cq++;
q=cq;}
else if(z=='R' || z=='r')
{cr++;
r=cr;}
else if(z=='S' || z=='s')
{cs++;
s=cs;}
else if(z=='T' || z=='t')
{ct++;
t=ct;}
else if(z=='U' || z=='u')
{cu++;
u=cu;}
else if(z=='V' || z=='v')
{cv++;
v=cv;}
else if(z=='W' || z=='w')
{cw++;
w=cw;}
else if(z=='X' || z=='x')
{cx++;
x=cx;}
else if(z=='Y' || z=='y')
{cy++;
y=cy;}
else if(z=='Z' || z=='z')
{cz++;
zz=cz;}
else if(z=='0' || z=='1' || z=='2' || z=='3' || z=='4' || z=='5' || z=='6' || z=='7' || z=='8' || z=='9')
{di++;
dig=di;}
}
//checking logic
characters--;
cout << "The letter A occurs " << ca << " times and the ratio to non white space is " << setprecision(3) << (ca/characters) << endl;
cout << "The letter B occurs " << cb << " times and the ratio to non white space is " << setprecision(3) << (cb/characters) << endl;
cout << "The letter C occurs " << cc << " times and the ratio to non white space is " << setprecision(3) << (cc/characters) << endl;
cout << "The letter D occurs " << cd << " times and the ratio to non white space is " << setprecision(3) << (cd/characters) << endl;
cout << "The letter E occurs " << ce << " times and the ratio to non white space is " << setprecision(3) << (ce/characters) << endl;
cout << "The letter F occurs " << cf << " times and the ratio to non white space is " << setprecision(3) << (cf/characters) << endl;
cout << "The letter G occurs " << cg << " times and the ratio to non white space is " << setprecision(3) << (cg/characters) << endl;
cout << "The letter H occurs " << ch << " times and the ratio to non white space is " << setprecision(3) << (ch/characters) << endl;
cout << "The letter I occurs " << ci << " times and the ratio to non white space is " << setprecision(3) << (ci/characters) << endl;
cout << "The letter J occurs " << cj << " times and the ratio to non white space is " << setprecision(3) << (cj/characters) << endl;
cout << "The letter K occurs " << ck << " times and the ratio to non white space is " << setprecision(3) << (ck/characters) << endl;
cout << "The letter L occurs " << cl << " times and the ratio to non white space is " << setprecision(3) << (cl/characters) << endl;
cout << "The letter M occurs " << cm << " times and the ratio to non white space is " << setprecision(3) << (cm/characters) << endl;
cout << "The letter N occurs " << cn << " times and the ratio to non white space is " << setprecision(3) << (cn/characters) << endl;
cout << "The letter O occurs " << co << " times and the ratio to non white space is " << setprecision(3) << (co/characters) << endl;
cout << "The letter P occurs " << cp << " times and the ratio to non white space is " << setprecision(3) << (cp/characters) << endl;
cout << "The letter Q occurs " << cq << " times and the ratio to non white space is " << setprecision(3) << (cq/characters) << endl;
cout << "The letter R occurs " << cr << " times and the ratio to non white space is " << setprecision(3) << (cr/characters) << endl;
cout << "The letter S occurs " << cs << " times and the ratio to non white space is " << setprecision(3) << (cs/characters) << endl;
cout << "The letter T occurs " << ct << " times and the ratio to non white space is " << setprecision(3) << (ct/characters) << endl;
cout << "The letter U occurs " << cu << " times and the ratio to non white space is " << setprecision(3) << (cu/characters) << endl;
cout << "The letter V occurs " << cv << " times and the ratio to non white space is " << setprecision(3) << (cv/characters) << endl;
cout << "The letter W occurs " << cw << " times and the ratio to non white space is " << setprecision(3) << (cw/characters) << endl;
cout << "The letter X occurs " << cx << " times and the ratio to non white space is " << setprecision(3) << (cx/characters) << endl;
cout << "The letter Y occurs " << cy << " times and the ratio to non white space is " << setprecision(3) << (cy/characters) << endl;
cout << "The letter Z occurs " << cz << " times and the ratio to non white space is " << setprecision(3) << (cz/characters) << endl;
cout << "The digits 0-9 occur " << di << " times and the ratio to non white space is " << setprecision(3) << (di/characters) << endl;



*****************problem area below line************************
// sort & median
int size;
float array[27]={a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, zz};
int middle = size/2;
float median;
if (size%2==0)
{median = static_cast<float>(array[middle-1]+array[middle])/2;}
else median = static_cast<float>(array[middle]);
cout << "The median of the array is:" << median << endl;
}


I am trying to take the set values from the previous code that work correctly, sort them, and then produce the median. However, I have been having problems understanding the concept of sorting given the various methods. I would greatly appreciate help with this concept.
Topic archived. No new replies allowed.