Lec 13 Oct 21, 02. Array Initialization in the declaration statement ► int temp[5] = {98, 87, 92, 79,85}; ► char codes[6] = { ‘s’, ’a’, ‘m’, ‘p’, ‘l’,

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Two-Dimensional Arrays Chapter What is a two-dimensional array? A two-dimensional array has “rows” and “columns,” and can be thought of as a series.
Topic 9C – Multiple Dimension Arrays. CISC105 – Topic 9C Multiple Dimension Arrays A multiple dimension array is an array that has two or more dimensions.
Arrays CSE 5100 Data Structures and Algorithms. One-Dimensional Arrays  A list of values with the same data type that are stored using a single group.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
©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.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Multiple-Subscripted Array
Lecture 5 Arrays A way to organize data © MIT AITI 2003.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
CSE202: Lecture 16The Ohio State University1 Two Dimensional Arrays.
Arrays. Arrays  When a value is to be used in a program, a variable is declared to hold that value  What about storing the results of exams for a large.
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
Arrays CS 308 – Data Structures. One-Dimensional Arrays A list of values with the same data type that are stored using a single group name (array name).
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
Multidimensional Arrays C++ also allows an array to have more than one dimension. For example, a two-dimensional array consists of a certain number of.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Arrays in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
A First Book of ANSI C Fourth Edition
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MCA, MSc[IT], MTech[IT],MPhil (Comp.Sci), PGDCA, ADCA, Dc. Sc. & Engg.
Chapter 8 Arrays and Strings
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Lecture 5: Arrays A way to organize data MIT AITI April 9th, 2005.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
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.
A First Book of ANSI C Fourth Edition Chapter 8 Arrays.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
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.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
Get Longest Run Index (FR) public int getLongestRunIndex(int []values) { int maxRunStart = -1, maxRunLength = 1; int runStart = 0, runLength = 1; for(int.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Computer Programming for Engineers
CSCI 130 More on Arrays. Multi-dimensional Arrays Multi - Dimensional arrays: –have more than one subscript –can be directly initialized –can be initialized.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Multidimensional Arrays Vectors of Vectors When One Is Not Enough.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
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.
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.
Arrays. Arrays are objects that help us organize large amounts of information.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
1 Two-Dimensional Arrays. 2 Terminology Two-dimensional arrays represent matrices A matrix contains a number of values of the same data type The values.
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.
Objectives You should be able to describe: One-Dimensional Arrays
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Arrays 4/4 By Pius Nyaanga. Outline Multidimensional Arrays Two-Dimensional Array as an Array of Arrays Using the length Instance Variable Multidimensional.
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.
multi-dimensional arrays
Two Dimensional Array Mr. Jacobs.
CNG 140 C Programming (Lecture set 8)
Declaration, assignment & accessing
Lecture 12 Oct 16, 02.
Multidimensional Arrays
Multidimensional array
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.
Multidimensional Arrays Section 6.4
C++ Array 1.
Dr. Khizar Hayat Associate Prof. of Computer Science
Visit for more Learning Resources
Presentation transcript:

Lec 13 Oct 21, 02

Array Initialization in the declaration statement ► int temp[5] = {98, 87, 92, 79,85}; ► char codes[6] = { ‘s’, ’a’, ‘m’, ‘p’, ‘l’, ‘e’ }; ► double slopes[3] = {10.96, 6.43, 2.58}; // multi line initialization below. ► int gallons[20] = {19, 16, 14, 19, 20, 18, 12, 10, 22, 15, 18, 17, 12, 10, 22, 15, 18, 17, 16, 14, 23, 19, 15, 18, 16, 14, 23, 19, 15, 18, 21, 5 }; 21, 5 };

Array Initialization in the declaration statement contd.. ► float length[7] = {7.8, 6.4, 4.9, 11.2}; / * In the above declaration, number of elements listed in curly brackets is not same as numbers of elements listed in square brackets. Therefore, length[0], length[1], length[2], and length[3] are initilized with listed values. The other array elements will be initialized to zero.*/ / * In the above declaration, number of elements listed in curly brackets is not same as numbers of elements listed in square brackets. Therefore, length[0], length[1], length[2], and length[3] are initilized with listed values. The other array elements will be initialized to zero.*/ ► int gallons[] = {16, 12, 10, 14, 11}; /* A unique feature of initializers is that the size of an array may be omitted when initializing values are included in the declaration statement. /* A unique feature of initializers is that the size of an array may be omitted when initializing values are included in the declaration statement.

Array Initialization in the declaration statement contd.. ► char codes[6] = { ‘s’, ‘a’, ‘m’, ‘p’, ‘l’, ‘e’ }; char codes[] = { ‘s’, ‘a’, ‘m’, ‘p’, ‘l’, ‘e’ }; char codes[] = { ‘s’, ‘a’, ‘m’, ‘p’, ‘l’, ‘e’ }; String initialization: String initialization: char codes[] = “sample”; char codes[] = “sample”; // \o below is called null character // \o below is called null character samPle\o

Declaring and Processing 2-D arrays ► A two – dimensional array, which is sometimes referred to as a table, consists of both rows and columns of elements. ► Eg: is called a two – dimensional array of integers. This array consists of 3 rows and four columns. is called a two – dimensional array of integers. This array consists of 3 rows and four columns. Declaration of the above array is: Declaration of the above array is: int val[3][4] ; // array named value with 3 rows and 4 int val[3][4] ; // array named value with 3 rows and 4 columns columns

Other examples ► float volts[10][5]; /* 2-D array named volts with 10 rows and /* 2-D array named volts with 10 rows and 5 columns */ 5 columns */ ► int code[6][26]; /* 2-D array named code with 6 rows and 26 columns */ /* 2-D array named code with 6 rows and 26 columns */

Accessing 2-D arrays ► The term val[1][3] uniquely identifies the element in row 1, column 3. ► watts = val[2][3]; val[0][0] = 62; val[0][0] = 62; newnum = 4 * (val[1][0] -5); newnum = 4 * (val[1][0] -5); Sum=val[0][0]+val[0][1]+val[0][2]+val[0][3]; Sum=val[0][0]+val[0][1]+val[0][2]+val[0][3];

Initilization in declaration statement: ► int val[3][4] = { 8, 16, 9, 52, 3, 15, 27, 6, 3, 15, 27, 6, 14, 25, 2, 10}; 14, 25, 2, 10}; (or) int val[3][4] = { {8,16,9,52}, (or) int val[3][4] = { {8,16,9,52}, {3,15,27,6}, {3,15,27,6}, {14,25,2,10} }; {14,25,2,10} }; (or) int val[3][4] ={8,16,9,52,3,15,27,6,14,25,2,10}; (or) int val[3][4] ={8,16,9,52,3,15,27,6,14,25,2,10}; // Note : 2-D array initialization is done row-wise. First, the elements of the first row are initialized, then second row and so on…

Example 1 ► #include ► #include int main() int main() { const int NUMROWS = 3; const int NUMCOLS = 4; const int NUMROWS = 3; const int NUMCOLS = 4; int i,j; int i,j; for(i=0; i<NUMROWS; i++) // outer loop for(i=0; i<NUMROWS; i++) // outer loop { cout<<endl; cout<<endl; for ( j=0; j<NUMCOLS; j++) // inner loop for ( j=0; j<NUMCOLS; j++) // inner loop //{ //{ //val [I] [j] = val [i] [j] * 10; for processing of 2-D arrays //val [I] [j] = val [i] [j] * 10; for processing of 2-D arrays cout<<setw(5)<<val[i][j]; cout<<setw(5)<<val[i][j]; // } // } } cout<<endl; cout<<endl; return 0; return 0; }

Larger Dimension arrays ► Although arrays with more than two dimensions are not commonly used, C++ does allow any number of dimensions to be declared. ► eg: int response[4][10][6]; first element [0][0][0] first element [0][0][0] last element [3][9][5] last element [3][9][5]

Conceptual Understanding of larger dimensional arrays ► Conceptually, a three dimensional array can be viewed as a book of data tables. Using this visualization, the first index can be thought of as the location of the desired row in a table, the second index value as the desired column, and the third index value, as the page number of the selected table. Similarly an array of any dimension can be declared. ► eg: Fourth dimension is used to declare a desired book on the shelf. Fifth selected shelf in the book case. And son on…….