CIS 130 Numeric Arrays Chapter 10. What is an array? Collection of data storage locations –stored adjacently in memory All the pieces of data share a.

Slides:



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

Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
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.
Introduction to C Programming
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Arrays Starting Out with C++ Early Objects Seventh Edition by.
Chapter 8: Arrays.
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.
Multidimensional Arrays Arrays with more than one dimension are called multidimensional arrays. Human cannot easily visualize more than three dimension.
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.
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.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Multiple-Subscripted Array
Chapter 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
Arrays.
VB .NET Programming Fundamentals
CS0007: Introduction to Computer Programming Introduction to Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Chapter 8 Arrays and Strings
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Chapter 8: Arrays.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
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.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
Pointers *, &, array similarities, functions, sizeof.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Lecture 7: Arrays Tami Meredith. Roadmap Variables Arrays Array indices & Using arrays Array size versus length Two dimensional arrays Sorting an array.
Arrays.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
CSCI 130 More on Arrays. Multi-dimensional Arrays Multi - Dimensional arrays: –have more than one subscript –can be directly initialized –can be initialized.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
Arrays An array is an indexed data structure which is used to store data elements of the same data type. An array is an indexed data structure which is.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
Arrays. C++ Style Data Structures: Arrays(1) An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
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.
Windows Programming Lecture 03. Pointers and Arrays.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
EGR 2261 Unit 11 Pointers and Dynamic Variables
EGR 2261 Unit 10 Two-dimensional Arrays
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Numeric Arrays Numeric Arrays Chapter 4.
7 Arrays.
EKT150 : Computer Programming
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
MSIS 655 Advanced Business Applications Programming
Arrays in Java.
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
C++ Array 1.
Variables and Constants
Presentation transcript:

CIS 130 Numeric Arrays Chapter 10

What is an array? Collection of data storage locations –stored adjacently in memory All the pieces of data share a common name All the pieces of data are the same type Each piece of data is called an ‘array element’

Why use an array? Related information can be stored in a single place Scenario 1: For company payroll, the total payroll for the 12 months can be stored in 12 different variables. Better programming would be to store them in one variable (an array) so all the related data is grouped together.

Why use an array? Scenario 2: A user in a company selects part numbers from a list box with 1000 possible selections. All of their selections (up to 1000) can be stored in an array. It is unknown ahead of time how many will be selected, so this would be harder to store in individual variables.

How do arrays work? Assume we need a 12 element array to hold the total payroll (one element for each month). Declaring the array: float monthlyPayroll[12] Each of the 12 elements in monthlyPayroll() is a float, and can be treated like any float variable

How do arrays work? When array is declared, compiler sets aside enough memory to hold the entire array Elements are stored in adjacent memory locations int x[5] causes the compiler to set aside memory:

Accessing array elements Elements are accessed by the array name followed by brackets with an integer x[1] = 3; x[3] = x[1] * 3; a = 100; b = 2 x[b] = a;

Accessing array elements The first element an array is always subscript 0 –the n th element in an array is subscript (n-1) Store the value 25 in the first element of x: x[0] = 25; Store the value 23 in the 102 element of a 200 element array called q: q[101] = 23;

Accessing array elements Some programmers like the first element in the array to correspond to subscript #1 –Declare the array 1 element too big –Ignore the 0 th subscript

Declaring arrays Can use a constant, or numeric literal Acceptable:Compiler Error: int x[3]; int q = 3 int x[q]; #define q 3 int x[q]; const int q = 3; int x[q]

Initializing Arrays Assigning values at declaration time: –int x[5] = {100, 200, 300, 400, 500}; Letting the compiler determine array size: –int x[] = {100, 200, 300, 400, 500}; Too few initializers - Compiles fine –int x[5] = {100, 200, 300}; Too many initializers - Compiler error –int x[5] = {100, 200, 300, 400, 500, 600};

The size of an array Arrays are very powerful programming tools Can be inefficient at run time –memory usage –searching can be time consuming Size of array should be kept to minimum, or other data structures can be used –Some other structures are covered in 131 and 221

The size of an array Keep arrays as small as possible Keep track of the size of an array –size = type size * number of elements How much memory does a 100 element integer array use? –integers take 4 bytes in Code Warrior –100 * 4 = 400 bytes

Data Types and Sizes TypeSize in bytes int2 - 4 (4 in our system) short2 long 4 float4 double8

How can size be determined? Size of a type can be determined by sizeof –printf(“The size of an integer is %d”, sizeof(int)); –printf(“The size of the array x is %d”, sizeof(x));

Multi-dimensional Arrays Arrays with more than 1 subscript –2 - D arrays have 2 subscripts –3 - D arrays have 3 subscripts –... –n - D arrays have n subscripts Only available memory limits the number of dimensions available Complexity increases exponentially with each added dimension

How do multi - dimensional arrays work? int x[4][3] causes the compiler to set aside memory:

Multi-dimensional arrays 2 - D arrays can be thought of as squares 3 - D arrays can be thought of as cubes 4 - D arrays can be though of as a 4 dimensional object (hyper-cubes) –(insert Twilight Zone theme here) 5 - D … (and so on) In all cases, the data is simply stored in adjacent memory locations

Initializing multi-dimensional arrays All values can be directly initialized: –int x[4][3] = { } Braces can be used to clarify each ‘row’: –int x[4][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10,11,12}}; –The idea of an ‘array of arrays’ extends from this form of initialization

Using for loops to initialize arrays int x[1000]; for (i = 0; i < 1000; i++) x[i] = 1; What does x look like after the loop executes?

Using for loops to initialize arrays counter = 0; int x[4][3]; for (i = 0; i < 4; i++) for (j = 0; j < 3; j++) { x[i][j] = counter; counter += 1; } What does x look like after the loop executes?