Respuesta :
Answer:
#include <iostream>
using namespace std;
struct Course
{
int courseNumber;
int courseStartDate;
int courseHours;
int lecturerId;
};
struct Lecturer
{
int lecturerId;
int officeHours;
Course courseTeaching[2];
};
struct Student
{
int studentId;
Course coursesTaken[2];
};
int main()
{
Course courseObj1, courseObj2, courseObj3;
courseObj1.courseNumber = 1000;
courseObj1.courseStartDate = 20200107;
courseObj1.courseHours = 2;
courseObj1.lecturerId = 100;
courseObj2.courseNumber = 1100;
courseObj2.courseStartDate = 20200113;
courseObj2.courseHours = 4;
courseObj2.lecturerId = 200;
courseObj3.courseNumber = 1200;
courseObj3.courseStartDate = 20200203;
courseObj3.courseHours = 4;
courseObj3.lecturerId = 100;
Lecturer lecturerObj1, lecturerObj2;
lecturerObj1.lecturerId = 100;
lecturerObj1.officeHours = 2;
lecturerObj1.courseTeaching[0] = courseObj1;
lecturerObj1.courseTeaching[1] = courseObj3;
lecturerObj2.lecturerId = 200;
lecturerObj2.officeHours = 2;
lecturerObj2.courseTeaching[0] = courseObj2;
Student student;
student.studentId = 2000;
student.coursesTaken[0] = courseObj1;
student.coursesTaken[1] = courseObj3;
cout << "1. Lecturer ID: " << lecturerObj1.lecturerId << "\tOffice Hours: " << lecturerObj1.officeHours << endl;
for(int i = 0; i < 2; i++)
cout << "COURSE " << (i + 1) << ":\n---------\n" << "Course Number: " << lecturerObj1.courseTeaching[i].courseNumber << endl << "Course start date: " << lecturerObj1.courseTeaching[i].courseStartDate << endl << "Course hours: " << lecturerObj1.courseTeaching[i].courseHours << endl << "Lecturer ID: " << lecturerObj1.courseTeaching[i].lecturerId << endl;
cout << "\n2. Lecturer ID: " << lecturerObj2.lecturerId << "\tOffice Hours: " << lecturerObj2.officeHours << endl;
for(int i = 0; i < 1; i++)
cout << "COURSE " << (i + 1) << ":\n---------\n" << "Course Number: " << lecturerObj2.courseTeaching[i].courseNumber << endl << "Course start date: " << lecturerObj2.courseTeaching[i].courseStartDate << endl << "Course hours: " << lecturerObj2.courseTeaching[i].courseHours << endl << "Lecturer ID: " << lecturerObj2.courseTeaching[i].lecturerId << endl;
int courseNumber;
cout << "\n Enter the course number: ";
cin >> courseNumber;
int index = -1;
int len = sizeof(student.coursesTaken) / sizeof(student.coursesTaken[0]);
int totalHours = 0;
for(int i = 0; i < len; i++)
{
if(student.coursesTaken[i].courseNumber == courseNumber)
{
totalHours += student.coursesTaken[i].courseHours;
}
}
if(totalHours == 0)
cout << "\n Student is not registered to this course!\n";
else
cout << "\nStudent " << student.studentId << " needs " << totalHours << " hours per week of this course.\n";
return 0;
}
Explanation:
- Create the 3 objects of Course class and set their properties respectively.
- Create the 2 objects of Lecturer class and set their properties respectively.
- Create an object of Student class and set their properties respectively.
- Run a for loop and check whether the current courseNumber is already added and then increment the total number of hours for that course.
- Finally check if totalHours is equal to 0 or not and then display the suitable message accordingly.