1 / 18

Nested Structures

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

akando
Download Presentation

Nested Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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]; };

  2. // 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; }

  3. // 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; }

  4. // 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; }

  5. // 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; }

  6. // 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; }

  7. // 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; }

  8. // 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?

  9. // 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; }

  10. // 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; }

  11. // 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); }

  12. 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?

  13. // 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; }

  14. // 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; }

  15. // 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; }

  16. 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

  17. 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; }

  18. Quiz 83 1 Point Due Time: 9:30 pm, Wednesday

More Related