Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA.

Similar documents
A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

Module 6: Array in C

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

Multiple-Subscripted Arrays

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

C for Engineers and Scientists: An Interpretive Approach. Chapter 10: Arrays

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

V2 3/5/2012. Programming in C. Introduction to Arrays. 111 Ch 07 A 1. Introduction to Arrays

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

Creating Two-dimensional Arrays

Arrays in C. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Chapter 9 Introduction to Arrays. Fundamentals of Java

ONE DIMENSIONAL ARRAYS

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

Objectives. Chapter 8 Arrays and Strings. Objectives (cont d.) Introduction 12/14/2014. In this chapter, you will:

Maltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays

How to declare an array in C?

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)

Chapter 7 : Arrays (pp )

CSE 230 Intermediate Programming in C and C++ Arrays and Pointers

Programming for Engineers Arrays

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

Lesson 7. Reading and Writing a.k.a. Input and Output

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

11. Arrays. For example, an array containing 5 integer values of type int called foo could be represented as:

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

Lecture 3 Tao Wang 1

Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal

Arrays in Java Multi-dimensional Arrays

Objectives of This Chapter

Outline Arrays Examples of array usage Passing arrays to functions 2D arrays Strings Searching arrays Next Time. C Arrays.

STRUCTURED DATA TYPE ARRAYS IN C++ ONE-DIMENSIONAL ARRAY TWO-DIMENSIONAL ARRAY

C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved.

CSI33 Data Structures

Unit 3 Decision making, Looping and Arrays

Review of the C Programming Language for Principles of Operating Systems

Procedural Programming

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

BITG 1113: Array (Part 1) LECTURE 8

INTRODUCTION 1 AND REVIEW

Chapter 7 Array. Array. C++, How to Program

Computers Programming Course 11. Iulian Năstac

Chapter 12 Two-Dimensional Arrays

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Unit 3. Operators. School of Science and Technology INTRODUCTION

Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal

Review of the C Programming Language

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Chapter 6 Pointers and Arrays

QUIZ: loops. Write a program that prints the integers from -7 to 15 (inclusive) using: for loop while loop do...while loop

Computer Programming: C++

Ar r ays and Pointer s

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

Engineering program development 6. Edited by Péter Vass

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 1

Chapter 7 Functions. Now consider a more advanced example:

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal

Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal

Write a C program using arrays and structure

by Pearson Education, Inc. All Rights Reserved.

[ DATA STRUCTURES] to Data Structures

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Principles of Programming. Chapter 6: Arrays

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

Intermediate Code Generation

Computer Science & Engineering 150A Problem Solving Using Computers

SNAP Centre Workshop. Graphing Lines

Fundamentals of Programming Session 14

Steps of initialisation:

IV Unit Second Part STRUCTURES

x = 3 * y + 1; // x becomes 3 * y + 1 a = b = 0; // multiple assignment: a and b both get the value 0

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

Function Call Stack and Activation Records

tablica: array: dane_liczbowe

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Chapter 8 :: Composite Types

PROGRAMMAZIONE I A.A. 2017/2018

Review of Important Topics in CS1600. Functions Arrays C-strings

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

UNIT-I Fundamental Notations

Computers Programming Course 10. Iulian Năstac

ARRAYS(II Unit Part II)

Arrays. Theoretical Part. Contents. Keywords. Programming with Java module 3

Matlab- Command Window Operations, Scalars and Arrays

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING

Cpt S 122 Data Structures. Introduction to C++ Part II

2. Each element of an array is accessed by a number known as a(n) a. a. subscript b. size declarator c. address d. specifier

COMPUTER APPLICATION

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Chapter 6 part 1. Data Types. (updated based on 11th edition) ISBN

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Transcription:

Arrays Defining arrays, declaration and initialization of arrays

Introduction Many applications require the processing of multiple data items that have common characteristics (e.g., a set of numerical data, represented by x 1, x 2, x 3,... x n ). In such situations it is often convenient to place the data item into an array, where they all share the same name ( e.g., x). The individual data items can be characters, integers, floatingpoint numbers, etc. However, they must all be of the same type and the same storage class.

Introduction Each array element ( i.e., each individual data item) is referred to by specifying the array name followed by one or more subscripts, with each subscript enclosed in square brackets. Each subscript must be expressed as a non-negative integer. In an n-element array, the array elements are x[0], x[1],....., x[n-1], as illustrated in Fig. x[0] x[1] x[2] x[n-2] x[n-1]

Introduction The value of each subscript can be expressed as an integer constant or an integer constant variable or a more complex integer expression. The number of subscripts determines the dimensionality of the array. For example, x[i] refers to an element in the one dimensional array x. Similarly, y[i][j] refers to an element in the two-dimensional array y.

Defining Arrays Arrays are defined in much the same manner as ordinary variables, except that each array name must be accompanied by a size specification (i.e., the number of elements). For a one-dimensional array, the size is specified by a positive integer expression, enclosed in square brackets. The expression is usually written as a positive integer constant.

Defining Arrays Method 1 In general terms, a one-dimensional array definition may be expressed as: storage-class data-type array [ expression ]; where storage-class refers to the storage class (namely, auto, extern, static, register) of the array, data-type is the data type, array is the array name, and expression is a positive-valued integer expression which indicates the number of array elements.

Defining Arrays Method 1 In general terms, a one-dimensional array definition may be expressed as: storage-class data-type array [ expression ]; the storage-class in optional; default values are automatic for arrays that are defined within a function or a block and external for arrays that are defined outside of a function.

Defining Arrays - Examples Several typical one-dimensional array definitions are shown below: int x[100]; char text [80]; static char message [25]; static float n [12]; The first line states that x is a 100-element integer array. The second line defines text to be an 80-element character array. In the third line, message is defined as static 25-element character array, whereas the fourth line establishes n as static 12-element floating-point array.

Defining Arrays Method 2 It is sometimes convenient to define an array size in terms of a symbolic constant rather than a fixed integer quantity. This makes it easier to modify a program that utilizes an array, since all references to the maximum array size can be altered simply by changing the value of the symbolic constant.

Defining Arrays Examples Typical one-dimensional array definition is shown below: #define SIZE 100 int x[size]; Notice that the symbolic constant SIZE is assigned a value of 100. This symbolic constant, rather than its value, appears in the array definition. ( Remember that the value of the symbolic constant will be substituted for the constant itself by the preprocessor before the compilation [translation] process. )

The general form is: Initializing Arrays storage-class data-type array [ expression ] = { value 1, value 2,, value n} ; where value 1 refers to the value of the first array element, value 2 refers to the value of the second element, and so on. The appearance of the expression, which indicates the number of array elements, is optional when initial values are present.

Initializing Arrays - Examples Shown below are several array definitions that include the assignment of initial values: int digits[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; static float x [6] = { 0, 0.25, 0, -0.50, 0, 0 }; char color [3] = { R, E, D }; Note that x is a static array. The other two arrays ( digits and color ) are assumed to be external arrays by virtue of their placement within the program.

Initializing Arrays - Note All individual array elements that are not assigned explicit initial values will automatically be set to zero.

Initializing Arrays Examples- Variations Consider the following array definitions: int digits[10] = { 3, 3, 3 }; The result, on an element-by-element basis, are as follows: ( Remember that the subscripts in an n-element array range from 0 to n-1). All elements are set to 0 except those that have been explicitly initialized within array definitions. digits [0] = 3 digits [1] = 3 digits [2] = 3 digits [3] = 0 digits [4] = 0 digits [5] = 0 digits [6] = 0 digits [7] = 0 digits [8] = 0 digits [9] = 0

Initializing Arrays Examples- Variations Consider the following array definitions: int digits[ ] = { 1, 2, 3, 4, 5, 6 }; Thus digits, will be a six-element integer array. The array size need not be specified explicitly when initial values are included as a part of as array definition. digits [0] = 1 digits [1] = 2 digits [2] = 3 digits [3] = 4 digits [4] = 5 digits [5] = 6

Initializing Arrays Strings ( i.e., character arrays) are handled somewhat differently. In particular, when a string constant is assigned to an external or a static character array as a part of the array definition, the array size specification is usually omitted. The proper array size will be assigned automatically. This will include a provision for the null character \0, which is automatically added at the end of every string.

Initializing Arrays Examples- Variations Consider the following two character array definitions. Each includes the initial assignment of the string constant RED. However,the first array is defined as a three element array, whereas the size of the second array is unspecified. char color [3] = RED ; char color [ ] = RED ; The results of these initial assignments are not the same because of the null character, \0, which is automatically added at the end of the second string.

Initializing Arrays Examples- Variations Thus, the elements of the first array are: char color [0] = R ; char color [1] = E ; char color [2] = D ; char color [3] = RED ; Thus, the elements of the second array are: char color [0] = R ; char color [1] = E ; char color [2] = D ; char color [3] = \0 ; char color [ ] = RED ;

Initializing Arrays Examples- Variations Thus, the first form is incorrect, since the null character \0 is not included in the array. The array definition could also have been written as: char color [4] = RED ; This definition is correct, since we are now defining a fourelement array which includes an element for the null character.

Defining Arrays 2D The arrays we have discussed so far are known as Onedimensional arrays because the data are organized linearly only in one direction ( dimension ). Many applications require that the data be organized in more than one dimension. One common example is matrix ( a table ), which is an array that consists of rows and columns.

Defining Arrays 2D Following Figure shows a matrix ( table) with 4 rows and 4 columns which is commonly used as a two-dimensional array. First dimension ( rows ) 0 1 2 3 0 1 2 3 Second dimension ( columns )

Defining Arrays 2D Although two-dimensional array is exactly what is shown in previous Figure, C language looks at it in a different way. It looks at the two-dimensional array as an array of arrays. In other words, a two-dimensional array in C language is an array of one-dimensional arrays. This concept is shown in the next figure.

Defining Arrays 2D table[0][0] table[0][1] table[0][2] table[0][3] table[0] table[1][0] table[1][1] table[1][2] table[1][3] table[1] table[2][0] table[2][1] table[2][2] table[2][3] table[2] table[3][0] table[3][1] table[3][2] table[3][3] table[3] table Array of arrays

Defining Arrays 2D In general terms, a two-dimensional array definition may be expressed as: storage-class data-type array [ expression 1 ] [ expression 2]; where storage-class refers to the storage class of the array, data-type is the data type, array is the array name, and expression 1 is a positive-valued integer expression which indicates the number of rows in the array while expression 2 indicates the number of columns in each row.

Defining Arrays 2D- Examples Typical two-dimensional array definition is shown below: int table[4][4]; The statement states that table is a 16-element integer array. Elements of two dimensional array are stored row-wise, i.e., in the contiguous block of memory, first element of first row are stored, then elements of second row, then elements of third row, and so on.

Initializing Arrays 2D- Examples Shown below is an array definition that include the assignment of initial values: int table[4][4] = { 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3 }; It will initialize all the elements of the first row with value 0, elements of second row with 1, elements of third row with 2 and elements of the fourth row with value 3.

Initializing Arrays 2D- Examples 0 0 0 0 table[0][0] table[0][1] table[0][2] table[0][3] table[0] 1 1 1 1 table[1][0] table[1][1] table[1][2] table[1][3] table[1] 2 2 2 2 table[2][0] table[2][1] table[2][2] table[2][3] table[2] 3 3 3 3 table[3][0] table[3][1] table[3][2] table[3][3] table[3] table Array of arrays

Initializing Arrays 2D- Examples Shown below is an array definition that include the assignment of initial values ( with the help of nest braces ): int table[4][4] = { { 0, 0, 0, 0 }, { 1, 1, 1, 1 }, { 2, 2, 2, 2 }, { 3, 3, 3, 3}, }; Each row is initialized as a onedimensional array of four elements enclosed in braces. It will initialize all the elements of the first row with value 0, elements of second row with 1, elements of third row with 2 and elements of the fourth row with value 3.

Initializing Arrays 2D- Examples Shown below is an array definition that include the assignment of initial values ( with the help of nest braces ): int table[ ][4] = { { 0, 0, 0, 0 }, { 1, 1, 1, 1 }, { 2, 2, 2, 2 }, { 3, 3, 3, 3}, }; The first dimension can be omitted if the array is completely initialized. It will initialize all the elements of the first row with value 0, elements of second row with 1, elements of third row with 2 and elements of the fourth row with value 3.

Defining Arrays Multi-dimensional Multi-dimensional arrays can have three, four or more dimensions. The terminology used to describe the three-dimensional array is planes, rows and columns. The first dimension is called plane, which consists of rows and columns.

Defining Arrays 3D Following Figure shows a three dimensional array ( 3 3 3 ): Second Dimension ( rows ) First Dimension ( planes ) Third Dimension ( columns )

Defining Arrays 3D Although three-dimensional array is exactly what is shown in previous Figure, C language looks at it in a different way. It takes a three-dimensional array to be an array of twodimensional arrays. It considers the two-dimensional array to bean array of one dimensional arrays. In other words, a three-dimensional array in C language is an array of arrays of arrays. This concept also holds true for arrays of more than three dimensions.

Defining Arrays 3D In general terms, a three-dimensional array definition may be expressed as: storage-class data-type array [ expression 1 ] [ expression 2] [ expression 3]; where storage-class refers to the storage class of the array, data-type is the data type, array is the array name, and expression 1 is a positive-valued integer expression which indicates the number of planes while expression 2 indicates the number of rows in an array and expression 3 indicates the columns in each row Ḋesigned by Parul Khurana, LIECA.

Defining Arrays 3D- Examples Typical two-dimensional array definition is shown below: int table[2][3][4]; The statement states that table is a 24-element integer array organized as having: 2 Planes or Pages (with plane index: 0, 1) 3 Rows in each plane (with row index: 0, 1, 2) 4 Columns in each row (with column index: 0, 1, 2, 3) Elements of two dimensional array are stored row-wise, i.e., in the contiguous block of memory, first element of first row are stored, then elements of second row, then elements of third row, and so on.

Initializing Arrays 3D- Examples Shown below is an array definition that include the assignment of initial values: int table[2][3][4] = { { /* PLANE 0 */ { 0, 0, 0, 0 }, /* ROW 0 */ { 1, 1, 1, 1 }, /* ROW 1 */ { 2, 2, 2, 2 } /* ROW 2 */ }, { /* PLANE 1 */ { 3, 3, 3, 3 }, /* ROW 0 */ { 4, 4, 4, 4 }, /* ROW 1 */ { 5, 5, 5, 5 } /* ROW 2 */ } };

Practice Questions Program to compute the sum of positive values and the negative values separately. All the values are stored in an array. Program to split the values of the array A[] = {12.34, 25.04, 1295.50, 45.33, 1.76} into two arrays as: R[] = {12, 25, 1295, 45, 1} P[] = {34, 4, 50, 33, 76} Write a program to print the reverse of every element of the array. Program to print the sum of elements of every row of the array in its respective final column.

Consider an array initialized as: int z[3][2][4] = {9,8,7,6,5,4,3,2,1,0,9,8,7,6,5,4,3,2,1,0,8,6,4,2}; Draw the logical memory map for the above array. Determine the value represented by z[2][0][1] z[0][1][0] z[1][1][2] z[0][0][3] z[1][0][1] Practice Questions Also, express the array index (in form of z[?][?][?]) where the stored data element is 6.