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.

Slides:



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

Introduction to C Programming
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
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.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Multiple-Subscripted Array
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
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
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS];
CPS120: Introduction to Computer Science Lecture 15 Arrays.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
 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.
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.
Module 1: Array ITEI222 - Advance Programming Language.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
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.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
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.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
4.1Introduction Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based arrays (C-like) –Arrays.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter 6 Arrays in C++ 2nd Semester King Saud University
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.
Computer Programming BCT 1113
Documentation Need to have documentation in all programs
Arrays Outline 1 Introduction 2 Arrays 3 Declaring Arrays
Arrays, For loop While loop Do while loop
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Array Data Structure Chapter 6
7 Arrays.
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Arrays Kingdom of Saudi Arabia
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.
EKT150 : Computer Programming
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.
MSIS 655 Advanced Business Applications Programming
Arrays Week 2.
CMSC202 Computer Science II for Majors Lecture 03 – Arrays and Functions Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
7 Arrays.
CS150 Introduction to Computer Science 1
Dr Tripty Singh Arrays.
Chapter 9: Data Structures: Arrays
Array Data Structure Chapter 6
CS150 Introduction to Computer Science 1
Arrays Arrays A few types Structures of related data items
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.
Structure (i.e. struct) An structure creates a user defined data type
C++ Array 1.
Programming Fundamental
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

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 are accessed using the base name with a numerical (integer) subscript Subscript numbering starts at zero Ends at N-1 where N is the number of elements in the array Array elements are stored in sequential memory locations

Declaring an Array Syntax: type arrayName[size]; type can be any valid C++ type Ex: int, float, double, char, string, … arrayName can be any valid C++ variable name size can be any positive integer (i.e. greater than zero) Specifies the number of elements in the array

Example Declarations int scores[10]; float salaries[25]; An array of 10 integers float salaries[25]; An array of 25 floating point numbers string names[4]; An array of 4 string variables

Accessing array elements Array elements are accessed using the subscript operator Square brackets [num] Num: an integer between 0 and N-1, where N is the number of elements Error if num is less than zero or greater than or equal to N // an array of 10 integers int scores[10]; scores[0] is the first element of array scores[4] is the fifth element of array scores[9] is the last element of array

Working with array elements Individual array elements are variables They can be used the same way simple variables are Examples: int scores[10]; string names[4]; scores[0] = 80; names[1] = “Bob”; int sum= scores[5] + scores [6]; cout << “hello “ << names[1] << endl; if(scores[3] > scores[4]) { cout << “yes!” << endl; }

Sequential vs. Random access Access any element at any time using a valid subscript number Sequential access Access in numerical order Example: int square[8]; for (int i = 0; i < 8; i++) { square[i] = i * i; } (stored in sequential memory locations) [0] [1] [2] [3] [4] [5] [6] [7] 1 4 9 16 25 36 49

Array Initialization Case 1: type arrayName[N] = { val0, val1, …, val(N-1) }; Specify N initial values If fewer than N are given, unspecified values are uninitialized (random values) Case 2: type arrayName[] = { val0, val1, …, val(N-1) }; Size not specified – array automatically becomes size of initialization list Example float cube[5]= { 0.0, 1.0, 8.0, 27.0, 64.0 }; [0] [1] [2] [3] [4] 0.0 1.0 8.0 27.0 64.0

Passing Arrays to Functions Arrays are always passed by reference (actually by pointer) Which means the elements can be changed by the function Pass entire array to a function by writing just its name (no subscripts or brackets) in the argument list of the function call In function definition and prototype, use empty brackets ([ ]) to indicate an array

Passing Arrays to Functions (example) // function definition void func(int A[], int size) { if(size >= 3) A[2]= 10; } // prototype void func(int[], int); main() { int array[3]= { 2, 4, 6 }; // call the function func(array, 3); cout << array[2] << endl; } This will output “10”

More on passing arrays Use keyword const to indicate that array argument cannot be changed by function Compiler will not allow changes to arrays passed with a const prefix This will not compile // function definition void func(const int A[], int size) { if(size >= 3) A[2]= 10; // error: attempting to change a value in a const array }

Individual elements are passed by value Individual elements are treated like any other variable Individual elements are passed by value // function definition void func(int a) { a=3; } Neither func(x) or func(array[2]) will change the value passed in i.e. both x and array[2] will retain their original values after the “func()” is finished its execution

Parallel Arrays Parallel arrays: a set of arrays that store related information in separate arrays, but with the same index number permits storage of multiple pieces of information for a collection of items For Example: string name[4]= { "John", "Mary", "Lucy", "George" }; string gender[4]= { "male", "female", "female", "male" }; int age[4]= { 20, 21, 19, 23 }; Information about Mary is stored in each array at index 1…

Parallel Array Use // find the oldest int oldest=age[0], indx=0; for (int i=1; i<4; i++) { if(age[i] > oldest) { oldest= age[i]; indx=i; } cout << name[indx] << “is oldest”; string name[4]= { "John", "Mary", "Lucy", "George" }; string gender[4]= { "male", "female", "female", "male" }; int age[4]= { 20, 21, 19, 23 };

Multidimensional Arrays Arrays can have more than one dimension Example: 2D array Commonly used to represent a table char ticTacToe[3][3]; Access ticTacTie[1][2] Note: The total number of elements is just the product of the sizes of each dimension: int array[3][5]; Number of elements is 15 (3*5)

Accessing a Multidimensional Array Indexing starts at zero (like a 1D array)

Initialization Each inner pair of braces contains initial values for one row of the array matrix const int NUM_ROWS = 2; const int NUM_COLS = 3; float matrix[NUM_ROWS][NUM_COLS] = { {5.0, 4.5, 3.0}, {-16.0, -5.9, 0.0} };

Strings and Arrays of Characters string object uses an array of char Can reference individual character of a string object in different ways name[ i ] name.at( i ) A useful member function of string class name.length()