How to declare an array in C?

Similar documents
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.

Module 6: Array in C

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos.

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

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

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

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

Government Polytechnic Muzaffarpur.

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

Computer Programming: C++

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 Science & Engineering 150A Problem Solving Using Computers

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

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

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

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

Unit 3 Decision making, Looping and Arrays

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

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

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

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

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

Array. Prepared By - Rifat Shahriyar

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

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

PROGRAMMAZIONE I A.A. 2017/2018

C++ PROGRAMMING SKILLS Part 4: Arrays

Multiple-Subscripted Arrays

Lab #10 Multi-dimensional Arrays

Multi-Dimensional arrays

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

Principles of Programming. Chapter 6: Arrays

CHAPTER 3 ARRAYS. Dr. Shady Yehia Elmashad

ONE DIMENSIONAL ARRAYS

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

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

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

Array. Array Declaration:

Programming for Engineers Arrays

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

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

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

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

CpSc 1111 Lab 9 2-D Arrays

Classification s of Data Structures

Matrix Multiplication

Computer Programming

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

Arrays. Week 4. Assylbek Jumagaliyev

Chapter 6: Using Arrays

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

Arrays and Strings. Arash Rafiey. September 12, 2017

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

Matrix Multiplication

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

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Chapter 9 Introduction to Arrays. Fundamentals of Java

Programming for Electrical and Computer Engineers. Pointers and Arrays

[ DATA STRUCTURES] to Data Structures

Array. Arrays. Declaring Arrays. Using Arrays

COS 140: Foundations of Computer Science

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

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

C: How to Program. Week /Apr/23

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

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

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

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

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

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

Learning Objectives. Introduction to Arrays. Arrays in Functions. Programming with Arrays. Multidimensional Arrays

CMSC 202. Pointers Dynamic Memory Allocation

Tribhuvan University Institute of Science and Technology 2065

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

Single Dimension Arrays

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

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

15. Multidimensional Arrays

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. #29 Arrays in C

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

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

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

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

PDS Class Test 2. Room Sections No of students

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

UNIT-1. Chapter 1(Introduction and overview) 1. Asymptotic Notations 2. One Dimensional array 3. Multi Dimensional array 4. Pointer arrays.

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

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

Subject: Computer Science

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Chapter 8 :: Composite Types

Two Dimensional Arrays

Binghamton University. CS-220 Spring Arrays in C

(Refer Slide Time: 00:23)

Chapter 7 : Arrays (pp )

Chapter 12: Pointers and Arrays. Chapter 12. Pointers and Arrays. Copyright 2008 W. W. Norton & Company. All rights reserved.

Arrays IT 1033: Fundamentals of Programming

Transcription:

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 add 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

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.

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???