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.

Slides:



Advertisements
Similar presentations
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Advertisements

1 Programming Structures COMP102 Prog. Fundamentals, Structures / Slide 2 2 Structures l A Structure is a collection of related data items, possibly.
Structure.
Structures Spring 2013Programming and Data Structure1.
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Overview 10.1 Structures 10.2 Classes 10.3 Abstract Data Types.
Starting Classes and Methods. Objects have behaviors In old style programming, you had: –data, which was completely passive –functions, which could manipulate.
Programming with Collections Collections in Java Using Arrays Week 9.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Introduction to C Programming CE Lecture 10 Data Structures typedef and struct.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.
ASP.NET Programming with C# and SQL Server First Edition
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
Chapter 9 Introduction to Arrays
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.
Values, variables and types © Allan C. Milne v
DCT1063 Programming 2 CHAPTER 5 ADVANCED DATA TYPE (part 1) Mohd Nazri Bin Ibrahim Faculty of Computer Media and Technology TATi University College
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.
© 2012 EMC Publishing, LLC Slide 1 Chapter 8 Arrays  Can store many of the same type of data together.  Allows a collection of related values to be stored.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
CS 31 Discussion, Week 8 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:00-1:00pm.
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.
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
CPS120: Introduction to Computer Science Lecture 15A Structures.
Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and.
Arrays Adapted from materials created by Dr. Donald Bell, Cal Poly 2000 (updated February 2004)
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.
Program Organization Sequential Execution: One line done after the other Conditional Execution: If a test is true, one section is done, otherwise another.
Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures as Functions.
CPS120: Introduction to Computer Science Data Structures.
1 CS161 Introduction to Computer Science Topic #15.
Introducing Arrays. Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
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.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Grouping Data Together Often we want to group together a number of values or objects to be treated in the same way e.g. names of students in a tutorial.
CS-1030 Dr. Mark L. Hornick 1 References & Pointers.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
Programming Fundamentals Enumerations and Functions.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
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.
Chapter 9 Introduction to Arrays Fundamentals of Java.
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 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Retrieving information from forms
Programming Structures.
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Structures Lesson xx In this module, we’ll introduce you to structures.
Structs And Arrays.
Lists in Python.
Objectives In this chapter, you will: - Learn about records (structs) - Examine operations on a struct - Manipulate data using a struct - Learn about the.
Topics Introduction to Functions Defining and Calling a Function
Lec17 Structs.
More on Structs Sometimes we use structs even when the fields are all of the same type. If the fields are different conceptually, that is, the data stands.
Structures Structured Data types Data abstraction structs ---
Programming Fundamental
Retrieving information from forms
Presentation transcript:

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 (homogeneous type). If we want to store data of different types (heterogeneous type) together in one object, we can use an array. Why would we want to do that?

There are several reasons: Conceptually, the pieces of data might be related, and we want to store them together. It's easier to move around a bunch of items if we put them in a box first. In this case, moving around means passing to and from functions. It simplifies the programming task by having less things to think about.

Struct A struct (for structure) is a way to associate different types of data with a single data object. Unlike an array, which uses numbers (ints starting at 0) to identify each data item, a struct names the data items. So, there will be two names – one for the data object itself (with multiple items) and one name for each particular item.

Struct - Example Suppose we want to represent information about a student (a student record). We might have the following items: First name Last name Student id number Birthday Year (Freshman, Sophomore,...) QPA

Example (cont'd) We represent a student record with a struct as follows: struct StudentRecord { string firstName; string lastName; int id; int year; double qpa; }; (We'll pretend for the moment we know what strings are.)

Example (cont'd) This creates a new type, a StudentRecord, which is a struct with five fields (or components, or member variables), that is, a description of a data object which can hold five pieces of disparate, but related, values. To actually create a data object, we use a declaration statement: StudentRecord sr;

Struct Data Object sr data object

Accessing the Fields In an array, the different values are indicated by using an index, an int. In a struct, the different fields are accessed by referring to them by the name of the field. The syntax is:. For example, sr.firstName, sr.lastName, sr.qpa This identifier can be used for both setting and retrieving a value.

More on Field Names The names of fields are local to their structs, i.e., different structs may use the same field names without confusion. The field names are identifiers and following the usual rules. Field names do have do be distinct within a struct.

Structs and Functions Structs may be passed into functions as arguments, just like other data objects. A struct parameter can be call-by-value or call-by- reference. The return type of a function may be a struct.