Answer:
To write a C++ program that adds 5 marks to all female students, we first need to create a structure to store student information, including name, gender, and marks. Next, we'll create a function to add 5 marks to all female students, and finally, display the updated marks.
Here's a C++ program that demonstrates this:
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;
struct Student {
string name;
char gender;
int marks;
};
void addMarksToFemaleStudents(vector<Student>& students) {
for (Student& student : students) {
if (student.gender == 'F' || student.gender == 'f') {
student.marks += 5;
}
}
}
void displayStudents(const vector<Student>& students) {
cout << left << setw(10) << "Name" << setw(8) << "Gender" << "Marks" << endl;
for (const Student& student : students) {
cout << left << setw(10) << student.name << setw(8) << student.gender << student.marks << endl;
}
}
int main() {
vector<Student> students = {
{"Alice", 'F', 85},
{"Bob", 'M', 78},
{"Eva", 'F', 90},
{"Tom", 'M', 88}
};
cout << "Original list of students:" << endl;
displayStudents(students);
addMarksToFemaleStudents(students);
cout << "\\Updated list of students:" << endl;
displayStudents(students);
return 0;
}
Step-by-step explanation:
The code defines a struct named Student which contains the name, gender, and marks of a student. It then defines two functions:
- addMarksToFemaleStudents(vector<Student>& students): This function takes a reference to a vector of Student objects and increases the marks of female students (indicated by a gender of 'F' or 'f') by 5.
- displayStudents(const vector<Student>& students): This function takes a constant reference to a vector of Student objects and displays their name, gender, and marks in a formatted table using the setw() and left manipulators.
In the main() function, a vector of Student objects is created and initialized with four elements. The original list of students is displayed using displayStudents(), then addMarksToFemaleStudents() is called to increase the marks of female students, and the updated list of students is displayed again using displayStudents().
The program then returns 0, indicating successful execution.