Structures EE2372 Software Design I Dr. Gerardo Rosiles.

Slides:



Advertisements
Similar presentations
Exposure C++ Chapter XVI C++ Data Structures, the Record.
Advertisements

C Language.
1 Programming Structures COMP102 Prog. Fundamentals, Structures / Slide 2 2 Structures l A Structure is a collection of related data items, possibly.
Structures Spring 2013Programming and Data Structure1.
Week 8 Arrays Part 2 String & Pointer
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.
Pointers EE2372 Dr. Gerardo Rosiles. Introduction Pointers are one of the most advanced features in the C-language. Allows to generalize code so different.
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
Introduction to C Programming CE Lecture 10 Data Structures typedef and struct.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 11: Records (structs)
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Computer Science 210 Computer Organization Strings in 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.
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.
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.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
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.
IF-ELSE IF-ELSE STATEMENT SWITCH-CASE STATEMENT Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Computer Science 210 Computer Organization Arrays.
Cosc237/structures1 Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT,
User Defined Data Types - updated Chapter 10: User Defined Data Types Objectives: In this chapter you will learn about, Introduction Declaring.
1 Structures UniMAP SEM I - 11/12EKT 120 Computer Programming.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi.
CPS120: Introduction to Computer Science Lecture 15A Structures.
ENEE150 – 0102 ANDREW GOFFIN Project 4 & Function Pointers.
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.
Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures as Functions.
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.
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Computer Programming for Engineers
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
1. 2 Introduction Structure Definitions and Declarations Initializing Structures Operations on Structures Members Structures as Functions Parameters Array.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
POORIA PANAHIFAR Mechanical Engineering Instructor: Dr. B. TAHERI The only thing that you have to do in order to understand what I’m saying is just sit.
chap11 Chapter 11 Structure and Union Types.
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.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
Chapter 6. Character String Types It is one in which the values consists of sequences of characters. How to Define a variable contain a string? In a programming.
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
Structured Data Objects (Structs). array Recall our definition of an array: fixedcontiguousall of the same data type an array is a fixed number of contiguous.
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Structs Structured Data Objects (Structs) Chapter 7.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Chapter 8 Arrays, Strings and Pointers
Computer Science 210 Computer Organization
CS1010 Programming Methodology
C Programming Tutorial – Part I
Module 2 Arrays and strings – example programs.
Computer Science 210 Computer Organization
Structures Lesson xx In this module, we’ll introduce you to structures.
Structure ការណែនាំអំពី Structure
Structures In C Programming By Rajanikanth B.
CPS120: Introduction to Computer Science
Chapter 10 C Structures and Unions
Structures, Unions, and Enumerations
Presentation transcript:

Structures EE2372 Software Design I Dr. Gerardo Rosiles

Introduction Arrays let you group elements of the same type. In many cases we want to group elements of different types that are seen as belonging to the same entity. Consider the information in your drivers license. It is stored as a “data record” on a computer which can be accessed by an officer when you are stopped for a ticket or something.

Introduction This data record is also known as a data structure in programming parlance. What are the elements of driver’s license structures:

Introduction structure driver license { Name DOB DL # Height Eyes Sex Address Expiration Date Picture }

Introduction A structure allows us to encapsulate this information of a well organized way. Each element of the structure can be further split: structure name { first MI last } structure date { month day year } structure address { number street City State ZIP }

C structures C allows us to create structures that combine different types. You use the struct key word with the following syntax: struct structure_name { type1 identifier1; type2 identifier2; type3 identifier3; etc. } The we can create variables that hold all the information as follows: struct_name var1, var2;

C structures – Examples struct date { int month; int day; int year; }; struct student_data { char name[SIZE]; int age; float grade; }; struct full_name { char first[20]; char MI; char last [30]; }; Note that we have included an array in this structure.

Quick introduction to Strings Strings are a way to group characters into words and sentences using arrays of characters. A quick way to create a string is as follows: char word[20]; word = “Hello”; As long as you use less than 19 characters you are fine. WHY?? We will discuss this in Chapter 10.

Using C structures struct student_data student1, student2; student1.name = “sam”; student1.age = 20; student1.grade = 3.5; printf("name: %s\n",student1.name); printf("age: %d\n",student1.age); printf("grade: %f\n",student1.grade); student2=student1; printf("name: %s\n",student2.name); printf("age: %d\n",student2.age); printf("grade: %f\n",student2.grade); } #include #define SIZE 10 main() { struct student_data { char name[SIZE]; int age; float grade; };

Using C structures To access the elements of a structure we use the “dot” operator. Initialization Accessing for printing Copying structure content struct data student1, student2; student1.name = “sam”; student1.age = 20; student1.grade = 3.5; printf("name: %s\n",student1.name); printf("age: %d\n",student1.age); printf("grade: %f\n",student1.grade); student2=student1; printf("name: %s\n",student2.name); printf("age: %d\n",student2.age); printf("grade: %f\n",student2.grade);

Using C structures There are other ways to initialize structures Initialize at declaration time struct data student1={"sam",20,3.5}; Use scanf for user defined values printf(“Enter student name: “); scanf(“ %s \n”, &student1.name); printf(“Enter student age: “); scanf(“%d \n”, &student1.age); printf(“Enter student grade: “); scanf(“%f \n”, &student1.grade);

Using C structures Individual components of structures can be used in any operation as any variable: arithmetic, logical, relational. if (student1.grade < 1.0) printf(“College of Business \n”); if( student_birthday_passed(student.age) ) student.age = student.age + 1;

Using C structures More interesting use of structures: – Structures containing arrays. We have seen this for the name strings. – Structures containing structures. – Arrays of structures.

Structure containing structures Perhaps the most useful feature of structures. Allows structured implementation of data records. Example 2 struct full_name { char first_name[SIZE]; char last_name[SIZE]; }; struct address { char street[SIZE]; unsigned int zipcode; }; struct position { char title[SIZE]; float salary; }; struct worker { struct full_name name; struct address home_address; char tel[SIZE]; struct position category; };

Arrays of structures Same definition as with arrays struct student_data EE2372_students[30]; We can access each structure through an index and then each element of the structure through the dot operator. EE2372_students[17].name = “Gerardo”; EE2372_students[17].age = 25; EE2372_students[17].grade = 4.0; Example 3

Functions and structures We can pass structures to functions as parameters Functions can return structures Example 5 Are parameters passed by value of by reference.? Let’s modify Example 4.