Scis.regis.edu ● CS-362: Data Structures Week 4 Dr. Jesús Borrego Lead Faculty, COS Regis University 1.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Introduction to C Programming
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Structures in C.
Data Management and File Organization
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 11: Records (structs)
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
More Storage Structures A Data Type Defined by You Characteristics of a variable of a specific ‘data type’ has specific values or range of values that.
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.
EGR 2261 Unit 10 Records (structs)
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
Advance Use of Structures CHAPTER 5. C.10 1 Using Structures with Functions » A function can return only one value back. » Some of the processes may yield.
1 Today’s Objectives  Announcements Turn in Homework #1 Homework #2 is posted and it is due on 21-Jun  Review Quiz #1  Pointers and C-style strings.
Program structure Four different storage-class specifications: –Automatic (auto): local to a function, normally declared variables are automatic. Does.
Structures, Unions, and Enumerations Chapter Data Structure ( 資料結構 ) Content of data –Attributes of an object Person:name, age, sex, … A poker card:suit.
1 Principles of Programming I Note Set #12. 2 Semester Overview Functions Character File I/O Arrays Pointers and Dynamic Memory Allocation Characters.
Foundation Studies Course M.Montebello Records Foundation Studies Course.
Chapter 8 Arrays and Strings
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 11: Records ( struct s)
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
Cosc237/structures1 Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT,
CMSC 2021 CMSC 202 Computer Science II for Majors Fall 2002 Mr. Frey (0101 – 0104) Mr. Raouf (0201 – 0204)
Programmer Defined Structures (Records)
1 Structures UniMAP SEM I - 11/12EKT 120 Computer Programming.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Exam Format  90 Total Points  60 Points Writing Programs  25 Points Tracing Code/Algorithms and determining results  5 Points Short Answer  Similar.
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures Lecture
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
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.
Structured Data Chapter 11. Combining Data Into Structures Structure: C++ construct that allows multiple variables to be grouped together Format: struct.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
Scis.regis.edu ● CS-361: Control Structures Week 6 Dr. Jesús Borrego Lead Faculty, COS Regis University 1.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
CMSC 2021 CMSC 202 Computer Science II for Majors Spring 2003 Mr. Frey (0101 – 0104) Mr. Raouf (0201 – 0204)
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
1.1 Data Structure and Algorithm Lecture 1 Array Record Sequential Search Binary Search Bubble Sort Recursion Complexity Topics.
Data Types Storage Size Domain of all possible values Operations 1.
Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jesús Borrego 1.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
Data Storage So far variables have been able to store only one value at a time. What do you do if you have many similar values that all need to be stored?
Topic 4 Data Structures Program Development and Design Using C++, Third Edition.
Brendan Murphy CS 377 Discussion 1 Brendan Murphy
REVISION CSC 138 – Structured Programming One Dimensional Array
CS150 Introduction to Computer Science 1
CS 2308 Exam I Review.
Heterogeneous aggregate datatypes
CS148 Introduction to Programming II
EGR 2261 Unit 12 structs Read Malik, Chapter 9.
CS150 Introduction to Computer Science 1
Arrays.
CS150 Introduction to Computer Science 1
Presentation transcript:

scis.regis.edu ● CS-362: Data Structures Week 4 Dr. Jesús Borrego Lead Faculty, COS Regis University 1

Topics String functions Records Mid Term 2

String Functions Size of string: aString.size() Find substring in string: aString.find ( substr ) Create substring: newStr = aString.substr (fm, to) Program String Functions 3

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 nameType LnameText FnameText AgeInt ClassEnumerated (Fr, Sp, Jr, Sr) DOBdate

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 student john; struct student paul; struct student ringo; struct student george; 8

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”; Joe.name.last = “Vega”; Joe.name.mi = ‘F’; Joe.empid = “123ABC”; Joe.salary = 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 to 24