1 Programming Structures COMP102 Prog. Fundamentals, Structures / Slide 2 2 Structures l A Structure is a collection of related data items, possibly.

Slides:



Advertisements
Similar presentations
Lecture 19: Using structures in C Language Computer Programming Structures in C Language Lecture 19.
Advertisements

Chapter 7 Completing a Program
Copyright © 2003 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
1 Records C++ Structs Chapter 14 2 What to do with records?  Declaring records  Accessing records  Accessing the field of a record  What is a union?
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Structure.
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
Structures. An array allows us to store a collection of variables However, the variables must be of the same type to be stored in an array E.g. if we.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Overview 10.1 Structures 10.2 Classes 10.3 Abstract Data Types.
1 Lecture 24 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Introduction to C Programming CE Lecture 10 Data Structures typedef and struct.
The evolution of Pokemon society Structures. COMP102 Prog. Fundamentals, Structures / Slide 2 “Good Old Days” At first it was Pokemon and his variables.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Structures COMP104 Structs / Slide 2 Motivation: a new type * Structures hold data that belong together. * Examples: n Student record  student id, name,
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 1 Overview – Chapter Section 4 Arrays and Classes (10.4)
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
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.
S-1 University of Washington Computer Programming I Lecture 18: Structures © 2000 UW CSE.
Structs. Structures We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except.
Programming Initializing Data of Struct Type. COMP102 Prog. Fundamentals: initialize struct type/ Slide 2 Ex. 10: Initialize Data of struct Type l By.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
Structures ANSI-C. Review: Data Structures Functions give us a way to organize programs. Data structures are needed to organize data, especially: –1.
Foundation Studies Course M.Montebello Records Foundation Studies Course.
1 Structures. Structure (struct) Definition A Structure is a container, it can hold a bunch of things. –These things can be of any type. Structures are.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 11. The Struct Data Type.
Module 4: Structures ITEI222 Advanced Programming.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
CPSC 252 Concrete Data Types Page 1 Overview of Concrete Data Types There are two kinds of data types: Simple (or atomic) – represents a single data item.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
Copyright © Curt Hill Structured Data What this course is about.
Structured Programming Instructor: Prof. K. T. Tsang Lecture 11: Structure and Union 1.
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures Lecture
Array, Structure and Union
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
 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.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
Structured Data and Classes
Struct Data Type in C++ What Are Structures?
Struct COMP104 Struct / Slide 2 Motivation * Structures hold data that belong together. * Examples: n Student record  student id, name, major, gender,
Multiple Items in one Data Object Arrays are a way to store more than one piece of data in a data object, provided that all the data is of the same type.
Structures 142 S -1 What is a structure? It is a collection of possibly different types Name the collection Name the components For example: a student.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
CGS 3460 Thus Far n Whenever we declare a variable, we specified its data type. n The data type helped us identify the type of information that a variable.
C Programming Structured Data.
Programming 2. Arrays & structure Arrays : allow you to define variables that combine several data items of the same kind. Structure : is another user.
Topic 4 Data Structures Program Development and Design Using C++, Third Edition.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Structures  Structure types  Pointers and structure types  Structures.
Programmer Defined Types and Classes
Chapter 10-1: Structure.
Programming Structures.
Learning Objectives Structures Structure types
Structures A Structure is a collection of related data items, possibly of different types. A structure type in C++ is called struct. A struct is heterogeneous.
Heterogeneous aggregate datatypes
Array of Structures A structure holds only one record But a record
Structures.
CS148 Introduction to Programming II
Objectives In this chapter, you will: - Learn about records (structs) - Examine operations on a struct - Manipulate data using a struct - Learn about the.
Starting to think about objects...
Structures In C Programming By Rajanikanth B.
Lec17 Structs.
Programming Structures.
Programming Structures.
Structures Structured Data types Data abstraction structs ---
Programming Fundamental
Presentation transcript:

1 Programming Structures

COMP102 Prog. Fundamentals, Structures / Slide 2 2 Structures l A Structure is a collection of related data items, possibly of different types. l A structure type in C++ is called struct. l A struct is heterogeneous in that it can be composed of data of different types. l In contrast, array is homogeneous since it can contain only data of the same type.

COMP102 Prog. Fundamentals, Structures / Slide 3 3 Structures l Structures hold data that belong together. l Examples: n Student record: student id, name, major, gender, start year, … n Bank account: account number, name, currency, balance, … n Address book: name, address, telephone number, … l In database applications, structures are called records.

COMP102 Prog. Fundamentals, Structures / Slide 4 4 Structures l Individual components of a struct type are called members (or fields). l Members can be of different types (simple, array or struct). l A struct is named as a whole while individual members are named using field identifiers. l Complex data structures can be formed by defining arrays of structs.

COMP102 Prog. Fundamentals, Structures / Slide 5 5 struct basics l Definition of a structure: struct { ;... } ; l Example: struct Date { int day; int month; int year; } ; The “Date” structure has 3 members, day, month & year. Each identifier defines a member of the structure.

COMP102 Prog. Fundamentals, Structures / Slide 6 6 struct examples l Example: struct StudentInfo{ int Id; int age; char Gender; double CGA; }; l Example: struct StudentGrade{ char Name[15]; char Course[9]; int Lab[5]; int Homework[3]; int Exam[2]; }; The “StudentGrade” structure has 5 members of different array types. The “StudentInfo” structure has 4 members of different types.

COMP102 Prog. Fundamentals, Structures / Slide 7 7 struct examples l Example: struct BankAccount{ char Name[15]; int AcountNo[10]; double balance; Date Birthday; }; l Example: struct StudentRecord{ char Name[15]; int Id; char Dept[5]; char Gender; }; The “StudentRecord” structure has 4 members. The “BankAcount” structure has simple, array and structure types as members.

COMP102 Prog. Fundamentals, Structures / Slide 8 8 struct basics l Declaration of a variable of struct type: ; l Example: StudentRecord Student1, Student2; Student1 and Student2 are variables of StudentRecord type. Student1Student2 Name IdGender Dept Name IdGender Dept

COMP102 Prog. Fundamentals, Structures / Slide 9 9 Chan Tai Man M COMP Ex. 1: struct basics l The members of a struct type variable are accessed with the dot (.) operator:. ; l Example: strcpy(Student1.Name, "Chan Tai Man"); Student1.Id = 12345; strcpy(Student1.Dept, "COMP"); Student1.gender = 'M'; cout << "The student is "; switch (Student1.gender){ case 'F': cout << "Ms. "; break; case 'M': cout << "Mr. "; break; } cout << Student1.Name << endl; Student1 Name IdGender Dept

COMP102 Prog. Fundamentals, Structures / Slide 10 10

COMP102 Prog. Fundamentals, Structures / Slide Chan Tai Man M COMP Ex. 2: struct-to-struct assignment The values contained in one struct type variable can be assigned to another variable of the same struct type. l Example: strcpy(Student1.Name, "Chan Tai Man"); Student1.Id = 12345; strcpy(Student1.Dept, "COMP"); Student1.gender = 'M'; Student2 = Student1; Student1 Chan Tai Man M COMP Student2

COMP102 Prog. Fundamentals, Structures / Slide 12 12

COMP102 Prog. Fundamentals, Structures / Slide Ex. 3-5: Nested structures We can nest structures inside structures. l Examples: struct point{ double x, y; }; point P; struct line{ point p1, p2; }; line L; struct triangle{ point p1, p2, p3; }; triangle T; (P.x, P.y) (L.p1.x, L.p1.y) (L.p2.x, L.p2.y) (T.p2.x, T.p2.y) (T.p1.x, T.p1.y) (T.p3.x, T.p3.y)

COMP102 Prog. Fundamentals, Structures / Slide Ex. 3-5: Nested structures We can nest structures inside structures. l struct line{ point p1, p2; }; line L; (L.p1.x, L.p1.y) (L.p2.x, L.p2.y) line p1 p2 x y

COMP102 Prog. Fundamentals, Structures / Slide Ex. 3-5: Nested structures Assign values to the variables P, L, and T using the picture: point P; line L; triangle T; (4, 11) (2, 7) (10, 9) (6, 5) (2, 0) (8, 3) l Ex. 3: Graph a point l Ex. 4: Graph a line l Ex. 5: Graph a triangle

COMP102 Prog. Fundamentals, Structures / Slide Ex. 3-5: Nested structures point P; line L; triangle T; P.x = 4; P.y = 11; (4, 11) (2, 7) (10, 9) (6, 5) (2, 0) (8, 3) L.p1.x = 2; L.p1.y = 7; L.p2.x = 10; L.p2.y = 9; T.p1.x = 2; T.p1.y = 0; T.p2.x = 6; T.p2.y = 5; T.p3.x = 8; T.p3.y = 3;

COMP102 Prog. Fundamentals, Structures / Slide Ex. 3: Graphing a Point x y (x 1, y 1 ) *

struct point {int x, y;}; void user_input(point&); void graph_point(char grid[NUMBER_ROWS][NUMBER_COLS], point); void print_grid(char grid[NUMBER_ROWS][NUMBER_COLS]); void set_background(char grid[][NUMBER_COLS]); void user_input(point& P){ // pass by reference // get user input and check that it is on the grid do{ cout << "Enter column (x<" << NUMBER_COLS << ") & row (y<" << NUMBER_ROWS <<") of the 1st point: "; cin >> P.x >> P.y; } while ((P.y = NUMBER_ROWS) || (P.x = NUMBER_COLS)); } // Put a point on the grid void graph_point(char grid[][NUMBER_COLS], point P){ grid[P.y][P.x] = '*'; }

COMP102 Prog. Fundamentals, Structures / Slide 19 19

COMP102 Prog. Fundamentals, Structures / Slide Ex. 4: Trajectory Between 2 Points x y The equation of a line going through two points (x 1, y 1 ) & (x 2, y 2 ) can be represented by: (y-y 1 )/ (x-x 1 ) = (y 2 -y 1 )/(x 2 -x 1 ) or y = ((y 2 -y1)/(x 2 -x 1 )) (x-x 1 ) + y 1 where (y 2 -y 1 )/(x 2 -x 1 ) is called the slope. (x 1, y 1 ) (x 2, y 2 ) * * *

// use struct to graph line between two points #include // for tolower #include using namespace std; int const NUMBER_ROWS = 11; int const NUMBER_COLS = 31; struct point {int x, y;}; struct line {point p1, p2;}; void user_input (line&); void graph_line (char grid[NUMBER_ROWS][NUMBER_COLS], line); void print_grid(char grid[NUMBER_ROWS][NUMBER_COLS]); void set_background (char grid[][NUMBER_COLS]);

// Graph line between two points int main(){ // set an array for the grid char grid[NUMBER_ROWS][NUMBER_COLS]; line line1; int row1=0, col1=0, row2=0, col2=0; char do_another; // function call to fill background of grid with ‘-’ set_background(grid); do{ // do-while loop allows multiple lines // get user input user_input(line1); // put ‘*’ into array to graph the line(s) on the grid graph_line(grid, line1); // print the grid from the array to the screen print_grid(grid); cout << "Do you want to add another line (y/n)? "; cin >> do_another; do_another = tolower(do_another); } while (do_another == 'y'); return 0; }

COMP102 Prog. Fundamentals, Structures / Slide Ex. 4: More on Graphing Line //A function to get user input and check that it is on the grid. void user_input(line& line1){ do{ cout << "Enter column (x<" << NUMBER_COLS << ") & row (y<" << NUMBER_ROWS << ") coordinates of the 1st point: "; cin >> line1.p1.x >> line1.p1.y; } while ((line1.p1.y<0) || (line1.p1.y>=NUMBER_ROWS) || (line1.p1.x<0) || (line1.p1.x >= NUMBER_COLS)); // use another do-while loop for the 2 nd point, col2 and row2 }

COMP102 Prog. Fundamentals, Structures / Slide Ex. 4: More on Graphing Line void graph_line(char grid[][NUMBER_COLS], line line1){ int row, col; double rise, run, slope; // one point if((line1.p1.y==line1.p2.y)&&(line1.p1.x==line2.p2.x)) grid[line1.p1.y][line1.p1.x] = '*'; else if(line1.p2.x==line1.p1.x){ // infinite slope if (line1.p1.y < line1.p2.y){ for(row=line1.p1.y; row <= line1.p2.y; row++) grid[row][line1.p1.x] = '*'; } else{ for(row=line1.p1.y; row >= line1.p2.y; row--) grid[row][line1.p1.x] = '*'; }

COMP102 Prog. Fundamentals, Structures / Slide Ex. 4: More on Graphing Line else{ rise=line1.p2.y-line1.p1.y; run=line1.p2.x-line1.p1.x; slope = (double)rise / run; // run cannot = 0 if (run >0){ for(col = line1.p1.x; col <= line1.p2.x; col++){ // line1.p1.y is offset for starting point row=(int)(slope*(col–line1.p1.x)+line1.p1.y); grid[row][col] = '*'; } else{ for(col=line1.p1.x; col >= line1.p2.x; col--){ row=(int)(slope*(col–line1.p1.x)+line1.p1.y); grid[row][col] = '*'; }

COMP102 Prog. Fundamentals, Structures / Slide 26 26

COMP102 Prog. Fundamentals, Structures / Slide Arrays of structures l An ordinary array: One type of data l An array of structs: Multiple types of data in each array element … 98 99

COMP102 Prog. Fundamentals, Structures / Slide Arrays of structures l We often use arrays of structures. Example: StudentRecord Class[100]; strcpy(Class[98].Name, "Chan Tai Man"); Class[98].Id = 12345; strcpy(Class[98].Dept, "COMP"); Class[98].gender = 'M'; Class[0] = Class[98]; … Chan Tai Man M COMP

COMP102 Prog. Fundamentals, Structures / Slide Arrays inside structures l We can use arrays inside structures. Example: struct square{ point vertex[4]; }; square Sq; Assign values to Sq using the given square (4, 3)(10, 3) (4, 1)(10, 1) x y