Any help would be appreciated! Find the Median You are given a file with a list
ID: 3693697 • Letter: A
Question
Any help would be appreciated!
Find the Median
You are given a file with a list of grades. You are asked to find the Median grade for the class.
You will be given two files, GradeListO.txt and GradeListE.txt. Write a program that will determine the median grade in GradeList for either of these two cases (one has an Odd number of grades and the other has an Even number of grades). Your program should allow the user to choose which of the two files should be selected for this computation. You may use either a Bubble Sort or a Selection Sort in implementing this program.
Explanation / Answer
/**
The C ++ program that prompts user to enter type of file name to find
the median of a value. If user selects even number of list file then
find the median from GradeListE and print to console.
If user selects odd number of list file then
find the median from GradeListO and print to console.
*/
//header files
#include<iostream>
#include<fstream>
using namespace std;
const int size=50;
void sort(double grades[]);
void print(double grades[]);
int main()
{
//object of ifstream
ifstream fin;
int choice;
double median;
double grades[size];
char file1[40]="GradeListO.txt";
char file2[40]="GradeListE.txt";
cout<<"1.GradeListO.txt "<<endl;
cout<<"2.GradeListE.txt "<<endl;
cout<<"Which file to open to find median value : 1 or 2 ?"<<endl;
//read choice
cin>>choice;
if(choice==1)
{
fin.open(file1);
}
else
fin.open(file2);
if(!fin)
{
cout<<"File not found"<<endl;
system("pause");
exit(0);
}
else
{
int index=0;
//read values fro file
while(index<size)
{
fin>>grades[index];
index++;
}
}
//call sort method
sort(grades);
cout<<"Sorted list : "<<endl;
print(grades);
//check if choice is 1 for odd
if(choice==1)
//calculate median
median=grades[size/2];
else
//calculate median for even list
median=(grades[(size-1)/2]+grades[size/2])/2.0;
//print median
cout<<"Median value : "<<median<<endl;
system("pause");
return 0;
}
//Helper method that prints the elements in array,arr
void print(double grades[])
{
for(int index=0;index<size;index++)
cout<<grades[index]<<" ";
cout<<endl;
}
/**Bubble sort method that takes grades as argument and sorts
the values in ascending order */
void sort(double grades[])
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size - 1; j++)
{
if (grades[j] > grades[j + 1])
{
int temp = grades[j];
grades[j] = grades[j + 1];
grades[j + 1] = temp;
}
}
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------
Input files:
GradeListE.txt
77
49
55
13
87
32
47
41
20
30
29
79
45
78
55
12
24
25
23
56
66
77
77
65
57
89
47
12
11
70
62
64
65
50
69
91
69
20
11
27
24
94
96
31
44
76
42
95
90
13
GradeListO.txt
97
29
85
29
47
65
62
80
87
23
17
38
41
27
52
38
21
36
76
88
90
83
29
88
56
61
79
29
14
54
65
85
89
12
35
92
74
27
46
87
64
60
70
74
70
38
74
95
32
96
----------------------------------------------------------------------------------------------------------------
Sample output:
1.GradeListO.txt
2.GradeListE.txt
Which file to open to find median value : 1 or 2 ?
1
Sorted list :
12 14 17 21 23 27 27 29 29 29 29 32 35 36 38 38 38 41 46 47 52 54 56 60 61 62 64
65 65 70 70 74 74 74 76 79 80 83 85 85 87 87 88 88 89 90 92 95 96 97
Median value : 62
sample output2:
1.GradeListO.txt
2.GradeListE.txt
Which file to open to find median value : 1 or 2 ?
2
Sorted list :
11 11 12 12 13 13 20 20 23 24 24 25 27 29 30 31 32 41 42 44 45 47 47 49 50 55 55
56 57 62 64 65 65 66 69 69 70 76 77 77 77 78 79 87 89 90 91 94 95 96
Median value : 52.5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.