1 CS161 Introduction to Computer Science Topic #15.

Slides:



Advertisements
Similar presentations
CS 141 Computer Programming 1 1 Arrays. Outline  Introduction  Arrays  Declaring Arrays  Examples Using Arrays  Sorting Arrays  Multiple-Subscripted.
Advertisements

Structure.
Structures Spring 2013Programming and Data Structure1.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 25 Thanks for Lecture Slides: Dr. Sadaf Tanveer Dr. Sadaf Tanveer,
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Road Map Introduction to object oriented programming. Classes
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann.
Chapter 11 Structure. 2 Objectives You should be able to describe: Structures Arrays of Structures Structures as Function Arguments Dynamic Structure.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Pointer Data Type and Pointer Variables
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
CS161 Topic #15 1 Today in CS161 Lecture #15 Practicing! Writing Programs to Practice Write a game program (1 player) of Mad Math Reuse the functions to.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction Structures are somewhat like an array in that.
Introduction to C++ Functions Topic #2 1CS162 Topic #2.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
DCT1063 Programming 2 CHAPTER 5 ADVANCED DATA TYPE (part 1) Mohd Nazri Bin Ibrahim Faculty of Computer Media and Technology TATi University College
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.
CS1201: Programming Language 2 Structure By: Nouf Almunyif.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
CS161 Week #8 1 Today in CS161 Week #8 Practicing! Writing Programs to Practice Write a program that counts the number of vowels in a sentence, ended by.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
1 CS161 Introduction to Computer Science Topic #10.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
1 CS161 Introduction to Computer Science Topic #9.
A FIRST BOOK OF C++ CHAPTER 16 DATA STRUCTURES. OBJECTIVES In this chapter, you will learn about: Single Structures Arrays of Structures Structures as.
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.
CS Class 03 Topics  Sequence statements Input Output Assignment  Expressions Read pages Read pages 40 – 49 for next time.
+ 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.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
1 CSC103: Introduction to Computer and Programming Lecture No 24.
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.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
1 CS161 Introduction to Computer Science Topic #16.
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.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
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.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Arrays.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Arrays Chapter 7. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
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];
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
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.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
Two-Dimensional Arrays Lesson xx
CS150 Introduction to Computer Science 1
Structures Lesson xx In this module, we’ll introduce you to structures.
CS148 Introduction to Programming II
Introduction to C++ Functions
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Presentation transcript:

1 CS161 Introduction to Computer Science Topic #15

CS161 Topic #152 Today in CS161 Structures –What is a structure –Why would we use them –How do we define structures –How do we define variables of structures –How do we define arrays of structures –How do we pass structures as arguments Programming Questions?

CS161 Topic #153 What is a Structure A structure is a way for us to group different types of data together under a common name With an array, we are limited to having only a single type of data for each element... –think of how limiting this would be if we wanted to maintain an inventory –we’d need a separate array for each product’s name, another for each product’s price, and yet another for each barcode!

CS161 Topic #154 What is a Structure With a structure, on the other hand, we can group each of these under a common heading –So, if each product can have a description, a price, a cost, and a barcode....a single structure entity can consist of an array of characters for the description, two floats for the price and cost, and an int for the barcode –Now, to represent the entire inventory we can have an array of these “products”

CS161 Topic #155 Why would we use a Structure Some people argue that with C++ we no longer need to use the concept of structures And, yes, you can do everything that we will be doing with structures, with a “class” (which we learn about next week!) My suggestion is to use structures whenever you want to group different types of data together, to help organize your data

CS161 Topic #156 How do you define a Structure? We typically define structures “globally” –this means they are placed outside of the main We do this because structures are like a “specification” or a new “data type” –which means that we would want all of our functions to have access to this way to group data, and not just limit it to some function by defining it to be local

CS161 Topic #157 How do you define a Structure? Each component of a structure is called a member and is referenced by a member name (identifier). Structures differ from arrays in that members of a structure do not have to be of the same type. And, structure members are not referenced using an index.

CS161 Topic #158 How do you define members of a Structure? A structure might look like: struct storeitem { char item[20]; float cost; float price; int barcode; }; In this example, item, price, cost and barcode are member names. storeitem is the name of a new derived data type consisting of a character array, two real numbers, and an integer.

CS161 Topic #159 How do you define instances of a Structure? Once your have declared this new derived data type, you can create instances -- variables (or “objects”) which are of this type (just like we are used to): storeitem one_item; If this is done in a function, then one_item is a local variable...

CS161 Topic #1510 How do you define instances of a Structure? By saying: storeitem one_item; –From this statement, one_item is the variable (or object) –We know that we can define a product which will have the members of the item name, the cost, the price, and the bar code. –Just think of storeitem as being a type of data which consists of an array of characters, two real numbers, and an integer.

CS161 Topic #1511 How do you access members of a Structure? By saying: storeitem one_item; –To access a member of a structure variable, we use a dot (the “direct member access” operator) after the structure variable’s identifier: one_item.item //an array of chars one_item.item[0] //1st character... one_item.price //a float one_item.barcode //an int

CS161 Topic #1512 How do you access members of a Structure? We can work with these members in just the same way that we work with variables of a fundamental type: To read in a price, we can say: cin >>one_item.price; To display the description, we say: cout <<one_item.item;

CS161 Topic #1513 What operations can be performed? Just like with arrays, there are very few operations that can be performed on a complete structure We can’t read in an entire structure at one time, or write an entire structure, or use any of the arithmetic operations... We can use assignment, to do a “memberwise copy” copying each member from one struct variable to another

CS161 Topic #1514 How do you define arrays of Structures? But, for structures to be meaningful when representing an inventory –we may want to use an array of structures –where every element represents a different product in the inventory For a store of 100 items, we can then define an array of 100 structures: storeitem inventory[100];

CS161 Topic #1515 How do you define arrays of Structures? Notice, when we work with arrays of any type OTHER than an array of characters, –we don’t need to reserve one extra location –because the terminating nul doesn’t apply to arrays of structures, (or an array of ints, or floats,...) –so, we need to keep track of how many items are actually stored in this array (10, 50, 100?)

CS161 Topic #1516 How do you define arrays of Structures? So, once an array of structures is defined, we can access each element via indices: storeitem inventory[100]; int inv_count = 0; //get the first product’s info cin.get(inventory[inv_count].item, 20); cin >> inventory[inv_count].price >> inventory[inv_count].cost >> inventory[inv_count].barcode; ++inv_count;

CS161 Topic #1517 How do you pass Structures to functions? To pass a structure to a function, we must decide whether we want call by reference or call by value By reference, we can pass 1 store item: return_type function(storeitem & arg); //or an array of store items: return_type function(storeitem arg[]); By value, we can pass/return 1 store item: storeitem function(storeitem arg);

18 CS161 Introduction to Computer Science Program Questions?