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

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

CSE101-lec#19. Array searching and sorting techniques. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

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

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

'C' Programming Language

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

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

Procedural Programming

Classification s of Data Structures

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

UNIT III ARRAYS AND STRINGS

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos.

Arrays in C. By Mrs. Manisha Kuveskar.

Government Polytechnic Muzaffarpur.

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.

Fundamental of Programming (C)

Write a C program using arrays and structure

How to declare an array in C?

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

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

IV Unit Second Part STRUCTURES

Array. Arrays. Declaring Arrays. Using Arrays

Module 6: Array in C

PROGRAMMING IN C LAB MANUAL FOR DIPLOMA IN ECE/EEE

Chapter 6. Arrays. Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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

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

Unit 3 Decision making, Looping and Arrays

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

While single variables have many uses, there are times when we wish to store multiple related values. For these we may wish to use an array.

Computers Programming Course 12. Iulian Năstac

Lecture 04 FUNCTIONS AND ARRAYS

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen

AMCAT Automata Coding Sample Questions And Answers

Arrays a kind of data structure that can store a fixedsize sequential collection of elements of the same type. An array is used to store a collection

CSCI 2132 Software Development. Lecture 18: Functions

Computer Science & Engineering 150A Problem Solving Using Computers

C Programming Lecture V

Structured programming

Sorting & Searching. Hours: 10. Marks: 16

Multi-Dimensional arrays

C Arrays. Group of consecutive memory locations Same name and type. Array name + position number. Array elements are like normal variables

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

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

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

APS105. Collecting Elements 10/20/2013. Declaring an Array in C. How to collect elements of the same type? Arrays. General form: Example:

Programming for Electrical and Computer Engineers. Pointers and Arrays

C Programming Language

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

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

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

Multiple-Subscripted Arrays

EECE.2160: ECE Application Programming Fall 2017

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

(Refer Slide Time 01:41 min)

Arrays. Dr. Madhumita Sengupta. Assistant Professor IIIT Kalyani

Computer Programming

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

Dynamic memory allocation

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

Q1 (15) Q2 (15) Q3 (15) Q4 (15) Total (60)

ARRAYS(II Unit Part II)

Principles of Programming. Chapter 6: Arrays

Array Initialization

ONE DIMENSIONAL ARRAYS

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

F.E. Sem. II. Structured Programming Approach

Worksheet 11 Multi Dimensional Array and String

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

Two Dimensional Array - An array with a multiple indexs.

Fundamental of Programming (C)

Q1 (15) Q2 (15) Q3 (15) Q4 (15) Total (60)

Fundamentals of Computer Programming Using C

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

Computer Programming Lecture 14 Arrays (Part 2)

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

MA 511: Computer Programming Lecture 2: Partha Sarathi Mandal

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

UIC. C Programming Primer. Bharathidasan University

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

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

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

Contents ARRAYS. Introduction:

Decision Making and Loops

Content. In this chapter, you will learn:

Fundamentals of Programming

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

BITG 1113: Array (Part 1) LECTURE 8

Chapter 7 Functions. Now consider a more advanced example:

Loops. CSCI 112: Programming in C

Programming in C Lab

C Arrays Pearson Education, Inc. All rights reserved.

Single Dimension Arrays

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions

EENG 212 Lab 2. Recursive Functions

Arrays and Applications

Objectives of This Chapter

Question Bank (SPA SEM II)

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

Transcription:

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

Outline Defining and processing 1D array 2D array Applications of arrays

1-D array A single- dimensional or 1-D array consists of a fixed number of elements of the same data type organized as a single linear sequence. The elements of a single dimensional array can be accessed by using a single subscript The arrays we have studied till now were 1D array or linear array. Example: int a[n];

#include<stdio.h> void main() { int avg, sum=0, i; int marks[30]; for(i=0;i<=29;i++) { printf( enter numbers ); scanf( %d,&marks[i]); /*array declaration*/ /*store data in array*/ } for(i=0;i<=29;i++) { sum = sum + marks[i]; /*read data from an array*/ } avg = sum/30; printf( Average marks= %d, avg); } Program to display the average of elements in 1D array

Multiple-Subscripted Arrays Multiple subscripted arrays Tables with rows and columns (m by n array) Like matrices: specify row, then column int a[rows][column]; Row 0 Row 1 Row 2 Column 0 a[ 0 0 ][ 0 ] a[ 1 ][ 0 ] a[ 2 ][ 0 ] Column 1 Column 2 Column 3 a[ 0 ][ 1 ] a[ 0 ][ 2 ] a[ 0 ][ 3 ] a[ 1 ][ 1 ] a[ 1 ][ 2 ] a[ 1 ][ 3 ] a[ 2 ][ 1 ] a[ 2 ][ 2 ] a[ 2 ][ 3 ] 5 Array name Column subscript Row subscript

Multiple-Subscripted Arrays Initialization int a[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } }; Initializers grouped by row in braces If not enough, unspecified elements set to zero Int a[ 2 ][ 2 ] = { { 1 }, { 3, 4 } }; Referencing elements Specify row, then column printf( "%d", a[ 0 ][ 1 ] ); 1 2 3 4 1 0 3 4

#include<stdio.h> void main() { int a[4][3], i, j; for(i=0; i<4; i++) { // for loop for rows for(j=0; j<3;j++) { // for loop for columns printf( enter the value of a[%d][%d]:, i, j); scanf( %d, &a[i][j]); } //end for columns } //end for rows printf( elements of 2D matrix are ); for(i=0; i<4; i++) { for(j=0;j<3;j++) { print( %d\t, a[i][j]); } //end for printf( \n ); } //end for } //end main Program to display 2D array

enter the value of a[0][1] :1 enter the value of a[0][1] :2 enter the value of a[0][1] :3 enter the value of a[0][1] :4 enter the value of a[0][1] :5 enter the value of a[0][1] :6 enter the value of a[0][1] :7 enter the value of a[0][1] :8 enter the value of a[0][1] :9 Element of 2D matrix are: 1 2 3 4 5 6 7 8 9

Operations on arrays Insertion of element into an array Deletion of element from an array

#include <stdio.h> #include <conio.h> int main() { int array[100], position, c, n, value; printf("enter number of elements in array:\n"); scanf("%d", &n); printf("enter %d elements:\n", n); for (c = 0; c < n; c++) { scanf("%d", &array[c]); } printf("enter the location where you wish to insert an element:\n"); scanf("%d", &position); printf("enter the value to insert:\n"); scanf("%d", &value); Program to insert an element into an array for (c = n - 1; c >= position - 1; c--) { array[c+1] = array[c]; } array[position-1] = value; } printf("resultant array is:\n"); for (c = 0; c <= n; c++) { printf("%d\n", array[c]); }

Enter number of elements in array: 4 Enter 4 elements 2 45 66 33 Enter the location where you wish to insert an element : 2 Enter the value to insert : 99 Resultant array is : 2 99 45 66 33

#include <stdio.h> int main() { int array[100], position, c, n; printf("enter number of elements in array\n"); scanf("%d", &n); printf("enter %d elements\n", n); for (c = 0; c < n; c++) { scanf("%d", &array[c]); } printf("enter the location where you wish to delete from an array\n"); scanf("%d", &position); for (c = position-1; c < n; c++) { array[c] = array[c+1]; } Program to delete an element from an array } printf("resultant array is\n"); for (c = 0; c < n-1; c++) { printf("%d\n", array[c]); }

Enter number of elements in array: 4 Enter 4 elements 2 45 66 33 Enter the location where you wish to to delete from an array : 2 Resultant array is : 2 66 33

Application Of Array : Stores Elements of Same Data Type Array is used to store the number of elements that are of same data type. Eg: int students[30]; array of marks of five subjects for single student. float marks[5]; array of marks of five subjects for 30 students. float marks[30][5] Similarly if we declare the character array then it can hold only character. So in short character array can store character variables while floating array stores only floating numbers.

Array Used for Maintaining multiple variable names using single name Suppose we need to store 5 roll numbers of students then without declaration of array we need to declare following - int roll1,roll2,roll3,roll4,roll5; 1. Now in order to get roll number of first student we need to access roll1. 2. Guess if we need to store roll numbers of 100 students then what will be the procedure. 3. Maintaining all the variables and remembering all these things is very difficult. So we are using array which can store multiple values and we have to remember just single variable name.

Array Can be Used for Sorting Elements We can store elements to be sorted in an array and then by using different sorting technique we can sort the elements. Different Sorting Techniques are : 1. Bubble Sort 2. Insertion Sort 3. Selection Sort

Array Can Perform Matrix Operation Matrix operations can be performed using the array. We can use 2-D array To store the matrix. To perform all mathematical manipulations on matrix. Matrix can be multi-dimensional.

Some classic daily examples 1D Running Distance [D] 2D Long Jump Distance, Height [D,H] 3D High Jump Distance, Height, Roll [D,H,R]

Next Lecture Finding my stuffs from a mess?? Searching Technique cse101@lpu.co.in