Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration Lecture 23.

Slides:



Advertisements
Similar presentations
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Structures –Collections of related.
Advertisements

Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
Chapter 10 C Structures, Unions, Bit Manipulations, and Enumerations.
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.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Data Types in C. Data Transformation Programs transform data from one form to another –Input data  Output data –Stimulus  Response Programming languages.
C Language.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect FXP. 1Winter Quarter Course Wrap Up and Final Review Topics Lecture.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Engineering EG167C - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M1 P. 1Winter Quarter Midterm I Review.
Structures Spring 2013Programming and Data Structure1.
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 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 1Winter Quarter Scope of Variables.
CS 201 Structure & Union Debzani Deb.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M1 P. 1Winter Quarter Midterm I Review Topics.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 1Winter Quarter Switch Case Structures.
Data Types.
Week 7 – String. Outline Passing Array to Function Print the Array How Arrays are passed in a function call Introduction to Strings String Type Character.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 15P. 1Winter Quarter Arrays and Pointers.
Data Type. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (4)
C Tokens Identifiers Keywords Constants Operators Special symbols.
Week 9 - Friday.  What did we talk about last time?  typedef  Linked lists.
Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 8 Structures, Unions,
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
GUIDED BY- A.S.MODI MADE BY- 1. SHWETA ALWANI 2. PRIYANKA.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
16. STRUCTURES, UNIONS, AND ENUMERATIONS. Declaring Structures A structure is a collection of one or more components (members), which may be of different.
Spring 2005, Gülcihan Özdemir Dağ Lecture 11, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 11 Outline 11.1.
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
© Oxford University Press All rights reserved. CHAPTER 8 STRUCTURES.
CPS120: Introduction to Computer Science Lecture 15A Structures.
Chapter2 Constants, Variables, and Data Types. 2.1 Introduction In this chapter, we will discuss –constants (integer, real, character, string, enum),symbolic.
Weekly C-minar Week 0. Today: Steps of the compile Basic inclusion/syntax rules Low-cost containment Debugging.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect M2P. 1Winter Quarter Midterm II Review Topics Lecture M2.
Structure A collection of values (members) struct date{ int day; char month[10]; int year; }; Declare a structure variable struct date today; struct struct_name.
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
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.
Structures, Unions, Enumerations
Pointers, Enum, and Structures
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
CSE101-Lec#3 Components of C Identifiers and Keywords Data types.
Structure.
C Structures, Unions, and Enumerations
C Structures, Unions, Bit Manipulations and Enumerations
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Derived types.
Windows Programming Lecture 04
Engineering H192 Winter 2005 Structs and Enumeration Prof. Almas Ansari Dept of Computer Science Engineering Anjuman College of Engineering & Technology..
Presentation transcript:

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration Lecture 23

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 2Winter Quarter Data Structures (struct) Arrays require that all elements be of the same data type. Many times it is necessary to group information of different data types. An example is a materials list for a product. The list typically includes a name for each item, a part number, dimensions, weight, and cost. C and C++ support data structures that can store combinations of character, integer floating point and enumerated type data. They are called a structs.

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 3Winter Quarter Structures (struct) A struct is a derived data type composed of members that are each fundamental or derived data types. A single struct would store the data for one object. An array of structs would store the data for several objects. A struct can be defined in several ways as illustrated in the following examples:

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 4Winter Quarter Declaring Structures (struct) Reserves Space struct my_example { int label; char letter; char name[20]; } mystruct ; Does Not Reserve Space struct my_example { int label; char letter; char name[20]; } ; /* The name "my_example" is called a structure tag */

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 5Winter Quarter User Defined Data Types (typedef) The C language provides a facility called typedef for creating synonyms for previously defined data type names. For example, the declaration: typedef int Length; makes the name Length a synonym (or alias) for the data type int. The data “type” name Length can now be used in declarations in exactly the same way that the data type int can be used: Length a, b, len ; Length numbers[10] ;

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 6Winter Quarter Typedef & Struct Often, typedef is used in combination with struct to declare a synonym (or an alias) for a structure: typedef struct /* Define a structure */ { int label ; char letter; char name[20] ; } Some_name ; /* The "alias" is Some_name */ Some_name mystruct ; /* Create a struct variable */

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 7Winter Quarter Accessing Struct Members Individual members of a struct variable may be accessed using the structure member operator (the dot, “.”): mystruct.letter ; Or, if a pointer to the struct has been declared and initialized Some_name *myptr = &mystruct ; by using the structure pointer operator (the “->“): myptr -> letter ; which could also be written as: (*myptr).letter ;

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 8Winter Quarter Sample Program With Structs /* This program illustrates creating structs and then declaring and using struct variables. Note that struct personal an included data type in struct "identity". */ #include struct personal { long id; float gpa; } ; struct identity { char name[30]; struct personal person; } ;

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 9Winter Quarter Sample Program With Structs (cont.) int main ( ) { struct identity js = {"Joe Smith"}, *ptr = &js ; js.person.id = ; js.person.gpa = 3.4 ; printf ("%s %ld %f\n", js.name, js.person.id, js.person.gpa) ; printf ("%s %ld %f\n", ptr->name, ptr->person.id, ptr->person.gpa) ; }

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 10Winter Quarter Structs with Union /* The program on the next 3 slides creates a union and makes it a member of struct personal which is, in turn, a member of struct identity. The union uses the same memory location for either rank or a character string (deg) depending on the answer to the prompt for student status in main( ) */

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 11Winter Quarter Structs with Union (cont.) #include union status { int rank ; char deg[4] ; } ; struct personal { long id ; float gpa ; union status level ; } ; struct identity { char name[30] ; struct personal person ; } ;

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 12Winter Quarter Structs with Union (cont.) int main( ) { struct identity jb = {"Joe Brown"}, *ptr = &jb; char u_g; jb.person.id = ; jb.person.gpa = 3.4 ; printf ("Enter student status - u or g\n"); scanf ("%c", &u_g); if (u_g == 'u') { printf ("Enter rank -- 1, 2, 3, 4 or 5\n"); scanf ("%d", &jb.person.level.rank); printf ("%s is level %d\n”, jb.name, jb.person.level.rank); } /* End of if statement */

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 13Winter Quarter Structs with Union (cont.) else { printf ("Enter degree sought -- ms or phd\n"); scanf ("%s", &jb.person.level.deg); printf ("%s is a %s candidate\n”, jb.name, jb.person.level.deg ); } /* End of else statement */ printf ("%s %ld %f\n”, jb.name, jb.person.id, jb.person.gpa ); printf ("%s%ld %f\n”, ptr->name, ptr->person.id, ptr->person.gpa ); } /* End of program */

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 14Winter Quarter Enumeration Enumeration is a user-defined data type. It is defined using the keyword enum and the syntax is: enum tag_name {name_0, …, name_n} ; The tag_name is not used directly. The names in the braces are symbolic constants that take on integer values from zero through n. As an example, the statement: enum colors { red, yellow, green } ; creates three constants. red is assigned the value 0, yellow is assigned 1 and green is assigned 2.

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 15Winter Quarter Enumeration /* This program uses enumerated data types to access the elements of an array */ #include int main( ) { int March[5][7]={{0,0,1,2,3,4,5},{6,7,8,9,10,11,12}, {13,14,15,16,17,18,19},{20,21,22,23,24,25,26}, {27,28,29,30,31,0,0}}; enum days {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 16Winter Quarter Enumeration enum week {week_one, week_two, week_three, week_four, week_five}; printf ("Monday the third week " "of March is March %d\n", March [week_three] [Monday] ); }