Arrays Low level collections.

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

1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
©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.
1 CS 201 Passing Function as Parameter & Array Debzani Deb.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
CSC 142 J 1 CSC 142 Arrays [Reading: chapter 10].
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
ARRAYS.
Memory Management.
Object Lifetime and Pointers
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Arrays.
Array in C# Array in C# RIHS Arshad Khan
1-d Arrays.
Computer Organization and Design Pointers, Arrays and Strings in C
Chapter 6 Arrays in C++ 2nd Semester King Saud University
EGR 2261 Unit 10 Two-dimensional Arrays
EGR 2261 Unit 9 One-dimensional Arrays
Pointers & Arrays.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Computer Programming BCT 1113
Array An “average.cpp” program
Chapter 7 Part 1 Edited by JJ Shepherd
Chapter 8 Arrays Objectives
C++ Arrays.
Arrays Part 1 Topic 19 - Stan Kelly-Bootle
Arrays … The Sequel Applications and Extensions
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, For loop While loop Do while loop
Arrays An Array is an ordered collection of variables
Previous Lecture Review
7 Arrays.
Arrays.
Chapter 8 Arrays Objectives
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the.
Arrays .
Dynamic Memory A whole heap of fun….
Topics discussed in this section:
Arrays ICS2O.
Dynamic Memory A whole heap of fun….
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Chapter 16 Pointers and Arrays
Review of Everything Arrays
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Building Java Programs
CSC 142 Arrays [Reading: chapter 12].
Chapter 8 Arrays Objectives
Arrays in Java.
Dynamic Memory A whole heap of fun….
Suggested self-checks: Section 7.11 #1-11
Pointers & Arrays.
The Stack.
Consider Write a program that prompts a user to enter the number of students and then, their names and grades. The program will then outputs the average.
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.
C++ Array 1.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Arrays Low level collections

Motivation I want to: Read in 30 test scores Print out grades based on a curve 15%+ over average : A 5%+ over average : B +/- 5% from average : C 5%+ below average : D 15%+ below average : F

Motivation Ways to solve Read twice – yuck, why?

Motivation Ways to solve 30 variables – yuck, why?

Arrays 10 5 8 9 6 7 4 Array : a collection of items Fixed size Must know at compile time Always the same type 10 5 8 9 6 7 4

Arrays 10 9 Array : a collection of items Fixed size Must know at compile time Always the same type Each element has an index, starting from 0 1 2 3 4 5 6 7 8 10 9

Array Syntax Declaring an array: type name[size]; int num[5]; Make 5 consecutive int storage boxes numbered 0-4

Array Syntax Declaring an array: type name[size]; Yes: No: size: must be a constant expression that evaluates to an integer Not a variable!!! Yes: No:

Background: The Stack C++ allocates variables on a stack void foo(int q) { if(true) { char c = 'a'; } } int main() { int x = 10; double y = 1.2; foo(5); int z = 5; } Address Identifier Value 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

The Stack C++ allocates variables on a stack void foo(int q) { if(true) { char c = 'a'; } } int main() { int x = 10; double y = 1.2; foo(5); int z = 5; } Address Identifier Value 16 15 14 13 12 11 10 9 8 7 6 5 4 3 X 2 1

The Stack C++ allocates variables on a stack void foo(int q) { if(true) { char c = 'a'; } } int main() { int x = 10; double y = 1.2; foo(5); int z = 5; } Address Identifier Value 16 15 14 13 12 11 Y 1.2 10 9 8 7 6 5 4 3 X 2 1

The Stack C++ allocates variables on a stack void foo(int q) { if(true) { char c = 'a'; } } int main() { int x = 10; double y = 1.2; foo(5); int z = 5; } Address Identifier Value 16 15 q 5 14 13 12 11 Y 1.2 10 9 8 7 6 4 3 X 2 1

The Stack C++ allocates variables on a stack void foo(int q) { if(true) { char c = 'a'; } } int main() { int x = 10; double y = 1.2; foo(5); int z = 5; } Address Identifier Value 16 c a 15 q 5 14 13 12 11 Y 1.2 10 9 8 7 6 4 3 X 2 1

The Stack C++ allocates variables on a stack void foo(int q) { if(true) { char c = 'a'; } } int main() { int x = 10; double y = 1.2; foo(5); int z = 5; } Address Identifier Value 16 15 q 5 14 13 12 11 Y 1.2 10 9 8 7 6 4 3 X 2 1

The Stack C++ allocates variables on a stack void foo(int q) { if(true) { char c = 'a'; } } int main() { int x = 10; double y = 1.2; foo(5); int z = 5; } Address Identifier Value 16 15 14 13 12 11 Y 1.2 10 9 8 7 6 5 4 3 X 2 1

The Stack C++ allocates variables on a stack void foo(int q) { if(true) { char c = 'a'; } } int main() { int x = 10; double y = 1.2; foo(5); int z = 5; } Address Identifier Value 16 15 z 5 14 13 12 11 Y 1.2 10 9 8 7 6 4 3 X 2 1

The Stack C++ allocates variables on a stack void foo(int q) { if(true) { char c = 'a'; } } int main() { int x = 10; double y = 1.2; foo(5); int z = 5; } Address Identifier Value 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

Stack Thoughts Works well to efficiently reuse space Requirements Need to know sizes Can't change the size once something is there

Array Implementation Arrays implemented with sequential blocks of memory cells Address Identifier Value 16 15 14 13 12 11 quiz[2] 8 10 9 7 quiz[1] 5 6 4 3 quiz[0] 2 1 1 2 10 5 8

g++ g++ has an extension for variable length arrays Turn off "Enforce standard C++" This works: DO NOT USE

Accessing Elements Subscript operator [ ] used to access elements list[5] = 34; //set 6th element to 34 Index can be any expression: //set item at index 3 to 63 //set element at (2i-3) to 58

Accessing Elements Elements can be read from or written to:

No Bounds Checking Nothing sanity checks your indexes:

No Bounds Checking Nothing sanity checks your indexes: 24 23 x 10 22 Address Identifier Value 24 23 x 10 22 21 20 19 nums[4] 18 17 16 … 3 nums[0] 2 1

No Bounds Checking Nothing sanity checks your indexes: 24 23 x 10 22 Address Identifier Value 24 23 x 10 22 21 20 19 nums[4] 18 17 16 … 3 nums[0] 2 1

No Bounds Checking No sanity checks of indexes: May get weird results May crash

Array Initialization Uninitialized arrays start with mystery garbage Initialization list can be used to set values: double myList[4] = {1.9, 2.9, 3.4, 3.5}; Same as

Array Initialization Uninitialized arrays start with mystery garbage Initialization list can be used to set values: double myList[4] = {1.9, 2.9, 3.4, 3.5}; Don't need to specify size if initializing: double myList[] = {1.9, 2.9, 3.4, 3.5};

Array Initialization Insufficient initializers? Use what is there, set rest to 0 Quick initialize whole array to 0:

No Aggregate Operations Operators do not work on entire arrays No = to whole array:

No Aggregate Operations Operators do not work on entire arrays Printing gives you a memory address

Base Address Array variable actually stores base address Printing shows you base address:

Base Address Array variable actually stores base address Can be used to efficiently find any element: elementLocation = (index * elementSize) + baseAddress Subscript does this recipe: nums[3]  Start at memory location given by nums array. Then go forward 3 units of size.

No Aggregate Operations Operators do not work on entire arrays Or at least don't do what you think < > = don't compare values, compare addresses

Work with Elements Moral: Create arrays, work with elements

Processing Arrays Arrays generally processed with counting loop: Count from 0 to size – 1 i is the current index array[i] is the current element

Processing Examples Initialize all values to 10: Count from 0 to size – 1 array[i] is the current element

Processing Examples Copy an array Count from 0 to size – 1 array[i] is the current element

Processing Examples Total up an array Need variable for total Count from 0 to size – 1 array[i] is the current element

Processing Examples 5 6 Find smallest 1 2 3 4 Assume first is smallest Count from i = 1 to size – 1 If current item is smaller than smallest, change smallest Smallest 4 1 2 3 4 5 6

Processing Examples 5 6 Find smallest 1 2 3 4 Assume first is smallest Count from i = 1 to size – 1 If current item is smaller than smallest, change smallest Smallest 4 1 2 3 4 5 6

Processing Examples 5 6 Find smallest 1 2 3 4 Assume first is smallest Count from i = 1 to size – 1 If current item is smaller than smallest, change smallest Smallest 3 1 2 3 4 5 6

Processing Examples 5 6 Find smallest 1 2 3 4 Assume first is smallest Count from i = 1 to size – 1 If current item is smaller than smallest, change smallest Smallest 2 1 2 3 4 5 6

Processing Examples 5 6 Find smallest 1 2 3 4 Assume first is smallest Count from i = 1 to size – 1 If current item is smaller than smallest, change smallest Smallest 2 1 2 3 4 5 6

Processing Examples Find smallest Assume first is smallest Count from 1 to size – 1 array[i] is the current element

Processing Examples 9 10 Check if sorted 1 2 3 4 5 6 7 8 Assume it is sorted Count from 0 to size – 2 array[i] is current, array[i + 1] is next 1 2 3 4 5 6 7 8 9 10

Processing Examples 9 10 Check if sorted 1 2 3 4 5 6 7 8 Assume it is sorted Count from 0 to size – 2 array[i] is current, array[i + 1] is next 1 2 3 4 5 6 7 8 9 10

Processing Examples 9 10 Check if sorted 1 2 3 4 5 6 7 8 Assume it is sorted Count from 0 to size – 2 array[i] is current, array[i + 1] is next Oooops – we found a problem!!! 1 2 3 4 5 6 7 8 9 10

Processing Examples 9 12 15 18 19 20 Check if sorted 1 2 3 4 5 6 7 8 Assume it is sorted Count from 0 to size – 2 – stop early!!! array[i] is current, array[i + 1] is next Don't want last element to become "current " No "next" 1 2 3 4 5 6 7 8 9 12 15 18 19 20

Processing Examples Check if sorted Assume it is sorted Count from 0 to size – 2 stop early!! array[i] is current, array[i + 1] is next