Arrays, Pointers and Structures CS-240 Dick Steflik.

Slides:



Advertisements
Similar presentations
Chapter 18 Vectors and Arrays
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Brown Bag #3 Return of the C++. Topics  Common C++ “Gotchas”  Polymorphism  Best Practices  Useful Titbits.
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Vectors, lists and queues
Chapter 18 Vectors and Arrays John Keyser’s Modification of Slides by Bjarne Stroustrup
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
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
CMSC 202, Version 2/02 1 Operator Overloading Strong Suggestion: Go over the Array class example in Section 8.8 of your text. (You may ignore the Array.
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Computer programming 1 Multidimensional Arrays, and STL containers: vectors and maps.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Value Semantics CS-240 & CS-341 Dick Steflik. Value Semantics determine how the value(s) of one object are copied to another object in C++ the value semantics.
Session 1 CS-240 Data Structures Binghamton University Dick Steflik.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
Lecture 2 Arrays, Pointers, and Structures. Objective In this chapter, we will discuss several concepts: Arrays (first-class arrays, using vector) Strings.
By – Tanvir Alam.  This tutorial offers several things.  You’ll see some neat features of the language.  You’ll learn the right things to google. 
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
Recap Visual Perception and Data Visualization Types of Information Display Examples of Diagrams used for Data Display Planning Requirement for Data Visualization.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Introduction to Programming Lecture 11. ARRAYS They are special kind of data type They are special kind of data type They are like data structures in.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
Operator Overloading. Binary operators Unary operators Conversion Operators –Proxy Classes bitset example Special operators –Indexing –Pre-post increment/decrement.
Arrays, Vectors, and Strings Allocation and referencing.
Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 5 – September 4 th, 2001.
Lecture 7.  There are 2 types of libraries used by standard C++ The C standard library (math.h) and C++ The C++ standard template library  Allows us.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
Data Structures in C++ Pointers & Dynamic Arrays Shinta P.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
17-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
14-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Student Book An Introduction
Pointers Psst… over there.
Pointers Psst… over there.
Vectors.
Built-In (a.k.a. Native) Types in C++
CMSC 202 Lesson 22 Templates I.
7. 11 Introduction to C++ Standard Library Class Template vector (Cont
Indirection.
Templates I CMSC 202.
Standard Template Library
Today’s Objectives 28-Jun-2006 Announcements
Class rational part2.
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
A dictionary lookup mechanism
Data Structures & Programming
Presentation transcript:

Arrays, Pointers and Structures CS-240 Dick Steflik

Pointers allow us to access something indirectly –index of a book points to some topic in the body of a book –street addresses in a telephone book point to where some lives in the community –a forwarding address points to an address of where some one can be reached (pointer to a pointer)

Arrays and Structures Arrays - ordered collections of objects, all of the same type – int abc[4]; Structure - a collection of objects of dissimilar types –struct MyType { int abc ; double def;}

First Class Objects First Class Object –can be manipulated in all of the “usual ways” –are usually “safer than Second Class Objects –preserve “value semantics” obj2 = obj1 (assignment) objtype a ; objtype b(a) (copy constructor) a =+ b (overloaded assignment operators) –usually implemented as a class –can have bounds checking

Second Class Objects Primitive types (arrays and structs) don’t preserve value semantics are not as safe as First Class Objects –no bounds checking on array indicies

Standard template Library a standard set of templates and class templates that provide a set of First Class types was an “extra” in older compilers built into current generation of compilers

vector vector is a template class that provides a First Class version of the array primitive vector a(3); –allocates a vector containing 3 integers vector methods –size() - return # of elements in the vector –resize(n) - change the size of the vector to n elements

#include using namespace std; int main() { const int DIFFERENT_NUMBERS = 100; int totalNumbers; cout << “How many numbers?”; cin >> totalNumbers vector numbers(DIFFERENT_NUMBERS + 1); for (int I=0 ; I < numbers.size() ; 1++) numbers[I] = 0; for (int j=0 ; j < totalNumbers ; j++) numbers[rand[] % DIFFERENT_NUMBERS + 1] ++; for (int k=1 ; k < = DIFFERENT_NUMBERS ; k++) cout << k << “ occurs “ << numbers[k] << “times” << “\n”; return 0 }