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.

Similar documents
How to declare an array in C?

Module 6: Array in C

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos.

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

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

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

Government Polytechnic Muzaffarpur.

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

Lab Session # 5 Arrays. ALQUDS University Department of Computer Engineering

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

Computer Science & Engineering 150A Problem Solving Using Computers

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

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

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

Arrays in C. By Mrs. Manisha Kuveskar.

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

Computer Programming

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

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

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

Principles of Programming. Chapter 6: Arrays

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

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

C++ PROGRAMMING SKILLS Part 4: Arrays

Unit 3 Decision making, Looping and Arrays

Computer Programming: C++

Chapter 9 Introduction to Arrays. Fundamentals of Java

Multiple-Subscripted Arrays

PROGRAMMAZIONE I A.A. 2017/2018

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

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

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

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

CHAPTER 3 ARRAYS. Dr. Shady Yehia Elmashad

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

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

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

Programming for Engineers Arrays

Arrays. int Data [8] [0] [1] [2] [3] [4] [5] [6] [7]

Chapter 6: Using Arrays

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

Tribhuvan University Institute of Science and Technology 2065

Array. Prepared By - Rifat Shahriyar

Abstract Data Type (ADT) & ARRAYS ALGORITHMS & DATA STRUCTURES I COMP 221

Subject: Computer Science

Matrix Multiplication

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

Outline Introduction Arrays Declaring Arrays Examples Using Arrays Passing Arrays to Functions Sorting Arrays

Lab #10 Multi-dimensional Arrays

Multi-Dimensional arrays

ONE DIMENSIONAL ARRAYS

Pointers and Arrays 1

Matrix Multiplication

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

Two Dimensional Arrays

Programming for Electrical and Computer Engineers. Pointers and Arrays

Arrays. Eng. Mohammed Abdualal

Array. Array Declaration:

Sparse Matrices. sparse many elements are zero dense few elements are zero

[ DATA STRUCTURES] to Data Structures

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

LAB 2 VECTORS AND MATRICES

Arrays in C C Programming and Software Tools. N.C. State Department of Computer Science

Give one example where you might wish to use a three dimensional array

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

OCA Java SE 7 Programmer I Certification Guide By Mela Gupta. Arrays

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays

15. Multidimensional Arrays

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

C++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections

CSE202- Lec#6. (Operations on CSE202 C++ Programming

Chapter 7 Multidimensional Arrays. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

C: How to Program. Week /Apr/23

CpSc 1111 Lab 9 2-D Arrays

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

C PROGRAMMING LANGUAGE. POINTERS, ARRAYS, OPERATORS AND LOOP. CAAM 519, CHAPTER5

Classification s of Data Structures

Single Dimension Arrays

Chapter 7 : Arrays (pp )

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04

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

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

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

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

Objectives of This Chapter

ENGR 1181 MATLAB 02: Array Creation

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

Arrays. Week 4. Assylbek Jumagaliyev

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

CSCE 110 PROGRAMMING FUNDAMENTALS. Prof. Amr Goneid AUC Part 7. 1-D & 2-D Arrays

Representing 2D Transformations as Matrices

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

List of Practical for Class XII Computer Science

Arrays and Strings. Arash Rafiey. September 12, 2017

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

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

Arrays array array length fixed array fixed length array fixed size array Array elements and subscripting

COMPUTER SCIENCE Paper 2 (PRACTICAL)

Matrices. Jordi Cortadella Department of Computer Science

Transcription:

Data Structures

Introduction 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. Representation of a large number of homogeneous values. Structures of related data items. Static entity same size throughout program. Group of consecutive memory locations. Array size must be a constant value.

How to declare an array in C? To refer to an element, specify Array name Position number / index/ subscript Format: data_type array_name[array_size]; For example: if you want to store marks of 100 students, you can create an array for it. int marks[100]; We have declared an array with name marks of int type and size 100, that means, it can hold 100 int values. The size and type of arrays cannot be changed after its declaration.

Elements of an array and how to access them? By default, first element is always at position 0. You can access elements of an array by indices. Suppose you declared an array mark as float mark[5]. The first element is mark[0], second element is mark[1] and so on. Arrays have 0 as the first index not 1. In this example, mark[0]. If the size of an array is n, to access the last element, (n-1) index is used. In this example, mark[4]. Suppose the starting address of mark[0] is 2120. Then, the next address, a[1], will be 2124, address of a[2] will be 2128d and so on. It's because the size of a float is 4 bytes. Hence, address of any element will be address of mark[i] = add of mark[0] + i * sizeof(datatype)

Types of arrays There are 2 types of C arrays. They are: One dimensional array Multi dimensional array Two dimensional array Three dimensional array four dimensional array etc

One-dimensional array How to initialize an array in C programming? Compile time initialization Run time initialization Compile time initialization It's possible to initialize an array during declaration. For example, int mark[5] = {19, 10, 8, 17, 9}; Another method to initialize array during declaration: int mark[] = {19, 10, 8, 17, 9};

One-dimensional array Declaring multiple arrays of same type, format similar to regular variables. Example: int b[ 100 ], x[ 27 ]; int n[ 5 ] = { 1, 2, 3, 4 }; If not enough initializers, rightmost elements become 0. Here, n[4] will be initialized to zero. int n[ 5 ] = { 0 } All elements 0 It is a best practice to initialize an array to zero or null while declaring, if we don t assign any values to array. C arrays have no bounds checking. If size omitted, initializers determine it. int n[ ] = { 1, 2, 3, 4, 5 }; 5 initializers, therefore 5 element array. If you try to access array elements outside of its bound, let's say a[12], the compiler may not show any error. However, this may cause unexpected output (undefined behavior)/garbage value.

One-dimensional array Run time initialization It's possible to initialize an array during run time by scanning the array.

Applications of 1D Array Calculate average of an array Largest element from an array Searching Sorting

Write a program using 1D array x to read the values and compute the sum of their squares

(Contd.)

Program for frequency counting

Multi-dimensional array

Two-dimensional array In C programming, you can create array of an array known as multidimensional array. For example, float x[3][4]; Here, x is a two-dimensional (2d) array. The array can hold 12 elements. You can think the array as table with 3 row and each row has 4 column.

How to initialize a two-dimensional array? There are many ways to initialize a two dimensional array (Compile Time): int c[2][3] = {{1, 3, 0}, {-1, 5, 9}}; int c[][3] = {{1, 3, 0}, {-1, 5, 9}}; int c[2][3] = {1, 3, 0, -1, 5, 9};

Initializing 2D array (Run time)

Applications of 2D Array Multiplication Table Matrix Operations Addition of matrices Multiplication of matrices Transpose of a matrix Symmetric matrix etc.

Representation of a two-dimensional array in memory

(Contd.)

Illustration of two-dimensional arrays

Three-dimensional array Similarly, you can declare a three-dimensional (3d) array. For example, float y[2][4][3]; Let's take a closer look at a 3D array. A 3D array is essentially an array of arrays of arrays: it's an array or collection of 2D arrays, and a 2D array is an array of 1D array. Here, The array can hold 24 elements. You can think this example as: Each 2 elements have 4 elements, which makes 8 elements and each 8 elements can have 3 elements. Hence, the total number of elements is 24.

Three-dimensional array

How to initialize a three-dimensional array? You can initialize a three dimensional array in a similar way like a two dimensional array. Here's an example, int test[2][3][4] = { { {3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2} }, }; { {13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9} }

Initializing 3D array (Run time)

ANY QUESTIONS???