PPT - Regis University: Academic Web Server for Faculty

Download Report

Transcript PPT - Regis University: Academic Web Server for Faculty

CS-362: Data Structures
Week 4
Dr. Jesús Borrego
Lead Faculty, COS
Regis University
1
scis.regis.edu ● [email protected]
Topics
• String functions
• Records
• Mid Term
2
String Functions
•
•
•
•
3
Size of string: aString.size()
Find substring in string: aString.find ( substr )
Create substring: newStr = aString.substr (fm, to)
Program String Functions
Records
• The course is about data structures – the
structure of data
• We saw files and arrays
• Now, we cover a new type of data structure:
records
4
Records
• If we have a student database with a Students
table, each student will be a record
• Records in C++ are called structures (struct)
• Records allow elements (fields) of different types
to be defined. For a Student record:
5
Field name
Type
Lname
Text
Fname
Text
Age
Int
Class
Enumerated (Fr, Sp, Jr, Sr)
DOB
date
Struct
struct nameOfStructure
{
datatype1 variable1;
datatype2 variable2;
…
…
…
datatypen variablen;
}
6
Defining a Student record
struct Students
{
string lname;
string fname;
char
mi;
double gpa;
};
7
Program Beatles
struct student
{
float mid_term;
float final;
char grade;
};
struct
struct
struct
struct
8
student
student
student
student
john;
paul;
ringo;
george;
Program Beatles – (Cont’d)
struct student
{
float mid_term;
float final;
char grade;
} john, paul, ringo, george;
9
Accessing record fields
• To access a field in a record, use the record
variable name followed by a dot (“.”) and then
the field name:
john.grade = ‘A’;
Note that we use the variable, not the record name
10
Record definition
• Struct defines a data type but does not declare a
variable
• No memory is allocated when we describe struct
• When we define variables of struct, then
memory is allocated
• We can input values directly into the field name
cin>> john.grade;
11
Record assignment
• We can assign values to a field in a record
john.grade = ‘A’;
• If we try to read a record from the keyboard, we
will find that cin is not defined for the record
cin >> john;
• Need to enter each field individually
▫ Or create a new cin that reads all elements of a
record
12
Comparing fields in a record
• We can compare fields in one record to those in
another:
if ( john.grade == paul.grade )
See Beatles program
13
Initializing a record
struct student
{
float mid_term;
float final;
char grade;
};
struct student john = { 75.4, 80.6, ‘B’ };
14
Arrays and Records
• Both arrays and records define multiple
elements using a single name
• Arrays have elements of the same type
• Records have fields that may be of different
types
• Arrays are passed by reference to functions
• Records can be passed by reference OR by value
• Arrays cannot be returned from a function
• Records can be returned from a function
15
Arrays and Records (Cont’d)
• We can have arrays of records
struct student
{
float mid_term;
float final;
char grade;
};
student Students [ MAX ];
Students [ i ].grade = ‘A’
16
Arrays and Records (Cont’d)
• We can have records of arrays
struct student
{
float mid_term;
float final;
char grade;
string courses [ NUM ];
};
student aStudent;
aStudent.courses [ i ] = “CS 362”;
17
Records in Records
struct nameType
{
string first;
string last;
char
mi;
};
struct Employee
{
nameType name;
string
empID;
double
salary;
}
18
Records in Records (Cont’d)
struct nameType
{
string first;
string last;
char
mi;
};
struct Employee
{
nameType name;
string
empID;
double
salary;
}
19
Employee Joe;
Joe.name.first
Joe.name.last
Joe.name.mi
Joe.empid
Joe.salary
=
=
=
=
=
”Joe”;
“Vega”;
‘F’;
“123ABC”;
60000;
Program Structs
•
•
•
•
Defines a month record
Contains number of days
Contains array of characters for name of month
Creates an array of records
• See structs.cpp
• See structs1.cpp
20
Records as parameters
• As with other types, we can pass records to
functions
• See RecStruct
21
Example of passing records to
functions
• See Tomorrow.cpp
• See Time.cpp
22
Mid term
• Will cover file processing, enumerated types,
single dimension arrays, sorts (bubble, insertion,
selection), sequential search, binary search.
• About 7 questions and 1 program.
• Due before week 6
• Submit to Week 4 assignments
23
Questions
• Email to
[email protected]
24