Homework due Test the random number generator Create a 1D array of n ints Fill the array with random numbers between 0 and 100 Compute and report the average.

Slides:



Advertisements
Similar presentations
Chapter 6 Data Types
Advertisements

Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Writing a Good Program 6. Pointers and Arrays
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
Lecture 2 Pointers Pointers with Arrays Dynamic Memory Allocation.
CS0007: Introduction to Computer Programming Introduction to Arrays.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
2 dimensional arrays Steven Wood ©2005. Arrays dimensions Java allows arrays with many subscripts 2-D examples Chess board Excel spreadsheet.
Microsoft Visual C++.NET Chapter 61 Memory Management.
Lecture 4. RAM Model, Space and Time Complexity
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
1 Pointers Arrays have a disadvantage: Their size must be known at compile time. We would like the capability to allocate an array-like object of any needed.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
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.
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.
Looping and Counting Lecture 3 Hartmut Kaiser
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.
Arrays Arrays in C++ An array is a data structure which allows a collective name to be given to a group of elements which all have.
POINTERS.
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
Arrays Dr. Jose Annunziato. Arrays Up to this point we have been working with individual primitive data types Arrays allow working with multiple instances.
Processing Arrays Lesson 9 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
System Programming Practical Session 7 C++ Memory Handling.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Assembly - Arrays תרגול 7 מערכים.
Engineering Computing I Chapter 5 Pointers and Arrays.
17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur1 Arrays and Pointers Lecture 17 18/2/2002.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial.
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
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.
Windows Programming Lecture 03. Pointers and Arrays.
EGR 2261 Unit 11 Pointers and Dynamic Variables
User-Written Functions
EGR 2261 Unit 10 Two-dimensional Arrays
COSC 220 Computer Science II
Java Review: Reference Types
void Pointers Lesson xx
Dynamic Memory Allocation
One-Dimensional Array Introduction Lesson xx
Pointers.
Pointers and Arrays Beyond Chapter 16
Data Structures & Algorithms
Assignment due Write a program the generates a random integer expression, presents the two operands and the result to the user, and asks the user to tell.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Homework due Test the random number generator Create a 1D array of n ints Fill the array with random numbers between 0 and 100 Compute and report the average of the array contents Do this for n = 10, 100, 1000, 10000,

Homework due Create a 2D array of m x n ints Fill the array with random numbers between 0 and 100 Compute and report the average of the array contents Do this for all combinations of m = {1, 10, 100} and n = {1, 10, 100, 1000}

Homework – special instructions Do not use any hard coded values or constants when calculating loop indices –Use the techniques discussed for computing the number of elements in rows/columns of an array Due Thursday, next class meeting – I’ll look at your results in class

C++ Arrays – Part II

Run time allocation Allocate an array of 20 ints int array[20]; –Allocates an array of 20 elements in main memory at compile time But what if we don’t know how big the array should be when we are compiling the program? –Java has no issues with this since everything is run time allocate

Run time allocation The dreaded “P” word…Pointers! In C++ a pointer is nothing more than a variable that holds an address in memory –Whereas most variables are themselves addresses in memory and hold data It’s often referred to as an indirect address

“Regular” (non-pointer) variables int x = 10; int y = 20; int z = 30; int a[3] = {0}; x y z a[0] a[1] a[2]

Pointer variables int *px; int *py; int *pz; int *pa; px py pz pa ? Notes int* x; is the same as int *x; int* x, y; declares 1 pointer (*x) and 1 non-pointer (y) variable

Pointers – what do you do with them? int *px; int *py; int *pz; int *pa; int x = 10; int y = 20; int z = 30; int a[3] = {0}; px = &x; py = &y; pz = &z; pa = a; x y z a[0] a[1] a[2] x y z a px py pz pa You can use them to refer to (point to) other variables The & is called the “address-of” operator and returns the address of the variable it is operating on Note that the array name is already and address and needs no &

Pointing to other variables *px = 27; *py = 28; *pz = 29; pa[0] = 30; pa[1] = 31; pa[2] = 32; These assignments will change the contents of the variables to whom they point x is now 27 y is now 28 x is now 29 a now holds {30, 31, 32} The * is called the “indirection” operator and refers to the contents of the variable it is operating on Note that the array index operators [ and ] perform a similar task on array type variables

Why? Typically, you won’t use pointers to access individual (primitive) variables You might use pointers to access arrays –At one point in time it used to be faster to do so rather than use indexing – not clear if this is still true –You could use an array of pointers to create a “ragged edged” array (like you can do in Java) The real value of pointers is in dynamic, run time memory allocation

Array access though pointers int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} for (int i = 0; i < 10; ++i) { std::cout << a[i] << std::endl; } int *pa = a; for (int i = 0; i < 10; ++i) { std::cout << *pa << std::endl; ++pa; } The tricks –Pointer arithmetic (like ++pa) is smart enough to know how much to add to the pointer (address) to get to the next variable of the appropriate type e.g. if pointing to an int it adds 4, if pointing to a char it adds 1, if pointing to a double it adds 8 Similar for things like pa += 2 (adds 8 for an int, 2 for a char, 16 for a double) –Be sure to initialize the pointer properly –Know how far you can go before overrunning the end of the array

Dynamic memory allocation It’s really quite simple int x = 5; int *pa = new int[x]; for (int i = 0; i < x; ++i) { *(pa + i) = i; } delete [] pa; Note that you have to keep track of the size – sizeof(pa) / sizeof(pa[0]) will not work

What not to do int x = 5; int *pa = new int[x]; for (int i = 0; i < x; ++i) { *pa = i; ++pa; } delete [] pa; Why is this a very, very, very bad thing to do?

To fix it int x = 5; int *pa = new int[x]; for (int i = 0; i < x; ++i) { *(pa + i) = i; } delete [] pa;

Dynamic multi-dimensional arrays Basically, you need to dynamically allocate an array of pointers The method shown in the book is (in my opinion) hooky at best Here’s how I do it…

Dynamic multi-dimensional arrays int rows = 4; int cols = 5; // -- requires a pointer to a pointer int **ma; // -- requires two separate allocations // and some pointer assignments ma = new int*[rows]; ma[0] = new int[rows * cols]; for (int i = 1; i < rows; ++i) { ma[i] = ma[i - 1] + cols; }

Dynamic multi-dimensional arrays // -- now you can access it as a normal 2D array for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { ma[i][j] = i * cols + j; } for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { std::cout << ma[i][j] << "\t"; } std::cout << std::endl; }

Dynamic multi-dimensional arrays // -- you have to delete both allocations in // reverse order delete [] ma[0]; delete [] ma;

Homework Same as the last assignment but this time use dynamically allocated arrays and ask the user how big they should be –i.e. you only need to write and compile 2 programs, 1 for the 1D case and 1 for the 2D case –All the different run sizes will be handled at run time based on user inputs Due next class period