Nested Structures struct TDate { int year, month, day; }; struct StudentType { string id, firstName, lastName; float gpa; TDate DOB; }; struct SectionType.

Slides:



Advertisements
Similar presentations
Array. Convert Numbers in Different Base Systems Generate values to a series of numbers in different base systems: Base is between 2 and 9; Maximum number.
Advertisements

Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT}; Day today; today = WED; if (today == FRI) cout
1 CS 105 Lecture 11 Arrays Version of Wed, Apr 6, 2011, 6:20 pm.
1 CS 105 Lecture 8 Strings; Input Failure Mon, Mar 7, 2011, 3:39 pm.
Chapter 11: Structured Data. Slide Introduction An array makes it possible to access a list or table of data of the same data type by using a single.
1 Techniques of Programming CSCI 131 Lecture 29 Search.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
1 CS 1430: Programming in C++. 2 IF Statement if (cond) statement //Next statement if (cond) { statement1 statement2 … } //Next statement.
Sudoku Jordi Cortadella Department of Computer Science.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
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.
Chapter 7 A Data Types – Structures Structures Structure: C++ construct that allows multiple variables to be grouped together Structure Declaration.
Structure TDate struct TDate { int year, month, day; }; // Define a new data type.
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.
Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string.
Quiz // // The function exchanges the two parameters. // Param: ( ) // Param:
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
Class Student class Student { private: string id; string firstName, lastName; float gpa; public: void Read() void Write() string getGPA() void setGPA(
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.
CS 1430: Programming in C++.
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)
April 11, 2005 More about Functions. 1.Is the following a function call or a function header? calcTotal(); 2.Is the following a function call or a function.
Functions & Strings CIS Feb-06. Quiz 1.Write a function Prototype for the function findSmallest that takes three integers and returns an integer.
Class Method Read class Student { private: string id; string firstName, lastName; float gpa; public: // Will the method change any data members? // Yes!
1 11/12/04CS150 Introduction to Computer Science 1 More Arrays.
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 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.
1 C++ Classes and Data Structures Course link…..
CS 1430: Programming in C++ No time to cover HiC.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 1430: Programming in C++.
Multi-dimensional Array
solve the following problem...
Structures - Part II aggregate operations arrays of type struct
Data Types – Structures
CS 1430: Programming in C++.
CS 1430: Programming in C++.
Student Data Score First Name Last Name ID GPA DOB Phone ...
Data Types – Structures
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};
CS 1430: Programming in C++ No time to cover HiC.
CS 1430: Programming in C++ No time to cover HiC.
CS 1430: Programming in C++.
Standard Input/Output Stream
CS150 Introduction to Computer Science 1
CS 1430: Programming in C++.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS150 Introduction to Computer Science 1
Function Overloading.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Fundamental Programming
CS150 Introduction to Computer Science 1
(Dreaded) Quiz 2 Next Monday.
Class StudentList class StudentList { private: int numStudents;
More on Structs Sometimes we use structs even when the fields are all of the same type. If the fields are different conceptually, that is, the data stands.
Structure (i.e. struct) An structure creates a user defined data type
Structures Structured Data types Data abstraction structs ---
CS 1430: Programming in C++.
Selection Sorting S[] : array of int or float
Presentation transcript:

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

// 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 Function Prototypes

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