220 likes | 796 Views
Nested Structures. struct TDate { int year, month, day; }; struct StudentType { string id, firstName, lastName; float gpa; TDate DOB; }; struct SectionType { int numStudents; StudentType students[30]; };. // The function inputs data to a TDate struct
E N D
Nested Structures struct TDate { int year, month, day; }; struct StudentType { string id, firstName, lastName; float gpa; TDate DOB; }; struct SectionType { int numStudents; StudentType students[30]; };
// The function inputs data to a TDate struct // The input is in the format: mm/dd/yyyy. // Parameter: ( out ) void ReadDate(TDate& someDate) { char slash; cin >> someDate.month >> slash >> someDate.day >> slash >> someDate.year; }
// The function inputs data to a TDate struct and then // returns the struct. // The input is in the format: mm/dd/yyyy. // Parameter: NONE TDate ReadDate() { aDate TDate; char slash; cin >> someDate.month >> slash >> someDate.day >> slash >> someDate.year; return aDate; }
// The function displays a TDate struct // in the format mm/dd/yyyy. // Parameter: (In) void WriteDate(const TDate& date) { cout << date.month << ‘/’ << date.day << ‘/’ << date.year; return; }
// The function inputs data to a StudentType struct // in the order: id, firstName, lastName, gpa, // DOB (month, day, year) and returns the struct. // Parameter: () StudentType GetStudent() { StudentType s; cin >> s.id >> s.firstName >> s.lastName; cin >> s.gpa; ReadDate(s.DOB); // Using function void ReadDate(TDate). //cin >> s.DOB.month >> s.DOB.day // >> s.DOB.year; return s; }
// The function inputs data to a StudentType // struct in the order: id, firstName, // lastName, gpa, DOB (month, day, year) // Parameter: (out) void GetStudent(StudentType& s) { cin >> s.id >> s.firstName >> s.lastName; cin >> s.gpa; s.DOB = ReadDate(); // Using function TDate ReadDate(). return; }
// The function inputs data to a SectionType sec. // It calls function // StudentType GetStudent() // void GetStudent(StudentType& s) // Parameter: (OUT) void InputSection(SectionType sec) { cin >> sec.numStudents; for (int i = 0; i < sec.numStudents; i ++) sec.students[i] = GetStudent(); //GetStudent(sec.students[i]); return; }
// The function compares two dates: date1 and date2. // It returns 0 if date1 is the same as date2, // 1 if date1 is after date2, // -1 otherwise. // Parameter: (In, In) int CompDate(const TDate& date1, const TDate& date2) { // When return 0? if (date1.year == date2.year && date1.month == date2.month && date1.day == date2.day) return 0; // When return 1? else if (date1.year > date2.year) return 1; else if (date1.year == date2.year && date1.month > date2.month) return 1; else if (date1.year == date2.year && date1.month == date2.month && date1.day > date2.day) return 1; // When return -1? else return -1; } // Correct?
// The function compares two dates: date1 and date2. // It returns 0 if date1 is the same as date2, // 1 if date1 is after date2, // -1 otherwise. // Parameter: (In, In) int CompDate(const TDate& date1, const TDate& date2) { // When return 0? if (date1.year == date2.year && date1.month == date2.month && date1.day == date2.day) return 0; // When return 1? else if ((date1.year > date2.year) || (date1.year == date2.year && date1.month > date2.month) || (date1.year == date2.year && date1.month == date2.month && date1.day > date2.day)) return 1; // When return -1? else return -1; }
// The function compares two dates: date1 and date2. // It returns true if date1 is after date2, // false otherwise. // Parameter: (In, In) bool After(const TDate& date1, const TDate& date2) { if ((date1.year > date2.year) || (date1.year == date2.year && date1.month > date2.month) || (date1.year == date2.year && date1.month == date2.month && date1.day > date2.day)) return true; else return false; }
// The function compares two dates: date1 and date2. // It returns true if date1 is after date2, // false otherwise. // Parameter: (In, In) // Use function CompDate. bool After(const TDate& date1, const TDate& date2) { return (CompDate(date1, date2) == 1); }
SectionType cs143; InputSection(cs143); // int CompDate(const TDate& date1, const TDate& date2) if (CompDate(cs143.students[0].DOB, cs143.students[1].DOB) == 1 ) cout << “The first student of cs143 is younger than ” << “the second student of cs143.”; // Correct? How to find the index of the youngest student? How to find the index of the student with the highest gpa?
// The function has one parameter sec of Section and // returns the index of the first student of sec // who has the highest GPA of sec. // Parameter: (In) int IndexOfMaxGPA(const SectionType& sec) { int index = 0; for (int i = 1; i < sec.numStudents; i ++) if (sec.students[i].gpa > sec.students[index].gpa) index = i; return index; }
// The function has one parameter sec of SectionType and // returns the index of the first youngest student of sec. // Parameter: (In) int IndexOfYoungest(const SectionType& sec) { int index = 0; for (int i = 1; i < sec.numStudents; i ++) if (CompDate(sec.students[i].DOB, sec.students[index].DOB)== 1) index = i; // else? return index; }
// The function has one parameter sec of SectionType and // returns the index of the first youngest student of sec. // Parameter: (In) // Use function // bool After(const TDate& date1, const TDate& date2); int IndexOfYoungest(const Section& sec) { int index = 0; for (int i = 1; i < sec.numStudents; i ++) if (After(sec.students[i].DOB, sec.students[index].DOB)) index = i; // no else! return index; }
Function Prototypes // Functions on TDate void ReadDate(TDate& someDate); TDate ReadDate(); void WriteDate(const TDate& date); int CompDate(const TDate& date1, const TDate& date2); // Functions on StudentType void GetStudent(StudentType& s); StudentType GetStudent(); void DisplayStudent(const StudentType& s); // Functions on SectionType void inputSection(SectionType& sec); SectionType inputSection(); int IndexOfMaxGPA(const SectionType& sec); int IndexOfYoungest(const SectionType& sec); // More functions
int main() { SectionType CS143; int index; // Input data to CS143 inputSection(CS143); index = IndexOfMaxGPA(CS143); cout << "The student with highest GPA: " << endl; DisplayStudent(CS143.students[index]); index = Youngest(CS143); cout << "The youngest student: " << endl; DisplayStudent(CS143.students[index]); // Display all students return 0; }
Quiz 83 1 Point Due Time: 9:30 pm, Wednesday