Arrays. Dr. Madhumita Sengupta. Assistant Professor IIIT Kalyani

Similar documents
APSC 160 Review. CPSC 259: Data Structures and Algorithms for Electrical Engineers. Hassan Khosravi Borrowing many questions from Ed Knorr

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

An array is a collection of similar elements that is all the elements in an array should have same data type. This means that an array can store

One Dimension Arrays 1

Unit 1 - Arrays. 1 What is an array? Explain with Example. What are the advantages of using an array?

i location name 3 value at location 6485 location no.(address)

UNIT-I Fundamental Notations

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

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

Principles of Programming. Chapter 6: Arrays

Arrays and Strings. Arash Rafiey. September 12, 2017

NCS 301 DATA STRUCTURE USING C

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos.

APSC 160 Review Part 2

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

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Functions. Arash Rafiey. September 26, 2017

Classification s of Data Structures

C library = Header files + Reserved words + main method

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

Lecture 6 Sorting and Searching

Unit 7. Functions. Need of User Defined Functions

Searching Elements in an Array: Linear and Binary Search. Spring Semester 2007 Programming and Data Structure 1

More Arrays. Last updated 2/6/19

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

n Group of statements that are executed repeatedly while some condition remains true

AMCAT Automata Coding Sample Questions And Answers

1. Simple if statement. 2. if else statement. 3. Nested if else statement. 4. else if ladder 1. Simple if statement

Array. Arrays. Declaring Arrays. Using Arrays

IV Unit Second Part STRUCTURES

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

Government Polytechnic Muzaffarpur.

Module 6: Array in C

Array. Arijit Mondal. Dept. of Computer Science & Engineering Indian Institute of Technology Patna IIT Patna 1

Arrays in C. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur. Basic Concept

Chapter4: Data Structures. Data: It is a collection of raw facts that has implicit meaning.

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

Two Dimensional Array - An array with a multiple indexs.

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

SHARDA UNIVERSITY SCHOOL OF ENGINEERING & TECHNOLOGY Mid Term Examination, (Odd Term, ) SOLUTION

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

MTH 307/417/515 Final Exam Solutions

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

Two Dimensional Array - An array with a multiple indexs.

Basic and Practice in Programming Lab7

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

ARRAYS(II Unit Part II)

CS 101, Spring 2016 March 22nd Exam 2

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal

Decimal Representation

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

MODULE V: POINTERS & PREPROCESSORS

Write a C program using arrays and structure

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

Columns A[0] A[0][0] = 20 A[0][1] = 30

Contents ARRAYS. Introduction:

Arrays in C. By Mrs. Manisha Kuveskar.

To store the total marks of 100 students an array will be declared as follows,

Computer Programming Unit 3

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

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

Introduction to Scientific Computing and Problem Solving

Unit IV & V Previous Papers 1 mark Answers

CSE101-Lec#17. Arrays. (Arrays and Functions) Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

Unit 3 Decision making, Looping and Arrays

Arrays and Pointers. CSE 2031 Fall November 11, 2013

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

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

'C' Programming Language

Initialisation of an array is the process of assigning initial values. Typically declaration and initialisation are combined.

Multi-Dimensional arrays

Two Dimensional Arrays

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

BITG 1113: Array (Part 1) LECTURE 8

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Arrays and Applications

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

Dept. of CSE, IIT KGP

Assoc. Prof. Dr. Tansu FİLİK

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities

CSE2301. Arrays. Arrays. Arrays and Pointers

Data Structures and Algorithms for Engineers

Arrays. Week 4. Assylbek Jumagaliyev

UNDERSTANDING THE COMPUTER S MEMORY

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

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

Pointers and scanf() Steven R. Bagley

C-Refresher: Session 06 Pointers and Arrays

AE52/AC52/AT52 C & Data Structures JUNE 2014

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

Programming for Engineers Arrays

PDS Class Test 2. Room Sections No of students

Solutions to Assessment

Data Structures. Chapter 04. 3/10/2016 Md. Golam Moazzam, Dept. of CSE, JU

C PROGRAMMING Lecture 4. 1st semester

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

Downloaded from

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

Content. In this chapter, you will learn:

Transcription:

Arrays Dr. Madhumita Sengupta Assistant Professor IIIT Kalyani

INTRODUCTION An array is a collection of similar data s / data types. The s of the array are stored in consecutive memory locations and are referenced by an index (also known as the subscript). Declaring an array means specifying three things: The datatype- what kind of values it can store ex, int, char, float Name- to identify the array The size- the maximum number of values that the array can hold Arrays are declared using the following syntax. 1 st type name[size]; 2 nd 3 rd 4 th 5 th 6 th 7 th 8 th 9 th 10 th marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7] marks[8] marks[9] 2

Accessing Elements of the Array To access all the s of the array, you must use a loop. That is, we can access all the s of the array by varying the value of the subscript into the array. But note that the subscript must be an integral value or an expression that evaluates to an integral value. int i, marks[10]; for(i=0;i<10;i++) marks[i] = -1; 3

Calculating the Address of Array Elements Address of data, A[k] = BA(A) + w( k lower_bound) Here, A is the array, k is the index of the of which we have to calculate the address, BA is the base address of the array A. w is the word size of one in memory, for example, size of int is 2. 99 67 78 56 88 90 34 85 Marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7] 1000 1002 1004 1006 1008 1010 1012 1014 Marks[4] = 1000 + 2(4 0) = 1000 + 2(4) = 1008 4

Storing Vales In Arrays Initialization of Arrays Arrays are initialized by writing, type array_name[size]={list of values}; int marks[5]={90, 82, 78, 95, 88}; Inputting Values int i, marks[10]; for(i=0;i<10;i++) scanf( %d, &marks[i]); Assigning Values int i, arr1[10], arr2[10]; for(i=0;i<10;i++) arr2[i] = arr1[i]; 5

Calculating the Length of the Array Length = upper_bound lower_bound + 1 Where, upper_bound is the index of the last and lower_bound is the index of the first in the array 99 67 78 56 88 90 34 85 Here, lower_bound = 0, upper_bound = 7 Therefore, length = 7 0 + 1 = 8 6

Write a Program to Read and Display N numbers using an array #include<stdio.h> int main() { int i=0, n, arr[20]; printf( \n Enter the number of s : ); scanf( %d, &n); for(i=0;i<n;i++) { printf( \n Arr[%d] =, i); } } scanf( %d,&arr[i]); printf( \n The array s are ); for(i=0;i<n;i++) printf( Arr[%d] = %d\t, i, arr[i]); return 0; 7

LINEAR SEARCH LINEAR_SEARCH(A, N, VAL, POS) Step 1: [INITIALIZE] SET POS = -1 Step 2: [INITIALIZE] SET I = 0 Step 3: Repeat Step 4 while I<N Step 4: IF A[I] = VAL, then SET POS = I PRINT POS Go to Step 6 [END OF IF] [END OF LOOP] Step 5: PRINT Value Not Present In The Array Step 6: EXIT 8

BINARY SEARCH BEG = lower_bound and END = upper_bound MID = (BEG + END) / 2 If VAL < A[MID], then VAL will be present in the left segment of the array. So, the value of END will be changed as, END = MID 1 If VAL > A[MID], then VAL will be present in the right segment of the array. So, the value of BEG will be changed as, BEG = MID + 1 9

BINARY SEARCH BINARY_SEARCH(A, lower_bound, upper_bound, VAL, POS) Step 1: [INITIALIZE] SET BEG = lower_bound, END = upper_bound, POS = -1 Step 2: Repeat Step 3 and Step 4 while BEG <= END Step 3: Step 4: [END OF LOOP] SET MID = (BEG + END)/2 IF A[MID] = VAL, then POS = MID PRINT POS Go to Step 6 IF A[MID] > VAL then; ELSE [END OF IF] Step 5: IF POS = -1, then SET END = MID - 1 SET BEG = MID + 1 PRINTF VAL IS NOT PRESENT IN THE ARRAY [END OF IF] Step 6: EXIT 10

BINARY SEARCH int A[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; and VAL = 9, the algorithm will proceed in the following manner. BEG = 0, END = 10, MID = (0 + 10)/2 = 5 Now, VAL = 9 and A[MID] = A[5] = 5 A[5] is less than VAL, therefore, we will now search for the value in the later half of the array. So, we change the values of BEG and MID. Now, BEG = MID + 1 = 6, END = 10, MID = (6 + 10)/2 =16/2 = 8 Now, VAL = 9 and A[MID] = A[8] = 8 A[8] is less than VAL, therefore, we will now search for the value in the later half of the array. So, again we change the values of BEG and MID. Now, BEG = MID + 1 = 9, END = 10, MID = (9 + 10)/2 = 9 Now VAL = 9 and A[MID] = 9. Now VAL = 9 and A[MID] = 9. 11

One dimensional arrays for inter function communication Passing data values main() { int arr[5] ={1, 2, 3, 4, 5}; func(arr[3]); } Passing addresses main() { int arr[5] ={1, 2, 3, 4, 5}; func(&arr[3]); } Passing the entire array void func(int num) { printf("%d", num); } void func(int *num) { printf("%d", num); } main() { } int arr[5] ={1, 2, 3, 4, 5}; func(arr); moid func(int arr[5]) { int i; for(i=0;i<5;i++) printf("%d", arr[i]); } 12

INSERTING AN ELEMENT IN THE ARRAY Algorithm to insert a new to the end of the array. Step 1: Set upper_bound = upper_bound + 1 Step 2: Set A[upper_bound] = VAL Step 3; EXIT The algorithm INSERT will be declared as INSERT( A, N, POS, VAL). Step 1: [INITIALIZATION] SET I = N Step 2: Repeat Steps 3 and 4 while I >= POS Step 3: SET A[I + 1] = A[I] Step 4: SET I = I 1 [End of Loop] Step 5: SET N = N + 1 Step 6: SET A[POS] = VAL Step 7: EXIT Calling INSERT (Data, 6, 3, 100) will lead to the following processing in the array 45 23 34 12 56 20 20 45 23 34 12 12 56 20 Data[0] Data[1] Data[2] Data[3] Data[4] Data[5] Data[6] Data[0] Data[1] Data[2] Data[3] Data[4] Data[5] Data[6] 45 23 34 12 56 56 20 Data[0] Data[1] Data[2] Data[3] Data[4] Data[5] Data[6] 45 23 34 100 12 56 20 13 Data[0] Data[1] Data[2] Data[3] Data[4] Data[5] Data[6]

DELETING AN ELEMENT FROM THE ARRAY Algorithm to delete an from the end of the array Step 1: Set upper_bound = upper_bound - 1 Step 2: EXIT The algorithm DELETE will be declared as DELETE( A, N, POS). Step 1: [INITIALIZATION] SET I = POS Step 2: Repeat Steps 3 and 4 while I <= N - 1 Step 3: SET A[I] = A[I + 1] Step 4: SET I = I + 1 [End of Loop] Step 5: SET N = N - 1 Step 6: EXIT 45 23 34 12 56 20 Data[0] Data[1] Data[2] Data[3] Data[4] Data[5] Calling DELETE (Data, 6, 2) will lead to the following processing in the array 45 23 12 56 56 Data[0] Data[1] Data[2] Data[3] Data[4] Data[5] 45 23 12 56 20 Data[0] Data[1] Data[2] Data[3] Data[4] Data[5] 20 20 20 23 12 12 56 45 Data[0] Data[1] Data[2] Data[3] Data[4] Data[5] 45 23 12 56 20 Data[0] Data[1] Data[2] Data[3] Data[4] 14

First Dimension TWO DIMENSIONAL ARRAYS A two dimensional array is specified using two subscripts where one subscript denotes row and the other denotes column. C looks a two dimensional array as an array of a one dimensional array. A two dimensional array is declared as: data_type array_name[row_size][column_size]; Therefore, a two dimensional mxn array is an array that contains m*n data s and each is accessed using two subscripts, i and j where i<=m and j<=n int marks[3][5] Second Dimension Rows/Columns Col 0 Col 1 Col2 Col 3 Col 4 Row 0 Marks[0][0] Marks[0][1] Marks[0][2] Marks[0][3] Marks[0][4] Row 1 Marks[1][0] Marks[1][1] Marks[1][2] Marks[1][3] Marks[1][4] Row 2 Marks[2][0] Marks[2][1] Marks[2][2] Marks[2][3] Marks[2][4] Two Dimensional Array 15

MEMORY REPRESENTATION OF A TWO DIMENSIONAL ARRAY There are two ways of storing a 2-D array in memory. The first way is row major order and the second is column major order. In the row major order the s of the array are stored row by row where n s of the first row will occupy the first nth locations. (0,0) (0, 1) (0,2) (0,3) (1,0) (1,1) (1,2) (1,3) (2,0) (2,1) (2,2) (2,3) However, when we store the s in a column major order, the s of the array are stored column by column where n s of the first column will occupy the first nth locations. (0,0) (1,0) (2,0) (3,0) (0,1) (1,1) (2,1) (3,1) (0,2) (1,2) (2,2) (3,2) Address(A[I][J])= Base_Address + w{m ( J - 1) + (I - 1)}, if the array s are stored in column major order. and, Address(A[I][J]) = Base_Address + w{n ( I - 1) + (J - 1)}, if the array s are stored in row major order. Where, w is the number of words stored per memory location M, is the number of columns 16 N, is the number of rows I and J are the subscripts of the array

MULTI DIMENSIONAL ARRAYS A multi dimensional array is an array of arrays. Like we have one index in a single dimensional array, two indices in a two dimensional array, in the same way we have n indices in a n- dimensional array or multi dimensional array. Conversely, an n dimensional array is specified using n indices. An n dimensional m1 x m2 x m3 x.. mn array is a collection m1*m2*m3*.. *mn s. In a multi dimensional array, a particular is specified by using n subscripts as A[I1][I2][I3] [In], where, I1<=M1 I2<=M2 I3 <= M3 In <= Mn 17

THE END 18