Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class StudentList class StudentList { private: int numStudents;

Similar presentations


Presentation on theme: "Class StudentList class StudentList { private: int numStudents;"— Presentation transcript:

1 Class StudentList class StudentList { private: int numStudents;
Student students[MAX_SIZE]; int find(const Student& s) const public: void Read() void Write() const float MaxGPA() const float MimGPA() const float AverageGPA() const void GetStats(float& max, float& min, float& avg) const Student MaxGPAStudent() const bool UpdateStudentGPA(const Student& s, float g) . . . }; // StudentList

2 Static List No Adding or Deleting! int main() { StudentList CS143_S2;
Student s; float maxGPA, minGPA, avgGPA; CS143_S2.Read(); CS143_S2.Write(); CS143_S2.GetStats(maxGPA, minGPA, avgGPA); s = CS143_S2.MaxGPAStudent(); s.Write(); s = Student(“Qi”, “Yang”); if (CS143_S2.UpdateStudentGPA(s, 0.5)) else cout << s.GetLast() << “ is not in the list”; return 0; } No Adding or Deleting!

3 Dynamic List int main() { StudentList CS143_S2; Student s;
char command; cin >> command; while (!cin.eof()) if (command == ‘A’ // add a student to CS143_S2 else if (command == ‘D’) // delete a student from CS143_S2 else if (command == ‘U’) // update a student in CS143_S2 else if (command == ‘P’) // print all students in CS143_S2 else // invalid command } return 0;

4 More Methods for Class StudentList
{ private: int numStudents; Student students[MAX_SIZE]; int find(const Student& s) const public: void Read() void Write() const float MaxGPA() const float MimGPA() const float AverageGPA() const void GetStats(float& max, float& min, float& avg) const Student MaxGPAStudent() const bool UpdateStudentGPA(const Student& s, float g) // Add // Delete };

5 Adding an Element to a List
Adding at the end of the list What’s the index? Assuming the list has 5 elements index: 5 Assuming the list has no elements index: 0 Assuming the list has numStudents elements index: numStudents

6 Pseudo Code for Adding Method
If the list is full Can not add Else Search the list If not found Insert Student What to do? (do not add)

7 Add Method for Class StudentList
const int FULL_LIST = 0; const int IN_LIST = 1; const int ADDED = 2; class StudentList { private: int numStudents; Student students[MAX_SIZE]; int find(const Student& s) const public: int Add(Student& s) if (numStudents == MAX_SIZE) return FULL_LIST; int index = find(s); if (index > -1) return IN_LIST; students[numStudents] = s; numStudents ++; return ADDED; } . . . };

8 Pseudo Code for Method Delete
Search in the list If not found return false Else delete the student from the list return true

9 Delete an Array Element
size: 9 Element to be deleted: at index 6 10 15 20 50 33 40 55 60 39 ? // Move Array[7] to Array[6] Array[6] = Array[7]; 10 15 20 50 33 40 60 39 // Move Array[8] to Array[7] Array[7] = Array[8]; 10 15 20 50 33 40 60 39 // Update size size --; // size 8; index: 0 to 7

10 Delete an Array Element
size: 9 Element to be deleted: at index 6 10 15 20 50 33 40 55 60 39 ? Array[6] = Array[7]; Array[7] = Array[8]; size --; // Move each element after the deleted one forward one step // Where to begin and where to stop? for (int i = index; i < size – 1; i ++) Array[i] = Array[i + 1];

11 Delete Method for Class StudentList
{ private: int numStudents; Student students[MAX_SIZE]; int find(const Student& s) const public: bool Delete(Student& s) int index = find(s); if (index == -1) return false; for (int i = index; i < numStudents – 1; i ++) students[i] = students[i + 1]; numStudents --; return true; } . . . };

12 Lab 9: Grace date: Wednesday
Schedule Lab 9: Grace date: Wednesday Program 5 Due Wednesday

13 Function main() for Prog5
int main() { Tournament tournament; tournament.readRatingAdjustmentLevelChart(); tournament.readPlayerList(); cout << "\nThe following is an echo of the original ” << << “players' list.\n"; tournament.printPlayerList(); cout << endl << "Processing transactions..." << endl; tournament.processAllTransactions(); cout << “\nThe following is the final players' list." << endl; return 0; }

14 Class Tournament class Tournament { private:
RatingAdjustmentLevel chart[MAX_LEVELS]; PlayerList allPlayers; . . . void processOneCommand() public: void readRatingAdjustmentLevelChart() void readPlayerList() void printPlayerList() const void processAllTransactions() cin >> command; while ( !cin.eof() ) processOneCommand(); } }; // Tournament

15 Method processOneCommand
void processOneCommand() { switch ( command ) case 'A': // inputs then calls add method break; case 'D': // inputs then calls delete method case 'P': // inputs then calls ? method default: // nothing to do }

16 Method processOneCommand
void processOneCommand() { if( command == ‘A’) // inputs then calls add method else if( command == ‘D’) // inputs then calls delete method else if( command == ‘P’) // inputs then calls ? Method else // invalid command }


Download ppt "Class StudentList class StudentList { private: int numStudents;"

Similar presentations


Ads by Google