1 11/30/05CS150 Introduction to Computer Science 1 Structs.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
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.
Structure.
Introduction to Programming Lecture 39. Copy Constructor.
Structures Spring 2013Programming and Data Structure1.
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 25 Thanks for Lecture Slides: Dr. Sadaf Tanveer Dr. Sadaf Tanveer,
Structures, Unions, and Typedefs CS-2301 D-term Structures, Unions, and Typedefs CS-2301 System Programming D-term 2009 (Slides include materials.
1 Lecture 32 Handling Selected Topics Again!! Structs and Arrays of Structs Special Lectures.
C structures and unions (Reek, Ch. 10) 1CS 3090: Safety Critical Programming in C.
CS150 Introduction to Computer Science 1
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
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.
Chapter 11: Structured Data. Slide Introduction An array makes it possible to access a list or table of data of the same data type by using a single.
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
S-1 University of Washington Computer Programming I Lecture 18: Structures © 2000 UW CSE.
Engineering Computing I Chapter 6 Structures. Sgtructures  A structure is a collection of one or more variables, possibly of different types, grouped.
1 3/2/05CS250 Introduction to Computer Science II Composition and friend Functions.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
1 CS161 Introduction to Computer Science Topic #10.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures Lecture
Spring 2005, Gülcihan Özdemir Dağ Lecture 11, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 11 Outline 11.1.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
1 CS161 Introduction to Computer Science Topic #17.
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
Standard C++ Library Part II. Last Time b String abstraction b Containers - vector.
ECE 103 Engineering Programming Chapter 50 Structures Unions, Part 2 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
1 CS161 Introduction to Computer Science Topic #15.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
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.
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.
1 11/12/04CS150 Introduction to Computer Science 1 More Arrays.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
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.
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)
Student Book An Introduction
CS150 Introduction to Computer Science 1
CS 1430: Programming in C++.
Structures Lesson xx In this module, we’ll introduce you to structures.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS148 Introduction to Programming II
CS150 Introduction to Computer Science 1
EGR 2261 Unit 12 structs Read Malik, Chapter 9.
CS111 Computer Programming
CS148 Introduction to Programming II
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Lec17 Structs.
Chapter 9: Pointers and String
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Structures, Unions, and Enumerations
Presentation transcript:

1 11/30/05CS150 Introduction to Computer Science 1 Structs

2 11/30/05CS150 Introduction to Computer Science 1 Last Time  We finished arrays  Today we will o Look at a new way of storing data called structs (short for structures)

3 11/30/05CS150 Introduction to Computer Science 1 Arrays and Data Types  Useful for storing a collection of data elements of the same data type (float, int, string). char myName[5]; //All elements chars float salaries[NUM_EMP]; //All elements floats char vowels[]={ ‘ A ’, ’ E ’, ’ I ’, ’ O ’, ’ U ’ };  What about storing a collection of data elements of different data types?

4 11/30/05CS150 Introduction to Computer Science 1 Data with Different Data Types  For example, what if we wanted to keep the following information on a particular employee: o employee id o SS# o number of children o salary o citizen  The elements have different data types, so we can’t conveniently use an array. Instead we will use a struct ( short for structure)

5 11/30/05CS150 Introduction to Computer Science 1 Structure Definition To store this information:  employee id  SS#  number of children  salary  citizen We would begin by defining a structure : struct employ { int id int ssnum; int numchild; float salary; bool citizen; };

6 11/30/05CS150 Introduction to Computer Science 1 Struct Terminology For this struct: struct employ { int id int ssnum; int numchild; float salary; bool citizen; };  employ is the identifier name and a NEW data type.  The individual components id, ssnum, etc. are called members.

7 11/30/05CS150 Introduction to Computer Science 1 Struct Declaration  As with all data types, in order to use our new data type employ we must allocate storage space by declaring variables of this data type: employ engineer, tech;  This will allocate space for two variables called engineer and tech with the previously described members id, ssnum, etc

8 11/30/05CS150 Introduction to Computer Science 1 Member Access Operator  To access a struct member, we use the member access operator (period between struct variable name and member name).  In the variable engineer of data type employ we can make the assignments: engineer.id = 12345; engineer.ssnum = ; engineer.numchild = 2; engineer.salary = ; engineer.citizen = true;  How do we access the data in arrays?

9 11/30/05CS150 Introduction to Computer Science 1 Example One  22.1: Write a C++ struct data type realnum that will have members number, realpart, and intpart  22.2: Declare a variable numinfo of that type  22.3: Place the value in the field number

10 11/30/05CS150 Introduction to Computer Science 1 Structs as function arguments  Structs can be passed to functions by reference or value in the same manner that other data types have been passed  Generally, passing structs by reference is preferred since passing by value requires a local copy of the struct to be created within the function’s variables

11 11/30/05CS150 Introduction to Computer Science 1 Example Two  22.4: Write a C++ function split that accepts a variable of type realnum  22.5: Assign the integer part of the number to the member variable intpart and the real part of the number to the member variable realpart  See the function prototype on the next slide

12 11/30/05CS150 Introduction to Computer Science 1 Example Two Solution  Function prototype: void split(realnum &);  Function call: split (numinfo);  Function definition: You write

13 11/30/05CS150 Introduction to Computer Science 1 Example Three Consider the following struct data type: struct info { int num; int divisors[10]; int howmany; }; 22.6: Write a C++ function compute that accepts a variable of type info and returns all the divisors greater than 1 of the variable num in the array divisors and the number of divisors in the variable howmany

14 11/30/05CS150 Introduction to Computer Science 1 Summary  In today’s lecture we covered o Structures