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.

Slides:



Advertisements
Similar presentations
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
Advertisements

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.
1 Programming Structures COMP102 Prog. Fundamentals, Structures / Slide 2 2 Structures l A Structure is a collection of related data items, possibly.
Structure.
 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the.
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.
Table design screen Field name Data type Field size Other properties.
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
User-defined Structure Types Structures: collection of data of different types. We can define a structure type student_t : typedef struct { string name;
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)
Wednesday, 11/6/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/6/02  QUESTIONS?? – HW # 4 due Monday  Today:  Return HW #3  Arrays (Chap. 10)  Reading:
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.
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.
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.
Structured Data Types array array union union struct struct class class.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 11. The Struct Data Type.
Structure A structure is a collection of variables of different data type under one name. It is a derived data type. e.g. struct employee {int empno; char.
Programmer Defined Structures (Records)
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
Nested LOOPS.
Lecture 131 CS110 Lecture 13 Thursday, March 11, 2004 Announcements –hw5 due tonight –Spring break next week –hw6 due two weeks from tonight Agenda –questions.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
COP Structures Instructor: Diego Rivera-Gutierrez I’m back baby!
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Visibility Control.
Structured Programming Instructor: Prof. K. T. Tsang Lecture 11: Structure and Union 1.
Chapters 1-5 Review C++ Class. Chapter 1 – the big picture Objects Class Inheritance Reusability Polymorphism and Overloading.
Foundation Studies Course M.Montebello Arrays Foundation Studies Course.
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.
Databases 101 © Dolinski What you will learn How relational databases work What are the components that make up a database How to create each component.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
User Defined Data Types - Structures in C CHAPTER 4.
Computer Engineering 2 nd Semester Dr. Rabie A. Ramadan 3.
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.
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.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
1 Lecture10: Structures, Unions and Enumerations 11/26/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Chapter 11 Structures, Unions and Typedef 11.1 Structures Structures allow us to group related data items of different types under a common name. The individual.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
Topic 4 Data Structures Program Development and Design Using C++, Third Edition.
Chapter 6 Data Structures Program Development and Design Using C++, Third Edition.
LESSON 06.
Programming Structures.
EECE.2160 ECE Application Programming
Structures in C++.
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.
הגדרת משתנים יום שלישי 27 נובמבר 2018
Structures.
Starting to think about objects...
Structures In C Programming By Rajanikanth B.
Programming Structures.
Programming Structures.
Chapter 10 C Structures and Unions
Arrays, Casting & User Defined Variables
EECE.2160 ECE Application Programming
Chapter 11 Structure and Union Types.
Structures Chapter 4.
Programming Fundamental
Presentation transcript:

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 used to organize related data (variables) in to a nice package. 46

3 Structures Examples: –Student record: student id, name, major, gender, start year, … –Bank account: account number, name, currency, balance, … –Address book: name, address, telephone number, … In database applications, structures are called records.

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

Declaring Structures (struct) Does Not Reserve Space struct my_example { int label; char letter; char name[20]; } ; /* The name "my_example" is structure name */

6 Definition of a structure: struct { ;... } ; 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. Declaring Structures (struct)

7 struct examples Example: struct StudentInfo{ int Id; int age; char Gender; double CGA; }; 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.

8 Structure Members Each thing in a structure is called member. Each member has a name, a type and a value. Names follow the rules for variable names. Types can be any defined type.

Structure Example

Structure Details

Exercise Write a structure specification that includes three variables—all of type int— called stid, age, and mark. Call this structure student.

12 Making a structure variable By defining a structure you create a new data type. Once a structure is defined, you can create variables of the new type. StudentRecord stu; Part part1;

13 More about struct Declaration of a variable of struct type: ; Example: StudentRecord Student1, Student2; Student1 and Student2 are variables of StudentRecord type. Student1Student2 Name IdGender Dept Name IdGender Dept

Accessing Structure Members Structure (e.g. part ) part1part2part3 part1.modelnumber=2001 part1.partnumber = 11 part1.cost = 100 part2.modelnumber=2005 part2.partnumber = 55 part2.cost = 200 Part3.modelnumber=2007 Part3.partnumber = 60 Part3.cost = 400 part part1 part part2part part3

Accessing Members part1 Structure (e.g. part ) part1. modelnumber part1. partnumber part1. cost part part1 Store values in structure elements part1.modelnumber = 2001 part1.partnumber = 11 part1.cost = 100 Input from users cin>>part1.modelnumber; cin>>part1.partnumber; cin>>part1.cost; Output cout<<part1.modelnumber; cout<<part1.partnumber; cout<<part1.cost;

Another Example Structure struct StudentRecord { char name;// student name double hw[3];// homework grades double test[2];// test grades double ave;// final average }; StudentRecord name hw[0]hw[1]hw[2] test[0]test[1] ave

StudentRecord name hw[0]hw[1]hw[2] test[0]test[1] ave Exercise Declare a variable studentlist from StudentRecord Enter name, homework marks, test marks, and ave into the variable

18 Arrays of structures An ordinary array: One type of data An array of structs: Multiple types of data in each array element … 98 99

Declare Array of Struct struct StudentRecord { char name;// student name double hw[3];// homework grades double test[2];// test grades double ave;// final average }; StudentRecord name hw[0]hw[1]hw[2] test[0]test[1] ave StudentList StudentRecord[10]; StudentRecord name hw[0]hw[1]hw[2] test[0]test[1] ave StudentRecord name hw[0]hw[1]hw[2] test[0]test[1] ave StudentRecord name hw[0]hw[1]hw[2] test[0]test[1] ave StudentRecord name hw[0]hw[1]hw[2] test[0]test[1] ave StudentRecord name hw[0]hw[1]hw[2] test[0]test[1] ave

Array of Struct StudentList StudentRecord[10]; StudentRecord name hw[0]hw[1]hw[2] test[0]test[1] ave StudentRecord name hw[0]hw[1]hw[2] test[0]test[1] ave StudentRecord name hw[0]hw[1]hw[2] test[0]test[1] ave StudentRecord name hw[0]hw[1]hw[2] test[0]test[1] ave StudentRecord name hw[0]hw[1]hw[2] test[0]test[1] ave Enter values to the second student

Another Example

Nested Structure

A Drawing Example d3.feet=d2.feet+d1.feet; } Distance d1={10, 6.75}

A Drawing Example }

Exercise A phone number, such as (212) , can be thought of as having three parts: the area code (212), the exchange (767), and the number (8900). Write a program that uses a structure to store these three parts of a phone number separately. Call the structure phone. Create two structure variables of type phone. Initialize one, and have the user input a number for the other one. Then display both numbers. The interchange might look like this: Enter area code: Enter exchange, Enter number: Then display like below: My number is (212) Your number is (415)

Continue Modify the program so that the program asks names of 10 persons and also phone numbers. Use the phone structure in the previous exercise and create another structure that includes names of persons and phone structure.