Class StudentList class StudentList { private: int numStudents;

Slides:



Advertisements
Similar presentations
1. List Static List: no adding or deleting Dynamic List: can add or delete items from the list Both static and dynamic lists: linear search, update item.
Advertisements

One Dimensional Arrays
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
Monday, 11/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Monday, 11/11/02  Questions? HW 04 due today at 5.  Today – Lists and an introduction to searching.
Queues CS 3358 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: – Elements are added.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
Queue Overview Queue ADT Basic operations of queue
Overview Reference parameters Documenting functions A game of craps. Design, code, test and document.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
CS Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
Review Binary Numbers Bit : 0 or 1 Byte: 8 bites 256 different values 2 8 KB : 1024 bytes 2 10 bytes MB : 1024 * 1024 bytes 2 10 * 2 10 (2 20 ) bytes GB.
Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.
Selection Sorting S[] : array of int or float Size: number of elements of s[] Pseudocode for i = 0 to size - 2 find the index of a smallest element between.
1 Data Structures CSCI 132, Spring 2014 Lecture 6 Applications using Stacks.
Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string.
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
1 CS 1430: Programming in C++. 2 Find Max, Min, Average of m Sections Max, Min and Average of each section Max, Min and Average of all sections together.
Data Types Storage Size Domain of all possible values Operations 1.
CS 1430: Programming in C++ 1. Class StudentList class StudentList { private: int numStudents; Student students[MAX_SIZE]; int find(const Student& s)
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
CS 1430: Programming in C++ Function Design 1. Good Functions Focusing on one thing Function name tells what it does sqrt(val) pow(base, exp) cin.eof()
Class Method Read class Student { private: string id; string firstName, lastName; float gpa; public: // Will the method change any data members? // Yes!
CS 1430: Programming in C++ 1. Test 2 Friday Functions Arrays For Loops Understand Concepts and Rules Memorize Concepts and Rules Apply Concepts and Rules.
CS 1430: Programming in C++ 1. File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right?
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Switch-Case Statement. What is a switch-case?  The switch-case statement is really nothing more than a glorified.
Nested Structures struct TDate { int year, month, day; }; struct StudentType { string id, firstName, lastName; float gpa; TDate DOB; }; struct SectionType.
Selection Sorting Pseudocode (Forward) for i = 0 to size - 2 find the index of the required element between s[i] and s[size - 1] If i not the same as index.
Searching Arrays Linear search Binary search small arrays
C++ Lesson 1.
CS505 Data Structures and Algorithms
CS 1430: Programming in C++ No time to cover HiC.
Template Classes and Functions
COMPUTER 2430 Object Oriented Programming and Data Structures I
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. cout >
CS 1430: Programming in C++.
A solution to a list of records
לולאות קרן כליף.
Reserved Words.
CS 1430: Programming in C++.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 1430: Programming in C++.
Student Data Score First Name Last Name ID GPA DOB Phone ...
CS 1430: Programming in C++.
CS 1430: Programming in C++.
CS 1430: Programming in C++.
Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT};
null, true, and false are also reserved.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 1430: Programming in C++ No time to cover HiC.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
CS 1430: Programming in C++ No time to cover HiC.
CMSC 202 Lesson 22 Templates I.
CS 1430: Programming in C++.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 1430: Programming in C++.
Templates I CMSC 202.
Fundamental Programming
Queues.
ICOM 4015 Advanced Programming
CS 1430: Programming in C++.
Sorting Algorithms.
Selection Sorting S[] : array of int or float
Presentation transcript:

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

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!

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;

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

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

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)

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

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

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

Delete an Array Element size: 9 Element to be deleted: at index 6 10 15 20 50 33 40 55 60 39 ? . . . . 0 1 2 3 4 5 6 7 8 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];

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

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

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

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

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 }

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 }