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
Introduction to C Programming
Advertisements

1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
© 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.
More Arrays Arrays and classes Multi-dimensional Arrays Dynamic arrays.
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 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Lesson 7 Arrays CS 1 Lesson 7 -- John Cole1. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored.
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
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
 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
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS];
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.
More Array Access Examples Here is an example showing array access logic: const int MAXSTUDENTS = 100; int Test[MAXSTUDENTS]; int numStudents = 0;... //
 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.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
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)
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
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.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
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
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
Lecture 18 Arrays and Pointer Arithmetic
MSIS 655 Advanced Business Applications Programming
Standard Version of Starting Out with C++, 4th Edition
Arrays Week 2.
Multidimensional 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.
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
C++ Array 1.
CS31 Discussion 1H Fall18: week 6
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 are used and treated the same way any other variable is! 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 in declaration – array automatically becomes size of initialization list Example float cube[5]= { 0.0, 1.0, 8.0, 27.0, 64.0 };

Array Storage Array elements are stored in sequential memory locations Example: float cube[5]= { 0.0, 1.0, 8.0, 27.0, 64.0 }; (note: float type uses 32bits/4 bytes) Cube[5] Memory address Array index Value 0x1000 [0] 0.0 0x1004 [1] 1.0 0x1008 [2] 8.0 0x1012 [3] 27.0 0x1016 [4] 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 Ex: func(array, 3); In both the function definition and prototype, use empty brackets ([ ]) to indicate an array Ex: void func(int A[], int size);

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 }, s=3; // call the function func(array, s); 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; }

Individual elements are passed by value Individual elements are treated like any other variable They 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

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]

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} };

Implicit Declaration Row dimension can be anonymous, but column dimension must be specified float matrix[][2] = {{5.0, 4.5, 3.0}, {-16.0, -5.9, 0.0}}; -> C++ must know the number of elements in each row in order to access a particular array element This can be used in function prototyping and definition

Function sumMatrix float sumMatrix (float table[][NUM_COLS], int rows) { float sum = 0.0; // Add each array element value to sum. for (int r = 0; r < rows; r++) for (int c = 0; c < NUM_COLS; c++) sum += table[r][c]; return sum; } * Note: NUM_COLS is global

Arrays with Several Dimensions const int PEOPLE = 10; const int YEARS = 5; money sales[PEOPLE][YEARS][12];

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()