1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Arrays.
CSC 270 – Survey of Programming Languages C Lecture 6 – Pointers and Dynamic Arrays Modified from Dr. Siegfried.
Programming and Data Structure
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
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.
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
C arrays (Reek, Ch. 8) 1CS 3090: Safety Critical Programming in C.
Pointers Applications
VB .NET Programming Fundamentals
C programming---Arrays scalar: capable of holding a single data item aggregate variables: capable of holding a collections of values. Two kinds of aggregates.
Java Unit 9: Arrays Declaring and Processing Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Chapter 8 Arrays and Strings
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
JAVA 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
21-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Multidimensional Arrays Lecture 20 4/3/2002.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
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.
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Lecture 3 Introduction to Computer Programming CUIT A.M. Gamundani Presentation Layout from Lecture 1 Background.
Chapter 7 Arrays. Introductions Declare 1 variable to store a test score of 1 student. int score; Declare 2 variables to store a test score of 2 students.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
General comments [1]  array is a fixed sized group in which the elements are of the same type  The general form of array's definition is as follows:
Arrays and Pointers.
Assembly - Arrays תרגול 7 מערכים.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Multi-dimensional Array 1 Multi-dimensional array refers to an array with more than one index. It is a logical representation. On physical storage, the.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Arrays Chapter 12. One-Dimensional Arrays If you wanted to read in 1000 ints and print them in reverse order, it would take a program that’s over 3000.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Chapter 12: Pointers and Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Windows Programming Lecture 03. Pointers and Arrays.
Hassan Khosravi / Geoffrey Tien
EKT150 : Computer Programming
Lecture 18 Arrays and Pointer Arithmetic
Pointers and Arrays Chapter 12
Dr Tripty Singh Arrays.
ENERGY 211 / CME 211 Lecture 11 October 15, 2008.
Introduction to Pointers
Presentation transcript:

1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008

2 Arrays An array is a contiguous block of memory allocated to a fixed number of objects of a given type To declare: type name [ size ]; where size is a positive integer constant Unlike MATLAB, arrays use zero-based indexing: x[0] refers to the first element, not x[1] !

3 Array Storage Example: let x be an array of n double- precision floating-point numbers, which are 8 bytes each x[0]x[1]x[2]…x[n-1] 8 bytes n elements base address

4 Behind Array Indices The location of each element in memory depends on: –the base address of the array –the index into the array –the size, in bytes, of each element Location of A[i] is base( A ) + i*n, where n is the size of each element We say i is an offset, since it indicates “distance” from the base address

5 2-D Arrays Useful for representing matrices of variables of the same type 2D arrays: type name [ size 1 ][ size 2 ]; First index refers to row, second index to column, as usual Elements are stored in row-major order: A[0][0], A[0][1], A[0][2], etc.

6 Indexing Multi-D Arrays Because all arrays are contiguous, any array can be treated as one- dimensional You can either declare, for example: –double A[m][n]; and access the ( i, j ) element by A[i][j] –double B[m*n]; and access the ( i, j ) element by B[i*n+j]

7 2-D Array Storage Example: let A be a 3-by-4 array of integers, 4 bytes each Note row-major ordering: all elements in a row are stored consecutively A[2][3] A[2][2] A[2][1] A[2][0] A[1][3] A[1][2] A[1][1] A[1][0] A[0][3] A[0][2] A[0][1] A[0][0] 4 bytes base address

8 Inefficient? Note that even in a one-dimensional array, computing the address of an element requires at least one multiplication If we are accessing elements sequentially, shouldn’t we be able to simply increment some address by the size of each element? We can…but not yet!

9 Array Don'ts Don't assign a value to an array variable; its base address is fixed Don't try to access or set an array element that is out-of-bounds; compiler will let you, but bad things will happen! Don't pass an array to a function without also passing an argument indicating its size; the function won't know its size because it only has its base address!

10 Initializing Arrays To initialize array, use { } to enclose comma-separated list of elements Don’t need to specify size for 1-D arrays For multi-D case, need to specify all dimensions except the first Examples: –int x[] = { 1, 2, 3, 4 }; –double A[][2] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 }; for 3-by-2 matrix

11 The Address Operator The & operator, applied to a variable, returns the address of that variable within the program’s memory space The type of an address is called “pointer to” the type of the original variable The address of the i th element of an array x is given by &x[i] We will discuss this operator further when we get to pointers

12 The const qualifier Variable declared const can’t be modified; value set at declaration Declaring variables as const, when applicable, is recommended because it helps compiler optimize code When passing arrays to functions, use const in function definition if they are meant to be read-only We will discuss const in greater detail after seeing pointers

13 The return statement A return statement is used to specify the result of a call to a function Form: return expr ; where expr should be of the same type as the function A function can only return one value A function of type void cannot return any value The returned value can be assigned to a variable in the calling function

14 Basic for statements A for loop is useful for iterating over all elements of an array in sequence Form: (for array of length n ) –for (i=0;i<n;i++) stmt –backwards: for (i=n-1;i>=0;i--) stmt In stmt, i is used to access array elements i is incremented ( ++ ) or decremented ( -- ) between iterations We will revisit for statements later

15 Next Time All Kinds of Expressions –Arithmetic, Logical, Relational, etc. Evaluation Rules Type Conversions