Multi-Dimensional arrays

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

IV Unit Second Part STRUCTURES

Module 6: Array in C

Unit 3 Decision making, Looping and Arrays

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos.

Write a C program using arrays and structure

Pointers and Arrays 1

Downloaded S. from Kiran, PGT (CS) KV, Malleswaram STRUCTURES. Downloaded from

NCS 301 DATA STRUCTURE USING C

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

How to declare an array in C?

ARRAYS(II Unit Part II)

Questions Bank. 14) State any four advantages of using flow-chart

In the previous lecture, we learned about how to find the complexity of algorithms and search algorithms on array.

First of all, it is a variable, just like other variables you studied

Two Dimensional Arrays

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

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.

DC54 DATA STRUCTURES DEC 2014

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

ONE DIMENSIONAL ARRAYS

UNIT - V STRUCTURES AND UNIONS

UNIT-V. Structures. The general syntax of structure is given below: Struct <tagname> { datatype membername1; datatype membername2; };

Arrays. Arrays are of 3 types One dimensional array Two dimensional array Multidimensional array

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

CSE101-Lec#18. Multidimensional Arrays Application of arrays. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

Structure, Union. Ashishprajapati29.wordpress.com. 1 What is structure? How to declare a Structure? Explain with Example

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

UNIT-I Fundamental Notations

Unit IV & V Previous Papers 1 mark Answers

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

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

Computers Programming Course 11. Iulian Năstac

3.3 Structures. Department of CSE

PDS Class Test 2. Room Sections No of students

'C' Programming Language

Government Polytechnic Muzaffarpur.

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

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

The syntax of structure declaration is. struct structure_name { type element 1; type element 2; type element n;

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Classification s of Data Structures

Example: Structure, Union. Syntax. of Structure: struct book { char title[100]; char author[50] ]; float price; }; void main( )

C++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I

Basics of Programming

F.Y. Diploma : Sem. II [CO/CD/CM/CW/IF] Programming in C

We will introduce another operator & using the example below

MCA Semester 1. MC0061 Computer Programming C Language 4 Credits Assignment: Set 1 (40 Marks)

Single Dimension Arrays

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

Arrays in C. By Mrs. Manisha Kuveskar.

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

Subject: Fundamental of Computer Programming 2068

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

Topics so far. Review. scanf/fscanf. How data is read 1/20/2011. All code handin sare at /afs/andrew/course/15/123/handin

Q 1. Attempt any TEN of the following:

Programming for Engineers Arrays

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Homework #3 CS2255 Fall 2012

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

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

Computer Science & Engineering 150A Problem Solving Using Computers

APSC 160 Review Part 2

Technical Questions. Q 1) What are the key features in C programming language?

Week 8: Arrays and File I/O. BJ Furman 21OCT2009

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Arrays, Pointers and Memory Management

Lab #10 Multi-dimensional Arrays

POINTER & REFERENCE VARIABLES

Microcontroller Systems. ELET 3232 Topic 8: Structures, Arrays, & Pointers

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

More about BOOLEAN issues

struct structure_name { //Statements };

Fundamentals of Computer Programming Using C

Data Representation and Storage

DECLARAING AND INITIALIZING POINTERS

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

Computer Programming Unit 3

b. array s first element address c. base address of an array d. all elements of an array e. both b and c 9. An array elements are always stored in a.

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

Pointers. Memory. void foo() { }//return

Array Initialization

Tribhuvan University Institute of Science and Technology 2065

Pointers and Arrays A QUICK PREVIEW OF FOR CHAPTERS 10 AND 11 CMPE13. Cyrus Bazeghi

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

Advanced C Programming and Introduction to Data Structures

SCSP Programming Technique C

C Programming Language

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

Arrays and Pointers (part 1)

Unit 7. Functions. Need of User Defined Functions

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

Chapter 9 Introduction to Arrays. Fundamentals of Java

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

Assist. Prof. Dr. Caner ÖZCAN

Lab 3. Pointers Programming Lab (Using C) XU Silei

{C} Programming. Part 1/2 Basics Variables, Conditions, Loops, Arrays, Pointer basics

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

Arrays and Pointers (part 1)

Transcription:

Multi-Dimensional arrays An array having more then one dimension is known as multi dimensional arrays. Two dimensional array is also an example of multi dimensional array. One can specify as many dimensions as required. There is no limit on specifying number of dimensions. All dimensions may or may not be the same. The syntax for creating multi dimensions arrays is Datatype arrayname[size1][size2]size3].

Two-Dimensional Arrays. An array is a collection of similar elements that is all elements should have same data type. In 2- D arrays, two dimensions that is two sizes are given. First size represents number of rows where as second size represented no of columns. Both the size is may may not be the same. The syntax for creating two dimension is Datatype arrayname[size1]size2];

A 2-D array can be think as a table which will have i number of rows and j number of columns. A 2-D array a which contains 3 rows and 4 columns can be shown as below: Thus, every element in array a is identified by an element name of the form a[ i ][ j ], where a is the name of the array, and i and j are the subscripts that uniquely identify each element in a.

Initialization of 2-D arrays Initialization means giving values to the arrays. There are two ways of giving values to the arrays i.e. 1- Static initialization 2- Dynamic initialization

Static initialization. In static or compile time we give values to arrays while creating the program. Multidimensional arrays may be initialized by specifying bracketed values for each row. Following is an array with 3 rows and each row have 4 columns. int a[3][4] = { {0, 1, 2, 3}, /* initializers for row indexed by 0 */ {4, 5, 6, 7}, /* initializers for row indexed by 1 */ {8, 9, 10, 11} /* initializers for row indexed by 2 */ };

Example of static initialization int main() { int i,j; int a[2][2] = {10,20,30,40}; // declaring and Initializing array for (i=0;i<2;i++) { for (j=0;j<2;j++) { printf( Elements of array is %d\n",a[i][j]); // Accessing variables } } }

Dynamic initialization.. Indivisual elements of array can be initialised using the following syntax :- #include<stdio.h> int main() { int i,j; int a[2][2]; for (i=0;i<2;i++) { for (j=0;j<2;j++) { scanf( %d,&a[i][j]); } } } // declaring // initializing values to the arrays

Accessing 2-D arrays An element in 2-dimensional array is accessed by using the subscripts i.e. row index and column index of the array.elements of 2-d array can be accessed as follow. a[0][0]=10; //assigns 10 to element at 1 row & 1 column a[0][1]=18; //assigns 10 to element at 1 row & 2 column a[1][0]=16; //assigns 10 to element at 2 row & 1 column a[1][1]=10; //assigns 10 to element at 2 row & 2 column a[2][0]=10; //assigns 10 to element at 3 row & 1 column

main() { int a[3][3],i,j; printf( Enter the elements in array ); for(i=0; i<=2;i++) { for(j=0; j<=2; j++) { scanf( %d,&a[i][j]); } } printf( The elements of array are ); for(i=0; i<=2;i++) { for(j=0; j<=2; j++) { printf( %d,a[i][j]); } printf( \n ); } getch(); }

Implementation of 2-d arrays.. Implementation is the realization of an application, or execution of a plan, idea, model, design etc. It is the process of moving an idea from concept to reality. A two dimensional array can be implemented in a programming language in two ways :- 1-Row major implementation 2-Column major implementation

Row-major implementation. Row major implementation is linearization technique in which elements of arrays is read from keyboard row-wise i.e. the complete first row is stored,then complete second row is stored and so on. e.g. an array a[3][3] is stored in memory as shown below :- a00 a01 a02 a10 a11 a12 a20 a21 a22 Row 1 Row 2 Row 3

We can easily understand the arrangement of array as arranging them in a matrix form like :- a = a 00 a 01 a 02 a10 a 11 a 12 a 20 a21 a 22 row1 row2 row3 above figure shows the actual physical storage of elements of the array, whereas the matrix form is logical representation of array(and not the actual way in which 2-D array is stored).

Address of element in row major implementation. The computer doesn t keep the track of all elements of the array, rather, it keeps a base address(i.e. the address of first element in array), and calculates the address of required element when needed. It calculates this in row major implementation by the following :- Address of element : a[i][j]=b+w(n(i-l 1 )+(j-l 2 )) B=base address, W=size of array s datatype, n=number of columns(i.e. U 2 -L 2 ), L 1 is lower bound of row & L 2 is lower bound of column

Column major implementation In column major implementation memory allocation is done column by column i.e. all the element of first column is stored, then all the elements of second column is stored, and so on. For example an array a[3][3] is stored in the memory as shown below:- a00 a10 a20 a01 a11 a21 a02 a12 a22 column1 column2 column3

Address of element in column major implementation.. In column major implementation address of an element a[i][j] is given by the following relation:- Address of element : a[i][j]=b+w(m(j-l 2 )+(i-l 1 )) Where m is the number of rows(i.e. (i.e. U 1 -L 1 )

POINTERS AND ARRAYS

POINTERS AND ARRAYS An array is a collection of homogeneous/similar type of elements stored in adjacent memory locations. Array and pointers have a close link, in fact array in itself acts like a pointer, as one element in the array is stored adjacent to the previous. Therefore while displaying the element of array, the next adjacent address is automatically called when its previous element is displayed. But in case of using pointers with arrays, the element can be stored in arrays at different locations and can be called from that location when needed.

Before starting the use of pointers with arrays,two facts must be kept in mind: Array element are always stored in continuous locations. The incrementation or decrementation of pointers leads to incrementation or decrementation of address based on the type of pointer defined.

Pointer and One-Dimensional array Pointer is a variable that can store address of another variable. An array that stores only addresses of other variables are known as an array of pointers. Array of pointers can also contain the address of another elements of an array. Processing rules of pointers are same as that of normal arrays. The syntax for an array of pointers is: Datatype *arraynamr[size]; For example: Int *a[3]; It means an array of pointers a is created in memory which can contain the addresses of three integers.

Pointer and Multidimensional arrays It is possible to use pointers with two or more dimensions. The two dimensional array is called a matrix. A simple two-dimensional array element is defined in the form: A[i][j] Where A is the array name The 1st subscript i represents row number. The 2nd subscript j represents numbers of columns.

Counting of rows and columns of the two-dimensional matrix always starts with zero. For example let initialize the elements of a two-dimensional matrix of the range 3 by 2. it will be initialized as: Inta[3][2]= {{10,20}, {30, 40}, {50, 60}}; The above initialized arrays will be stored in memory in following arrangement: Elements : 10 20 30 40 50 60 Matrix location : a[0][0] a[0][1] a[1][0] a[1][1] a[2][0] a[2][1] Address : 200 202 204 206 208 210 Pointers is used in two-dimensional arrays in the same way as in single dimension.the address of the first element of the matrix can be passed on the pointer and rest of the elements can be displayed. For example: Int number[3][4] ; Int *ptr ; Ptr = &number[0][0] ; If we increment the pointer i.e.,ptr++ then pointer variable will be incremented to next data in the matrix. The increment in the pointer leads to increment in the address of elements of matrix(row wise).

ARRAY OF STRUCTURE

STRUCTURE A structure is a collection of related data items held together in a single unit and is referenced by a single name. the data items can be of different data types. The data items enclosed with in a structure are known as structure members or element or fields.

Structure declaration A structure groups various variable into a single record. A structure declaration specifies the grouping of variable of different types in a single unit. The syntax of declaring a structure in C is Struct structure name { Datatype variablename1; Datatype variablename2;... Datatype variablename n; }; A structure declaration do not allocate any memory space.e.g. Struct student { Char name[20]; Int age,roll no; Char address[40] ; };

Structure definition Structure definition creates structure variables and allocate storage space for them. no storage space is allocated at the time of structure declaration. All the structure members are allocated contiguous space in the memory. Struct student s1 Data type variable declaration Creates one variable s1 of the data type and allocates memory to its members as

address 201 220 name 20 bytes S[1] 222 rollno 2 bytes 224 age 2 bytes address 40 bytes 264

ARRAY AND STRUCTURES Structure and array can be used together in two way: 1.Structure containing array 2. Array of structure

Structure containing array structure containing array means some members of a structure is a collection of same type e.g. consider an example of a structure student Storing marks of 5 subjects of a student. It can be represented as struct student { Int roll no; Int marks[5]; Float percent; }s1; The individual marks can be accessed as S1.marks[0],s1.marks[1],s1.marks[2]

ARRAY OF STRUCTURES An array of structure refers to an array in which each element is a structure. as an array of 5 integers is a collection of 5 integers values,similarly, an array of 5 student[structure] is a collection of details of 5 student structure type. Consider an example of student structure: Struct student { Char name[15]; Int rollno; Char adress; }; Student data [50]; The above declaration sets the memory space for 50 objects.

The defination of such an array will be Student Datatype s[10] array of 10 elements of type student S[0] will refer to student1 S[1] will refer to student2 and so on. Memory representation is contiguous as in the case of array of basic data types. E.g. student S[10] occupies contiguous memory for all elements as follows: N name rollno address name rollno address -------- -- name rollno address S[0] S[1] S[10] Memory representation of array of structures

INITIALIZING ARRAY OF STRUCTURES The arrays of structure can also be initialized same as array data in C.it is to be noted that only static or external variable can be initialized. A simple example of initializing the array objects of a structure is : Struct employee { Char name[15]; Int number; Int salary; };

Employee data[3]= {{ rohan,1,20000}, { sumit,2,20000}, { prem,3,20000}}; The above initialized values will be assigned by the compiler as: Data[0].name= rohan data[0].number=1 data[0].salary=20000 Data[1].name= sumit data[1].number=2 data[1].salary=20000 Data[2].name= prem data[2].number=3 data[2].salary=20000 If in case the structure object or any elements of structure are not initialized the compiler will automatically assign zero to the field of that particular record.for example for above Employee data[3]= {{ rohan,1}, { sumit,2}, { prem,3}}; The values assigned by the compiler for the above declaration will be Data[0].name= rohan data[0].number=1 data[0].salary=0 Data[1].name= sumit data[1].number=2 data[1].salary=0 Data[2].name= prem data[2].number=3 data[2].salary=0

Sparse matrix & dense matrix Sparse matrix: contains mostly zero elements. Such as DENSE MATRIX:containing less 0 elements. 1 3 5 2 2 5 2 1 0

LIMITATION OF LINEAR ARRAYS The main limitations of linear arrays are 1. The prior knowledge of number of elements in the linear array is necessary 2. These are static structures. Static in the sense that memory is allocated at compilation time their memory used by them cannot be reduced or extended. 3. Since the elements of these arrays are stored in the these arrays are time consuming this is because of moving down or up to create a space of new element or to occupy the space vacated by the deleted element.