-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment2.cpp
More file actions
186 lines (165 loc) · 5.04 KB
/
Assignment2.cpp
File metadata and controls
186 lines (165 loc) · 5.04 KB
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
/*
PHYS30762 Programming in C++
Assignment 2
Author: Leo Feasby
Date: 23/02/2024
Description:
This program processes course data by reading from a file, sorting the data based on user preference,
and filtering and printing course information according to user input. Additionally, it calculates
statistical measures such as the mean and standard deviation of course marks.
*/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
// Class to represent course information
class course
{
public:
double mark;
int course_code;
std::string name;
std::string full_title;
// Constructor that initializes course details and constructs the full title
course(double mark, int course_code, const std::string& name)
: mark(mark), course_code(course_code), name(name)
{
std::stringstream ss;
ss << "PHYS " << course_code << " " << name; // Constructing full title
full_title = ss.str();
}
// Function to get the year of the course from its code
int get_year() const
{
return course_code / 10000;
}
};
// Declaration of functions used in the program
void print_courses_and_statistics(const std::vector<course>& courses);
void sort_courses(std::vector<course>& courses, const std::string& sort_by);
double calculate_mean(const std::vector<double>& values);
double calculate_standard_deviation(const std::vector<double>& values, double mean);
void filter_and_print_courses(std::vector<course>& courses, char filter_option);
int main()
{
std::string data_file;
std::vector<course> courses;
// Prompt user for the filename containing course data
std::cout << "Enter data filename: ";
std::cin >> data_file;
// Open the file and check for errors
std::ifstream course_stream(data_file);
if (!course_stream.is_open())
{
std::cerr << "Error opening file.\n";
return 1;
}
// Read course data from file
double mark;
int course_code;
std::string name;
while (course_stream >> mark >> course_code)
{
std::getline(course_stream, name);
if (!name.empty())
{
name = name.substr(1); // Remove leading space
}
courses.emplace_back(mark, course_code, name);
}
course_stream.close();
std::cout << "Total Number of Courses Loaded: " << courses.size() << "\n";
// Sorting courses based on user preference
std::string sort_option;
std::cout << "Sort courses by 'title', 'code', or 'mark'? ";
std::cin >> sort_option;
sort_courses(courses, sort_option);
// Filtering and printing courses based on user input
char filter_option;
std::cout << "Print all courses (A) or filter by year (1-4)? Enter A or 1-4: ";
std::cin >> filter_option;
filter_and_print_courses(courses, filter_option);
return 0;
}
// Function to filter and print courses based on the specified filter option
void filter_and_print_courses(std::vector<course>& courses, char filter_option)
{
int year_filter = filter_option - '0';
std::vector<course> filtered_courses;
// Filtering courses
for (const auto& course : courses)
{
if (filter_option == 'A' || course.get_year() == year_filter)
{
filtered_courses.push_back(course);
}
}
// Printing filtered courses
std::cout << "Filtered Courses:\n";
for (const auto& course : filtered_courses)
{
std::cout << course.full_title << " - Mark: " << course.mark << "\n";
}
// Calculating and printing statistics if applicable
if (!filtered_courses.empty())
{
std::vector<double> marks;
for (const auto& course : filtered_courses)
{
marks.push_back(course.mark);
}
double mean = calculate_mean(marks);
double std_dev = calculate_standard_deviation(marks, mean);
std::cout << "Mean for selected courses: " << mean << "\nStandard Deviation: " << std_dev << "\n";
}
std::cout << "Number of courses selected: " << filtered_courses.size() << "\n";
}
// Function to sort courses based on the specified criteria
void sort_courses(std::vector<course>& courses, const std::string& sort_by)
{
if (sort_by == "title")
{
std::sort(courses.begin(), courses.end(), [](const course& a, const course& b)
{
return a.name < b.name;
});
}
else if (sort_by == "code")
{
std::sort(courses.begin(), courses.end(), [](const course& a, const course& b)
{
return a.course_code < b.course_code;
});
}
else if (sort_by == "mark")
{
std::sort(courses.begin(), courses.end(), [](const course& a, const course& b)
{
return a.mark < b.mark;
});
}
}
// Function to calculate the mean of a set of values
double calculate_mean(const std::vector<double>& values)
{
double sum = 0;
for (double value : values)
{
sum += value;
}
return sum / values.size();
}
// Function to calculate the standard deviation of a set of values
double calculate_standard_deviation(const std::vector<double>& values, double mean)
{
double sum = 0;
for (double value : values)
{
sum += std::pow(value - mean, 2);
}
return std::sqrt(sum / (values.size() - 1));
}