BITG 1233: STRUCTURED DATA. LECTURE 11 (Sem 2, 17/18)

Similar documents
Chapter 11: Structured Data

Chapter 11: Structured Data

Chapter 11: Abstract Data Types. Abstraction and Data Types. Combining Data into Structures 8/23/2014. Abstract Data Types

LECTURE 11 STRUCTURED DATA

LAB 4.1 Relational Operators and the if Statement

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

C++ PROGRAMMING SKILLS Part 4: Arrays

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)

BITG 1113: Array (Part 1) LECTURE 8

Complex data types Structures Defined types Structures and functions Structures and pointers (Very) brief introduction to the STL

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Lecture 3 Tao Wang 1

CS 141, Introduction to Computer Science Fall Midterm Exam

Introduction to Programming EC-105. Lecture 2

BITG 1113: Function (Part 2) LECTURE 5

A structure is an aggregate data type which contains a fixed number of heterogeneous components.

Chapter 7 Array. Array. C++, How to Program

BITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17)

BITG 1113: POINTER LECTURE 12

Syntax to define a Structure: struct structurename { datatype membername1; datatype membername2;... } ; For Example:

Fundamentals of Programming CS-110. Lecture 2

What we will learn about this week: Declaring and referencing arrays. arrays as function arguments. Arrays

CSci 1113 Midterm 1. Name: Student ID:

CHAPTER 3 ARRAYS. Dr. Shady Yehia Elmashad

Structures. Lecture 15 COP 3014 Spring April 16, 2018

Downloaded S. from Kiran, PGT (CS) KV, Malleswaram STRUCTURES. Downloaded from

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

BITG 1113: Array (Part 2) LECTURE 9

Topics. Topics (Continued) 7.1 Abstract Data Types. Abstraction and Data Types. 7.2 Object-Oriented Programming

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 6: One Dimensional Array

Logical Operators and if/else statement. If Statement. If/Else (4.3)

Lecture #1. Introduction to Classes and Objects

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Classes and Data Abstraction. Topic 5

The syntax of structure declaration is. struct structure_name { type element 1; type element 2; type element n;

CSCE Practice Midterm. Data Types

Chapter-14 STRUCTURES

The C++ Language. Arizona State University 1

Chapter 4 - Notes Control Structures I (Selection)

Arrays. Arizona State University 1

CSI33 Data Structures

Computer Programming : C++

Input And Output of C++

CSCE Practice Midterm. Data Types

C++ Programming: From Problem Analysis to Program Design, Third Edition


Largest Online Community of VU Students

Functions, Arrays & Structs

Structured Data. CIS 15 : Spring 2007

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program)

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) A few types

Arrays 2 CS 16: Solving Problems with Computers I Lecture #12

Introduction to Programming I COS1511 School of Computing Revision Notes

CS Spring 05 - MidTerm

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Example of Objective Test Questions for Test 4

Add Subtract Multiply Divide

Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10

Elements of C in C++ data types if else statement for loops. random numbers rolling a die

CS 115 Midterm 2 Solutions

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body

CSC 1300 Exam 4 Comprehensive-ish and Structs

BITG 1233: Introduction to C++

CS150 Introduction to Computer Science 1. Logical Operators and if/else statement

Microcontroller Systems. ELET 3232 Topic 8: Structures, Arrays, & Pointers

Arrays. Week 4. Assylbek Jumagaliyev

Functions, Arrays & Structs

Review of Important Topics in CS1600. Functions Arrays C-strings

The American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010

a data type is Types

CS242 COMPUTER PROGRAMMING

Lecture 4. 1 Statements: 2 Getting Started with C++: LESSON FOUR

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Structures. Lecture 5. Structures

6.096 Introduction to C++

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14)

Test Bank for Problem Solving with C++: The Object of Programming, 8/e Chapter 2 C++ Basics

C++ Final Exam 2017/2018

CS 105 Lecture 5 Logical Operators; Switch Statement. Wed, Feb 16, 2011, 5:11 pm

by: Lizawati, Norhidayah & Muhammad Noorazlan Shah Computer Engineering, FKEKK, UTeM

CSCE 2004 Midterm Exam Spring 2017

Introduction to Programming

.:: UNIT 1 ::. INTRODUCTION TO ALGORITHM & DATA STRUCTURES

F4104 ALGORITHM & DATA STRUCTURE

Computer Programming

Tutorial Letter 103/1/2017 Introduction to Programming I

Lab 12 Object Oriented Programming Dr. John Abraham

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

Score score < score < score < 65 Score < 50

Chapter 7 - Notes User-Defined Functions II

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

UNIT-2 Introduction to C++

Boolean Algebra Boolean Algebra

Computer Programming

C++ For Science and Engineering Lecture 12

Exercises 1. class member { int membernum = 25; float memberpay; public void Input(cin >> membernum >> memberpay); void Output; }

Write a program that displays all the even integers between 1 and 100, inclusive

CS201 Latest Solved MCQs

Transcription:

BITG 1233: STRUCTURED DATA LECTURE 11 (Sem 2, 17/18) 1

LEARNING OUTCOMES At the end of this lecture, you should be able to: 1. Describe the concept of structure. 2. Describe the difference between array and structure. 3. Describe the concept of structure containing array and array of structure. 4. Use structures in program. 5. Use structures as Function Parameters. 2 2

Combining Data into Structures Structure is a collection of related elements, possibly of different types, having a single name. C++ construct that allows multiple variables to be grouped together. Each variable is an element in a structure. Element of a structure is called a field or member. The difference between an array and a structure is that all elements in an array must be of the same type, while the elements in a structure may be of the same or different types. 3

Structure The first example (fraction), has two fields, both of which are integers. The second example (student), has three fields, an integer number, a character array and a floating point number. 5 7 1234 F A T I M A H /0 3.7 4

To declare a structure type, use the general format as follows: struct <structurename> type1 field1; type2 field2;... }; Structure Declaration Notes: Must have ; after closing } Multiple fields of same type can be in comma-separated list: Example : char name[25], address[30]; To define a variable of a structure type, use the structure name as the variable s type. Structure Declaration 5

Example : Structure declaration: struct int ; char ; short ; double ; }; It s variable declaration: STUDENT bill; Or; structure name structure fields / members bill studentid name yearinschool gpa struct STUDENT bill; 6

Structure Initialization A structure can be initialized. The rules of structure initialization are similar to the rules of array initialization: (1) the initializers are enclosed in braces and separated by commas; (2) the initializers must match their corresponding types in the structure declaration; (3) if a nested structure is used, the nested initializers must be enclosed in their own set of braces. 7

Example : Structure Initialization struct SAMPLE int x; int y; float t; char u; }; sam1 SAMPLE sam1 = 2, 5, 3.2, A }; SAMPLE sam2 = 7, 3 }; 8

Referencing Individual Field/Member We can read data into and write data from structure members just as we can from individual variables. Use the dot (.) operator to refer to members of struct variables. For example the value for the field of the sample structure can be read from the keyboard and placed in sam2 using the input statement below. Suppose, the data entered are 7, 3, 0, R. cin >> sam2.x >> sam2.y >> sam2.t >> sam2.u; R 9

Structure Operation The structure is an entity that can be treated as a whole. However, only one operation(assignment) is allowed on the whole structure itself. In other words, a structure can only be copied to another structure of the same type using the assignment operator. Example : 10

Displaying a struct Variable To display the contents of a struct variable, must display each field separately, using the dot operator: cout << bill; // won t work cout << bill.studentid << endl; cout << bill.name << endl; cout << bill.yearinschool; cout << " " << bill.gpa; 11

Comparing struct Variables Cannot compare struct variables directly: if (bill == william) // won t work Instead, must compare on a field/member basis: if (bill.studentid == william.studentid) 12

struct STUDDATA int id; char name[20]; float gradepoint; }; Program 11-1: A struct to keep a record of data void main( ) STUDDATA studbitg1113; cout << "Please enter your id"; cin >> studbitg1113.id; cout << "\nplease enter your name"; cin.getline(studbitg1113.name,19); cout << "\nplease enter your gradepoint"; cin >> studbitg1113.gradepoint; } cout << "\nyour id is :"<<studbitg1113.id; cout << "\nyour name is :"<<studbitg1113.name; cout << "\nyour grade point is :"<<studbitg1113.gradepoint; if(studbitg1113.gradepoint > 3.5) cout<<"excellent!" 13 13

Nested Structure We can have structures as members of a structures. When a structure includes another structure, it is a nested structure. For example, we can have a structure called STAMP that stores the date and the time. The DATE is in turn a structure that stores the month, day and year. The TIME is also structure, one that stores the hour, minute and second. 14

Declaration of the structure called STAMP is as follows : struct DATE int month; int day; int year; }; struct TIME int hour; int min; int sec; }; struct STAMP DATE date; TIME time; }; 15 STAMP stamp;

It is possible to nest the same structure type more than once in a declaration. Example : struct JOB }; STAMP starttime; STAMP endtime; JOB job; 16

Referencing Nested Structure When accessing a nested structure, we must include each level from the highest (stamp) to the component being referenced. 17

Structure Containing Array Structures can have one or more arrays as members. /*Global declarations */ struct PUPIL char name[26]; int midterm[3]; int final; }; PUPIL student; 18

struct STUDDATA char name[20]; float test[3]; float ass[5]; float quiz[2]; float final; float total; float project; }; Program 11-2: A struct with array as members void main( ) STUDDATA studbitg1113; float tottest=0, totass=0, totquiz=0; cout << "\nplease enter your name : "; cin.getline(studbitg1113.name,19); for( int i = 0; i < 3; i++ ) cout << "\nplease enter the score for test : "<< i+1; cin >> studbitg1113.test[i]; tottest += studbitg1113.test[i]; 19 } *Program continues to the next slide 19

for(i=0; i<5; i++) cout << "\nplease enter the score for assignment"<<i+1<<" : "; cin >> studbitg1113.ass[i]; totass += studbitg1113.ass[i]; } for(i=0; i<2; i++) cout << "\nplease enter the score for quiz"<<i+1 <<" : "; cin >> studbitg1113.quiz[i]; totquiz += studbitg1113.quiz[i]; } cout << "\nplease enter the score for final : "; cin >> studbitg1113.final; cout << "\nplease enter the score for project : "; cin >> studbitg1113.project; studbitg1113.total = tottest + totass + totquiz + studbitg1113.final + studbitg1113.project; cout << "\nyour score for this subject is : << studbitg1113.total; 20 }//end of main() 20

Output : 21

Array of Structure As a programmer, you will encounter many situations that require you to create an array of structures. By putting the data in an array, we can quickly and easily work with the data. Example array of structures might look like : 22

Example : Array of Structure struct PELAJAR int id; char name[31]; float project_mark; int test_mark; int final_mark; char gred; }; PELAJAR rekod_pelajar[3]; OR with initialization : struct pelajar rekod_pelajar[] = 1342, "Zulaiha Ismail", 10.2, 10, 20, F }, 1343, "Aina Ahmad", 51.4, 60, 60, C }, 1344, "Maria Musa", 90.0, 99, 99, A } }; 23

Example : Array of Structure To print the elements in rekod_pelajar : for(i=0; i<3;i++) cout << rekod_pelajar[i].id <<endl; cout << rekod_pelajar[i].name <<endl; cout << rekod_pelajar[i].project_mark <<endl; cout << rekod_pelajar[i].test_mark <<endl; } cout << rekod_pelajar[i].final_mark <<endl; cout << rekod_pelajar[i].gred <<endl; 24

struct STUDDATA char name[20]; float test[3]; float ass[5]; float quiz[2]; float final; float total; float project; }; Program 11-3: Array of struct int main( ) STUDDATA studbitg1113[50]; float tottest=0, totass=0, totquiz=0; for( int i=0; i<50; i++ ) cout << "\nplease enter your name : "; cin.getline(studbitg1113[i].name,19); for( int t=0; t<3; t++ ) cout << "\nplease enter the score for test : "<<i+1; cin >> studbitg1113[i].test[t]; 25 tottest += studbitg1113[i].test[t]; 25 } *Program continues to the next slide

for( int a=0; a<5; a++ ) cout << "\nplease enter the score for assignment"<<i+1<<" : "; cin >> studbitg1113[i].ass[a]; totass += studbitg1113[i].ass[a]; } for( int q=0; q<2; q++ ) cout << "\nplease enter the score for quiz"<<i+1 <<" : "; cin >> studbitg1113[i].quiz[q]; totquiz += studbitg1113[i].quiz[q]; } cout << "\nplease enter the score for final : "; cin >> studbitg1113[i].final; cout << "\nplease enter the score for final : "; cin >> studbitg1113[i].project; studbitg1113[i].total = tottest + totass + totquiz + studbitg1113[i].final + studbitg1113[i].project; cout <<"\nyour score for this subject is : " <<studbitg1113[i].total; }//for i 26 return 0; } //end of main() 26

Structures as Function Parameters May pass members of struct variables to functions: computegpa(stu.gpa); May pass entire struct variables to functions: showdata(stu); Can use reference parameter if function needs to modify contents of structure variable. 27

A Code Snippet: struct as function parameter 28

Structures as Function Parameters - Notes Using value parameter for structure can slow down a program, waste space. Using a reference parameter will speed up program, but function may change data in structure. Using a const reference parameter allows read-only access to reference parameter, does not waste space, speed. 29

Revised showitem Function 30

Returning a Structure from a Function Function can return a struct: STUDENT getstudentdata(); // prototype stu1 = getstudentdata(); // function call Function must define a local structure for internal use for use with return statement 31

Returning a Structure from a Function - Example STUDENT getstudentdata() STUDENT tempstu; cin >> tempstu.studentid; getline(cin, tempstu.pdata.name); getline(cin, tempstu.pdata.address); getline(cin, tempstu.pdata.city); cin >> tempstu.yearinschool; cin >> tempstu.gpa; return tempstu; } 32

Program 11-4 : pg1 33

Program 11-4 : Continued (pg2) 34

Program 11-4 : Continued (pg3) 35 - END -