16/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Methods to manipulate a collection of values under one name. Arrays: Homogenous.

Slides:



Advertisements
Similar presentations
Chapter 17 vector and Free Store Bjarne Stroustrup
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
PHYS707: Special Topics C++ Lectures Lecture 3. Summary of Today’s lecture: 1.Functions (call-by-referencing) 2.Arrays 3.Pointers 4.More Arrays! 5.More.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Chapter 17 vector and Free Store John Keyser’s Modifications of Slides By Bjarne Stroustrup
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Structure.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Informática II Prof. Dr. Gustavo Patiño MJ
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Chapter 10.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
More Arrays Arrays and classes Multi-dimensional Arrays Dynamic arrays.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Chapter 9: Arrays and Strings
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Lecture 2 Arrays, Pointers, and Structures. Objective In this chapter, we will discuss several concepts: Arrays (first-class arrays, using vector) Strings.
February 11, 2005 More Pointers Dynamic Memory Allocation.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Basic Semantics Associating meaning with language entities.
Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007.
GUIDED BY- A.S.MODI MADE BY- 1. SHWETA ALWANI 2. PRIYANKA.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
1 Data Structures CSCI 132, Spring 2014 Lecture 10 Dynamic Memory and Pointers.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
Dynamic memory allocation and Pointers Lecture 4.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Chapter 7 Pointers: Java does not have pointers. Used for dynamic memory allocation.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 11 Scott Marino.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
Objective: Students will be able to: Declare and use variables Input integers.
CPS120: Introduction to Computer Science Lecture 15A Structures.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
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.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
DATA STRUCTURE & ALGORITHMS Pointers & Structure.
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.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
11 Introduction to Object Oriented Programming (Continued) Cats.
Chapter 2. Variable and Data type
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Slide 1 of 36. Attributes Associated with Every Variable Data type Data type Actual value stored in variable Actual value stored in variable Address of.
P OINTERS A pointer is an address All data is stored in memory in some location that is indexed with an address Can refer to variables by name or by memory.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
Sudeshna Sarkar, CSE, IIT Kharagpur1 Structure and list processing Lecture
CPS120 Introduction to Computer Science Exam Review Lecture 18.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
Lecture 01d: C++ review Topics: functions scope / lifetime preprocessor directives header files C structures ("simple classes")
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
Pointers Revisited What is variable address, name, value?
CS 1430: Programming in C++.
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Pointers, Dynamic Data, and Reference Types
Chapter 12 Pointers and Memory Management
Dynamic Memory.
Submitted By : Veenu Saini Lecturer (IT)
COP 3330 Object-oriented Programming in C++
ENERGY 211 / CME 211 Lecture 10 October 13, 2008.
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

16/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Methods to manipulate a collection of values under one name. Arrays: Homogenous data Homogenous data Variable quantity of data items all of the same type Variable quantity of data items all of the same typeStructures: Heterogeneous data Heterogeneous data Fixed quantity of data values of different types Fixed quantity of data values of different types

26/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Arrays Declaration: [int-qty] [int-qty]Access:<var-name>[subscript]Subscript: Integer Integer In the range 0 to n-1, where n is the number of elements In the range 0 to n-1, where n is the number of elements

36/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Structures Definition: struct // typically in global area { …. /* composed of data types and element names */ …. /* composed of data types and element names */};Declaration: Access:<var-name>.<element-expression>

46/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Array Examples * int counts[100]; … counts[0] = 0; double speeds[2000]; … speeds[1999] = 1.23; string names[500]; … cout << “Name: “ << names[10];

56/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Structures Example Structures: struct employee struct employee{ string name; float annual_salary; int dept_num; }; employee boss; employee boss; boss.name = “Bill”; boss.name = “Bill”; cout << “Annual Salary is “ << boss.annual_salary << “\n”; cout << “Annual Salary is “ << boss.annual_salary << “\n”;

66/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Nesting An array may be an array of arrays or an array of structures. A structure may have arrays as members or other structures as members. These arrays or structures may also have arrays or structures as members, and so forth to any arbitrary depth.

76/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Nesting Example struct class { string time; string place; string students[30]; // structure w/ array }; class schedule[5]; // array of structures schedule[0].place = “Robinson 310”; schedule[0].students[0] = “Bill”;

86/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Arrays and For Loops For loops are typically used to process arrays. Typically when using arrays, the number of elements that actually contain valid data is saved in a variable. for (i=0; i<NUM_ELEMENTS; ++i) counts[i] = 0; for (i=0; i<n; ++i) area[i] = base[i] * height[i];

96/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Sentinals int SENTINEL = -1; Sentinels may also be used: i = 0; while (speeds[i] != SENTINEL) cout << speed[i] << “\n”; ++ i; }

106/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Terminology Scalars: a single data value Vectors: a one-dimensional array Matrices: a two-dimensional array

116/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Strings A string is a array of characters. A string literal is delimited by double-quotation marks. Examples: “Computer” “Computer” “special characters in it: ” “special characters in it: ” “one” “one” “1” “1” “” “” NOT strings: ‘a’, ‘1’, 1, one NOT strings: ‘a’, ‘1’, 1, one

126/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Another method to do input getline(cin, a_string, ‘\n’); This will accept all input up to a newline character. It will store the input (NOT the newline) in a_string. There are functions to do character-at-a- time input, as well as other special input functions (see lecture on streams and files). In practice, these functions sometimes do not mix well with cin, with erratic results.

136/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Using Strings in C++ #include #include Declaring strings: string name; string name; string account_number; string account_number; Using strings: Name = “Bob”; Name = “Bob”; cin >> account_number; cin >> account_number; whole_name = last_name + “, “ + first_name; // Concatenation whole_name = last_name + “, “ + first_name; // Concatenation Name_length = name.length(); Name_length = name.length();

146/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Pointers A pointer is a variable that contains the address of another variable, that is, a memory address. Definition: * Definition: * Access: for the pointer itself; * for the variable pointed to (“de-referencing”).

156/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Dynamic Memory Three areas of memory: Global: defined outside any function (allocated once) Global: defined outside any function (allocated once) Local: defined inside a function (allocated on the system stack) Local: defined inside a function (allocated on the system stack) Dynamic (also known as the heap) Dynamic (also known as the heap) Accessed through operators new and delete To request a section of memory: = new = new To access that memory: use operators [], *, -> To return that memory: delete To return that memory: delete

166/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Memory Examples #include #include using namespace std; int g; // global: visible from here to end of file void main() { float l; // local: visible from here to end of block double* h = new double; // heap allocation delete h; // heap de-allocation int* weights = new int[num_weights]; // delete[] }

176/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Reading Chapter 9 Chapter 11, Section 1-2 Chapter 13, Sections 1-2