Fall 2004CS-183 Dr. Mark L. Hornick 1 C++ Arrays C++ (like Java) supports the concept of collections – mechanisms to sort and manipulate many instances.

Slides:



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

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.
C Language.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Programming and Data Structure
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 20 Arrays and Strings
Programming in C Pointers and Arrays, malloc( ). 7/28/092 Dynamic memory In Java, objects are created on the heap using reference variables and the new.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
©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.
Pointers and Arrays C and Data Structures Baojian Hua
Chapter 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
Arrays and Pointers in C Alan L. Cox
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
Pointers CSE 2451 Rong Shi.
Lecture 2 Arrays, Pointers, and Structures. Objective In this chapter, we will discuss several concepts: Arrays (first-class arrays, using vector) Strings.
Chapter 8 Arrays and Strings
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
SE-1010 Dr. Mark L. Hornick 1 Some Java Classes using & calling methodsS.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
(continue) © by Pearson Education, Inc. All Rights Reserved.
CS-1030 Dr. Mark L. Hornick 1 Pointers are fun!
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
CS-1030 Dr. Mark L. Hornick 1 C++ Language Basic control statements and data types.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
CS 261 – Data Structures C Pointers Review. C is Pass By Value Pass-by-value: a copy of the argument is passed in to a parameter void foo (int a) { a.
Slide 1 Chapter 5 Arrays. Slide 2 Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays  Arrays in memory.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
System Programming Practical Session 7 C++ Memory Handling.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Engineering Computing I Chapter 5 Pointers and Arrays.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Multi-dimensional Arrays and other Array Oddities Rudra Dutta CSC Spring 2007, Section 001.
CS-1030 Dr. Mark L. Hornick 1 References & Pointers.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Object Oriented Programming Lecture 2: BallWorld.
Arrays Collections of data Winter 2004CS-1010 Dr. Mark L. Hornick 1.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
CSI 3125, Data Types, page 1 Data types Outline Primitive data types Structured data types Strings Enumerated types Arrays Records Pointers Reading assignment.
Arrays Chapter 7.
Characters and Strings
Array, Strings and Vectors
Command Line Arguments
Command line arguments
CSE 303 Concepts and Tools for Software Development
Java Review: Reference Types
Arrays and Collections
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
MSIS 655 Advanced Business Applications Programming
Arrays Chapter 7.
Pointers and dynamic objects
C Pointers Another ref:
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Fall 2004CS-183 Dr. Mark L. Hornick 1 C++ Arrays C++ (like Java) supports the concept of collections – mechanisms to sort and manipulate many instances of the same type of data. In C++ (and Java), an array is an indexed collection of data values of the same type. Arrays can represent collections of primitives or objects (e.g. int’s or SearchResult’s)

Fall 2004CS-183 Dr. Mark L. Hornick 2 Array Basics Recall that in Java, an array declaration merely creates a reference to an array, which must then be created via new int[] x; // int x[]; an alternate way x = new int[5]; // size 5 x int

Fall 2004CS-183 Dr. Mark L. Hornick 3 Java Arrays of objects Recall that a Java array of objects is actually an array of object references: Shape[] s; s = new Shape[5]; // size 5 s[0]= new Shape(Color, x1, x2, y1, y2); s shape

Fall 2004CS-183 Dr. Mark L. Hornick 4 C++ Array Basics In C++ the array size must be supplied as part of the definition; new is not used An array named x of type int can be defined in the following ways: int x[5];// array of 5 ints; uninitialized int x[5] = {3,2,6,4,1}; // initialized int x[] = {3,2,6,4,1}; // size implied x is really a pointer to the array (more later) int x

Fall 2004CS-183 Dr. Mark L. Hornick 5 Array Basics Declaration of arrays with constants is called fixed-size array declaration. Java is not limited to fixed-size array declaration. // Java array int n=12; int[] x;// x is an array name x = new int[n]; // size determined from variable n C++ is limited to fixed-size array declaration More restrictive than Java Better alternatives (like vector)

Fall 2004CS-183 Dr. Mark L. Hornick 6 C++ array limitations A Java array has a public constant length for the size of an array. for (i=0; i<x.length; i++)… In C++, there are no methods (e.g. length()) for determining the size of an array Shape x[5];// array of 5 shapes All C++ does is allocate a contiguous block of memory for the adjacent objects You have to know how big it is shape x

Fall 2004CS-183 Dr. Mark L. Hornick 7 Array indexing in C++ An indexed expression is used to refer to the individual values of the collection. C++ arrays use zero-based indexing (like Java) x[2]; // 3rd element There are no safeguards to prevent you from indexing to a location outside the bounds of the array. x[5]; // 6th element? shape x

Fall 2004CS-183 Dr. Mark L. Hornick 8 Pointers and Arrays The C++ variable representing an array maintains the location of the beginning of the memory block occupied by the array x (with no index) simply refers to the beginning of the array x is really just a pointer (to the first element of the array) int x[10]; // x points to the first of 10 elements int* ptr1 = x; // No type mismatch! ptr1 = &x[0]; // x contains the address of the 1 st element, // meaning x = &x[0]

Fall 2004CS-183 Dr. Mark L. Hornick 9 Pointers and Arrays Subscripting and dereferencing are similar Both convert a pointer to an object: int y = x[0]; // get first element y = *x; // same int* p = &x[0]; // or p = x y = *p; // get first element y = x[1]; // get second element y = *(x+1); // same y = *(p+1); // also the same

Fall 2004CS-183 Dr. Mark L. Hornick 10 Arrays of pointers int* py[5]; // array of 5 pointers // py is a pointer to an array of pointers Int* py 5 y

Fall 2004CS-183 Dr. Mark L. Hornick 11 char Arrays C/C++ have a special syntax for handling arrays of char’s An array of char’s is a C-Style string char msg[] = {‘H’,’e’,’l’,’l’,’o’,’\0’}; char msg[] = “Hello”; // more convenient Stored as: ‘H’,’e’,’l’,’l’,’o’,’\0’ char* msg = “Hello”; Last entry is ‘\0’, the null character

Fall 2004CS-183 Dr. Mark L. Hornick 12 Array example

Fall 2004CS-183 Dr. Mark L. Hornick 13 Passing arrays as arguments Since an array is just a pointer, you pass it as an argument as if you were passing a pointer Example

Fall 2004CS-183 Dr. Mark L. Hornick 14 Multi-dimensional Arrays An 2-dimensional array named x of type int can be defined in the following ways: int x[2][2];// 2x2 array of 4 ints int x[2][2] = {{3,2},{6,4}}; // initialized

Fall 2004CS-183 Dr. Mark L. Hornick 15 Program Arguments All C++ programs can have arguments By common convention In a console-mode program They come from the command line They are always passed to main int main(int argc, char* argv[]) Number of wordsArray of C-style strings

Fall 2004CS-183 Dr. Mark L. Hornick 16 Program Argument Example Command line myprog data.txt results.txt Arguments to main argc = 3 argv[0] = “myprog” argv[1] = “data.txt” argv[2] = “results.txt” m y p r o g \0 d a t a. t x t \0 r e s u l t s. t x t \0 argv