0% found this document useful (0 votes)
60 views76 pages

Module1 DATA STRUCTURE

This is Data Structure Notes

Uploaded by

protyson203
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views76 pages

Module1 DATA STRUCTURE

This is Data Structure Notes

Uploaded by

protyson203
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DATA STRUCTURE

Introduction to Data Structure


Computer is an electronic machine which is used for data processing and
manipulation.
When programmer collects such type of data for processing, he would
require to store all of them in computers main memory.
In order to make how computer work we need to know
Representation of data in computer.
Accessing of data.
How to solve problem step by step.
For doing all of this task we used Data Structure
What is Data
Structure

A data structure is a
specialized format for
organizing, processing,
retrieving and storing
data.
In computer
programming, a data
structure may be
selected or designed to
store data for the
purpose of working on it
with various algorithms.
What is Data Structure
Data Structure can be defined as the group of data elements which provides an efficient
way of storing and organizing data in the computer so that it can be used efficiently.
examples of Data Structures are arrays, Linked List, Stack, Queue, etc.
Data Structures are widely used in almost every aspect of Computer Science i.e.
Operating System, Compiler Design, Artificial intelligence, Graphics and many more.
Data Structures are the main part of many computer science algorithms as they enable
the programmers to handle the data in an efficient way.
It plays a vital role in enhancing the performance of a software or a program, as the main
function of the software is to store and retrieve the user’s data as fast as possible
Data Structure
◦ A data structure is a particular way of organizing data in a computer so that it can be used effectively.

◦ For example, we can store a list of items having the same data-type using the array data structure.
The representation of particular data structure in the main memory of a computer is called as
storage structure.
The storage structure representation in auxiliary memory is called as file structure.
It is define as the way of storing and manipulating data in organized form so that it can be
used efficiently
Data Structure mainly specifies the following four things:
1)organization of data 2)accessing method 3)degree of associativity 4) processing
alternative for information
Algorithm + Data Structure = Program
Data Structure study Covers the following points
1) Amount of memory require to store
2) Amount of time require to process
3) Representation of data in memory
4) Operations performed on data
Types Of DS

The DS are divided into


two types:
1) Primitive
2) Non primitive
Non primitive divided
into two type
1) Linear DS
2) Non linear DS
DATA TYPES
A particular kind of data item, as defined by the values it can take, the
Programming language used, or the operations that can be performed on it.
◦ Primitive Data Structure
◦ Primitive Data Structure are basic structure and directly operated upon by machine
instructions.
◦ Primitive data structures have different representations on different computers.
◦ Integers, floats, character and pointers are example of primitive data structures.
◦ These data types are available in most programming languages as built in type.
Integer: It is a data type which allows all values without fraction part. We can used it for
whole numbers.
Float: It is a data type which is use for storing fraction numbers.
Character: It is a data type which is used for character values.
Pointer: A variable that hold memory address of another variable are called pointer.
Non Primitive Data Type
◦ These are more sophisticated data structures.
◦ These are derived from primitive data structure.
◦ The non – primitive data structures emphasize structuring of a group of homogeneous or
heterogeneous data items.
◦ Example of non – primitive data types are Array, List, and File etc.
◦ A non – primitive data type is further divided into Linear and non – Linear data structure.
Array: An array is a fixed size sequenced collection of elements of the same data type.
List: An ordered set containing variable number of elements is called as List.
File: A file is a collection of logically related information. It can be viewed as a large list of
records consisting of various fields.
Linear Data Structures
▪ A linear data structure simply means that it is a storage format
of the data in the memory in which the data are arranged in
contiguous blocks of memory.
▪ Example is the array of characters it represented by one
character after another.
▪ In the linear data structure, member elements form a
sequence in the storage.
▪ There are two ways to represent a linear data structure in
memory.
static memory allocation
dynamic memory allocation
The possible operations on the linear data structure are:
1) Traversing 2) Insertion 3) Deletion 4) searching 5) sorting
6) merging
◦ Example of Linear data structure are Stack and
Queue
Stack
◦ Stack is a data structure in which insertion and
deletion operations are performed at one end
only.
◦ The insertion operation is referred to as ‘PUSH’
and deletion is referred as ‘POP’ operation
◦ Stack is also called as Last In First Out (LIFO) data
structure.
Queue
◦ The data structure which permits the insertion at
one and deletion at another end, known as
Queue.
◦ End at which deletion is occurs is known as
FRONT end and another end at which insertion
occurs is known as REAR end.
◦ Queue is also called as First In First Out (FIFO)
Non-Linear Data Structure
◦ Non linear DS are those data structure in which data items are not
arranged in a sequence.
◦ Example on Non Linear DS are Tree and Graph.
TREE
◦ A Tree can be defined as finite data items (nodes) in which data
items are arranged in branches and sub branches
◦ Tree represent the hierarchical relationship between various
elements
Components of Graph ◦ Tree consist of nodes connected by edge, the represented by
circle and edge lives connecting to circle.
Graph
◦ Graph is collection of nodes (information) and connecting edges
(Logical relation) between nodes.
◦ A tree can be viewed as restricted graph
◦ Graph have many types: 1) Simple graph 2) Mixed graph 3) Multi
graph 4) Directed graph 5) Un-directed graph
Difference Between Linear and Nonlinear Data Structure

Linear Data Structure Non – Linear Data Structure

◦ Every item is related to its previous and ◦ Every item is attached with many other
next item. items.
◦ Data is arranged in linear sequence. ◦ Data is not arranged in sequence.
◦ Data items can be traversed in a single ◦ Data cannot be traversed in a single run.
run ◦ E.g. Tree, Graph
◦ E.g. Array, Stacks, Linked list, Queue ◦ Implementation is difficult.
◦ Implementation is easy.
Operation on Data Structures
Design of efficient data structure must take operations to be performed on the DS into account.
The most commonly used operations on DS are broadly categorized into following types

1. Create: This operation results in reserving memory for program elements. This can be done by
declaration statement Creation of DS may take place either during compile-time or run-time.
2. Selection: This operation deals with accessing a particular data within a data structure.
3. Updation: It updates or modifies the data in the data structure.
4. Searching: It finds the presence of desired data item in the list of data items, it may also find
locations of all elements that satisfy certain conditions.
5. Sorting: This is a process of arranging all data items in a DS in particular order, for example
either ascending order or in descending order.
6. Splitting: It is a process of partitioning single list to multiple list.
7. Merging: It is a process of combining data items of two different sorted list into single sorted
list.
8. Traversing: It is a process of visiting each and every node of a list in systematic manner.
What are Arrays?
Array is a container which can
hold a fix number of items and
these items should be of the same
type.
Most of the data structures make
use of arrays to implement their
algorithms.
•Following are the important terms
to understand the concept of
Array.
Element − Each item stored
in an array is called an element.
Index − Each location of an
element in an array has a
numerical index, which is used to
1. An array is a container of elements. identify the element.
2. Elements have a specific value and data type, like "ABC", TRUE or FALSE,
etc.
3. Each element also has its own index, which is used to access the element.
• Elements are stored at
contiguous memory locations.
• An index is always less than the
total number of array items.
• In terms of syntax, any variable
that is declared as an array can
store multiple values.
• Almost all languages have the
same comprehension of arrays
but have different ways of
declaring and initializing them.
• However, three parts will
always remain common in all
the initializations, i.e., array
name, elements, and the data
type of elements.

•Array name: necessary for easy reference to the collection of elements


•Data Type: necessary for type checking and data integrity
•Elements: these are the data values present in an array
How to access a
specific array
value?
You can access any array item by
using its index

Syntax
arrayName[indexNum]

Example
balance[1]

Here, we have accessed the second value of the array using its index, which is
1. The output of this will be 200, which is basically the second value of the
balance array.
◦ Array Representation
◦ Arrays can be declared in various ways in different languages. For illustration, let's take C array
declaration.
In ADT Minimum required functionality is given

Show user only essential information and hide the implementation

Abstract/logical view Implementation View

8GB RAM Class smartview{


Snapdragon 2.2GHZ processor Private:
6.5 inch LCD screen Int ram size;
Dual Camera String processor name;
Android 8.0 Float screen size;
Int camera count;
String android version;
Public:
Call Void call()
Text Void text()
Photo Void photo()
Video Void video()
}
Characteristics of an Algorithm
Not all procedures can be called an algorithm. An algorithm should have the following characteristics −

● Unambiguous − Algorithm should be clear and unambiguous. Each of its steps (or phases), and
their inputs/outputs should be clear and must lead to only one meaning.
● Input − An algorithm should have 0 or more well-defined inputs.
● Output − An algorithm should have 1 or more well-defined outputs, and should match the
desired output.
● Finiteness − Algorithms must terminate after a finite number of steps.
● Feasibility − Should be feasible with the available resources.
● Independent − An algorithm should have step-by-step directions, which should be independent
of any programming code.
How to Write an Algorithm?
There are no well-defined standards for writing algorithms. Rather, it is problem and resource dependent.
Algorithms are never written to support a particular programming code.

As we know that all programming languages share basic code constructs like loops (do, for, while),
flow-control (if-else), etc. These common constructs can be used to write an algorithm.

We write algorithms in a step-by-step manner, but it is not always the case. Algorithm writing is a process
and is executed after the problem domain is well-defined. That is, we should know the problem domain,
for which we are designing a solution.
Problem − Design an algorithm to add two numbers and display the
result.

Step 1 − START
Step 2 − declare three integers a, b & c
Step 3 − define values of a & b
Step 4 − add values of a & b
Step 5 − store output of step 4 to c
Step 6 − print c
Step 7 − STOP

Algorithms tell the programmers how to code the program.


Alternatively, the algorithm can be written as −

Step 1 − START ADD


Step 2 − get values of a & b
Step 3 − c ← a + b
Step 4 − display c
Step 5 − STOP
Algorithm Analysis
Efficiency of an algorithm can be analyzed at two different stages, before implementation and after
implementation. They are the following −

● A Priori Analysis − This is a theoretical analysis of an algorithm. Efficiency of an algorithm is


measured by assuming that all other factors, for example, processor speed, are constant and
have no effect on the implementation.
● A Posterior Analysis − This is an empirical analysis of an algorithm. The selected algorithm is
implemented using programming language. This is then executed on target computer
machine. In this analysis, actual statistics like running time and space required, are collected.

We shall learn about a priori algorithm analysis. Algorithm analysis deals with the execution or running
time of various operations involved. The running time of an operation can be defined as the number of
computer instructions executed per operation.
Asymptotic analysis of an algorithm refers to defining the mathematical boundation/framing of its run-time
performance. Using asymptotic analysis, we can very well conclude the best case, average case, and worst
case scenario of an algorithm.

Asymptotic analysis is input bound i.e., if there's no input to the algorithm, it is concluded to work in a
constant time. Other than the "input" all other factors are considered constant.

Asymptotic analysis refers to computing the running time of any operation in mathematical units of
computation. For example, the running time of one operation is computed as f(n) and may be for another
operation it is computed as g(n2). This means the first operation running time will increase linearly with
the increase in n and the running time of the second operation will increase exponentially when n
increases. Similarly, the running time of both operations will be nearly the same if n is significantly small.

Usually, the time required by an algorithm falls under three types −

● Best Case − Minimum time required for program execution.


● Average Case − Average time required for program execution.
● Worst Case − Maximum time required for program execution.
Big Oh Notation, Ο
The notation Ο(n) is the formal way to express the upper bound of an algorithm's running
time. It measures the worst case time complexity or the longest amount of time an
algorithm can possibly take to complete.

For example, for a function f(n)

Ο(f(n)) = { g(n) : there exists c > 0 and n0 such that f(n) ≤ c.g(n) for all n > n0. }
Omega Notation, Ω
The notation Ω(n) is the formal way to express the lower bound of an algorithm's running time. It
measures the best case time complexity or the best amount of time an algorithm can possibly take
to complete.

For example, for a function f(n)

Ω(f(n)) ≥ { g(n) : there exists c > 0 and n0 such that g(n) ≤ c.f(n) for all n > n0. }
Theta Notation, θ
The notation θ(n) is the formal way to express both the lower bound and the upper
bound of an algorithm's running time. It is represented as follows −

θ(f(n)) = { g(n) if and only if g(n) = Ο(f(n)) and g(n) = Ω(f(n)) for all n > n0. }
Following is a list of some common asymptotic notations

constant − Ο(1)

logarithmic − Ο(log n)

linear − Ο(n)

n log n − Ο(n log n)

quadratic − Ο(n2)

cubic − Ο(n3)

polynomial − nΟ(1)

exponential − 2Ο(n)
Classification of Data Structures
Primitive Data Structures

Primitive Data Structures are the data structures consisting of the numbers and
the characters that come in-built into programs.

1. These data structures can be operated directly by machine-level instructions.


2. Basic data types like Integer, Float, Character, and Boolean come under the
Primitive Data Structures.
3. These data types are also called Simple data types, as they contain
characters that can't be divided further
Non-Primitive Data Structures

1. Non-Primitive Data Structures are those data structures derived from Primitive
Data Structures.
2. These data structures can't be operated directly by machine-level instructions.
3. The focus of these data structures is on forming a set of data elements that is
either homogeneous (same data type) or heterogeneous (different data types).
4. Based on the structure and arrangement of data, we can divide these data
structures into two sub-categories -
a. Linear Data Structures
b. Non-Linear Data Structures
Linear Data Structures

A data structure that preserves a linear connection among its data elements is known as a Linear Data Structure

The arrangement of the data is done linearly, where each element consists of the successors and predecessors
except the first and the last data element.

1. Static Data Structures: The data structures having a fixed size are known as Static Data Structures. The
memory for these data structures is allocated at the compiler time, and their size cannot be changed by the
user after being compiled; however, the data stored in them can be altered.
The Array is the best example of the Static Data Structure as they have a fixed size, and its data can be
modified later.
2. Dynamic Data Structures: The data structures having a dynamic size are known as Dynamic Data
Structures. The memory of these data structures is allocated at the run time, and their size varies during the
run time of the code. Moreover, the user can change the size as well as the data elements stored in these
data structures at the run time of the code.
Linked Lists, Stacks, and Queues are common examples of dynamic data structures
Linked Lists

A Linked List is another example of a linear data structure used to store a collection of data elements dynamically.

Data elements are represented by the Nodes, connected using links or pointers.

Each node contains two fields, the information field consists of the actual data, and the pointer field consists of the address of
the subsequent nodes in the list.

The pointer of the last node of the linked list consists of a null pointer, as it points to nothing. The user can dynamically adjust
the size of a Linked List as per the requirements.
Linked Lists can be classified into different types:

a. Singly Linked List: A Singly Linked List is the most common type of Linked List. Each node has data
and a pointer field containing an address to the next node.
b. Doubly Linked List: A Doubly Linked List consists of an information field and two pointer fields. The
information field contains the data. The first pointer field contains an address of the previous node,
whereas another pointer field contains a reference to the next node. Thus, we can go in both directions
(backward as well as forward).
c. Circular Linked List: The Circular Linked List is similar to the Singly Linked List. The only key difference
is that the last node contains the address of the first node, forming a circular loop in the Circular Linked
List.

Applications of Linked Lists:

a. Circular Linked List is also helpful in a Slide Show where a user requires to go back to the first slide after
the last slide is presented.
b. Doubly Linked List is utilized to implement forward and backward buttons in a browser to move forward
and backward in the opened pages of a website.
Stacks

A Stack is a Linear Data Structure that follows the LIFO (Last In, First Out) principle that allows operations like
insertion and deletion from one end of the Stack, i.e., Top.

Stacks can be implemented with the help of contiguous memory, an Array, and non-contiguous memory, a
Linked List.

Real-life examples of Stacks are piles of books, a deck of cards, piles of money, and many more.
The primary operations in the Stack
are as follows:

a. Push: Operation to insert a new


element in the Stack is termed
as Push Operation.
b. Pop: Operation to remove or
delete elements from the Stack
is termed as Pop Operation.
Queues

A Queue is a linear data structure similar to a Stack with some limitations on the insertion and deletion of the elements.

The insertion of an element in a Queue is done at one end, and the removal is done at another or opposite end.

Queue data structure follows FIFO (First In, First Out) principle to manipulate the data elements.

Some real-life examples of Queues are a line at the ticket counter, an escalator, a car wash, and many more.
Primary operations of the Queue:

a. Enqueue: The insertion or Addition of some data elements to the Queue


is called Enqueue.
b. Dequeue: Deleting or removing data elements from the Queue is termed
Dequeue.
Abstract Data Type
The operations in data structure include:

a. A high level of abstractions like addition or deletion of an item from a list.


b. Searching and sorting an item in a list.
c. Accessing the highest priority item in a list.

Whenever the data structure does such operations, it is known as an Abstract Data Type (ADT).

We can define it as a set of data elements along with the operations on the data. The term "abstract"
refers to the fact that the data and the fundamental operations defined on it are being studied
independently of their implementation. It includes what we can do with the data, not how we can do
it.

An ADI implementation contains a storage structure in order to store the data elements and
algorithms for fundamental operation. All the data structures, like an array, linked list, queue, stack,
etc., are examples of ADT.
Array in Data Structure
● Arrays are defined as the collection of similar types of data items stored at contiguous memory
locations.
● In C programming, they are the derived data types that can store the primitive type of data such as int, char,
double, float, etc.
● For example, if we want to store the marks of a student in 6 subjects
● we can define an array that can store the marks in each subject at the contiguous memory locations.

Arrays can be classified into different types:

a. One-Dimensional Array: An Array with only one row of data elements is known as a One-Dimensional
Array. It is stored in ascending storage location.
b. Two-Dimensional Array: An Array consisting of multiple rows and columns of data elements is called a
Two-Dimensional Array. It is also known as a Matrix.
c. Multidimensional Array: We can define Multidimensional Array as an Array of Arrays. Multidimensional
Arrays are not bounded to two indices or two dimensions as they can include as many indices are per the
need.
Properties of array

○ Each element in an array is of the same data type and carries the same size that
is 4 bytes.
○ Elements in the array are stored at contiguous memory locations from which the
first element is stored at the smallest memory location.
○ Elements of the array can be randomly accessed since we can calculate the
address of each element of the array with the given base address and the size
of the data element.
Representation of an array

○ Index starts with 0.


○ The array's length is 10, which means we can store 10 elements.
○ Each element in the array can be accessed via its index.
Why are arrays required?
Arrays are useful because -

○ Sorting and searching a value in an array is easier.


○ Arrays are best to process multiple values quickly and easily.
○ Arrays are good for storing multiple values in a single variable - In computer
programming, most cases require storing a large number of data of a similar type. To
store such an amount of data, we need to define a large number of variables. It would
be very difficult to remember the names of all the variables while writing the programs.
Instead of naming all the variables with a different name, it is better to define an array
and store all the elements into it.
Memory allocation of an array

All the data elements of an array are stored at contiguous locations in the main memory. The first
element in the main memory represents the base address. Each element of the array is represented by
proper indexing.

We can define the indexing of an array in the below ways -

1. 0 (zero-based indexing): The first element of the array will be arr[0].


2. 1 (one-based indexing): The first element of the array will be arr[1].
3. n (n - based indexing): The first element of the array can reside at any random index number.
The memory allocation of an array arr of
size 5. The array follows a 0-based
indexing approach. The base address of
the array is 100 bytes. It is the address
of arr[0]. Here, the size of the data type
used is 4 bytes; therefore, each element
will take 4 bytes in the memory
How to access an element from the array?
We required the information given below to access any random element from the array -

○ Base Address of the array.


○ Size of an element in bytes.
○ Type of indexing, array follows.

The formula to calculate the address to access an array element -

1. Byte address of element A[i] = base address + i* ( size of data type)


Calculate address of i=2, having Base address (BA) = 100 and size of data type = 4 bytes, find the location of i=2.

= 100 + 2 x [4]

= 100 + 8

= 108
Basic operations

○ Traversal - This operation is used to print the elements of the array.


○ Insertion - It is used to add an element at a particular index.
○ Deletion - It is used to delete an element from a particular index.
○ Search - It is used to search an element using the given index or by the value.
○ Update - It updates an element at a particular index.
Traversal operation
This operation is performed to traverse through the array elements. It prints all array elements one after another.

1. #include <stdio.h>
2. void main()
3. {
4. int Arr[5] = {18, 30, 15, 70, 12};
5. int i;
6. printf("Elements of the array are:\n");
7. for(i = 0; i<5; i++)
8. {
9. printf(" %d, ", Arr[i]);
10. }
11. }
Insertion operation
1. x = 50; // element to be inserted
This operation is performed to insert one or more elements into the 2. pos = 4;
array. An element can be added at the beginning, end, or at any index 3. for (i = n-1; i >= pos-1; i--)
of the array.
4. arr[i] = arr[i + 1];
1. #include <stdio.h> 5. arr[pos - 1] = x;
2. int main() 6. n++;
3. { 7. printf("Array elements after insertion\n");
4. int arr[20] = { 18, 30, 15, 70, 12 }; 8. for (i = 0; i < n; i++)
5. int i, x, pos, n = 5; 9. printf("%d ", arr[i]);
6. printf("Array elements before insertion\n"); 10. printf("\n");
7. for (i = 0; i < n; i++) 11. return 0;
8. printf("%d ", arr[i]);
9. printf("\n");
Deletion operation

As the name implies, this operation removes an element from the array and then reorganizes all of the array elements.
1. #include <stdio.h> 1. while( j < n) {
2. void main() { 2. arr[j+1] = arr[j];
3. int arr[] = {18, 30, 15, 70, 12}; 3. }
4. int delete = 12 , n = 5; 4.
5. int i, j; 5. n = n -1;
6. 6.
7. printf("Given array elements are :\n"); 7. printf("\nElements of array after deletion:\n");
8. for(i = 0; i<n; i++) { 8.
9. printf("%d, ", arr[i]); 9. for(i = 0; i<n; i++) {
10. } 10. printf("arr[%d] = %d, ", i, arr[i]);
11. if(delete==a[i]) 11. }
12. j = i; 12. }
Search operation

This operation is performed to search an element in the array based on the value or index.

1. #include <stdio.h> 1. while( i < 5){


2.
2. if( arr[i] == item ) {
3. void main() {
3. break;
4. int arr[5] = {18, 30, 15, 70, 12}; 4. }
5. int item = 70, i, j=0 ; 5.
6. 6. i= i + 1;
7. printf("Given array elements are :\n");
7. }
8.
8.
9. for(i = 0; i<5; i++) { 9. printf("\nElement %d is found at
10. printf("%d, ",arr[i]);
%d position", item, j+1);
11. } 10. }
12. printf("\nElement to be searched = %d", item);
Update operation

This operation is performed to update an existing array element located at the given
index.
1. #include <stdio.h>
2.
3. void main() {
4. int arr[5] = {18, 30, 15, 70, 12};
5. int item = 50, i, pos = 3;
6.
7. printf("Given array elements are :\n");
8.
9. for(i = 0; i<5; i++) {
10. printf("arr[%d] = %d, ", i, arr[i]);
11. }
12.
13. arr[pos-1] = item;
14. printf("\nArray elements after updation :\n");
Advantages of Array
○ Array provides the single name for the group of variables of the same type. Therefore, it is easy to
remember the name of all the elements of an array.
○ Traversing an array is a very simple process; we just need to increment the base address of the array in
order to visit each element one by one.
○ Any element in the array can be directly accessed by using the index.

Disadvantages of Array
○ Array is homogenous. It means that the elements with similar data type can be stored in it.
○ In array, there is static memory allocation that is size of an array cannot be altered.
○ There will be wastage of memory if we store less number of elements than the declared size.
2D Array
2D array can be defined as an array of arrays. The 2D array is organized as matrices which can be
represented as the collection of rows and columns.

However, 2D arrays are created to implement a relational database look alike data structure. It provides
ease of holding bulk of data at once which can be passed to any number of functions wherever required

How to declare 2D Array


The syntax of declaring two dimensional array is very much similar to that of a one dimensional array,
given as follows.

1. int arr[max_rows][max_columns];
How do we access data in a 2D array
Due to the fact that the elements of 2D arrays can be random accessed. Similar to one dimensional arrays, we can access
the individual cells in a 2D array by using the indices of the cells. There are two indices attached to a particular cell, one is
its row number while the other is its column number.

However, we can store the value stored in any particular cell of a 2D array to some variable x by using the following syntax.

1. int x = a[i][j];

where i and j is the row and column number of the cell respectively.

We can assign each cell of a 2D array to 0 by using the following code:

1. for ( int i=0; i<n ;i++)


2. {
3. for (int j=0; j<n; j++)
4. {
5. a[i][j] = 0;
6. }
7. }
Initializing 2D Arrays 1. #include <stdio.h>
2. void main ()
when we declare and initialize one dimensional array in
C programming simultaneously, we don't need to specify 3. {
the size of the array. However this will not work with 2D 4. int arr[3][3],i,j;
arrays. We will have to define at least the second 5. Printf(“Enter the elements”);
dimension of the array.
6. for (i=0;i<3;i++)
The syntax to declare and initialize the 2D array is given 7. {
as follows. 8. for (j=0;j<3;j++)

1. int arr[2][2] = {0,1,2,3}; 9. {


10. scanf("%d",&arr[i][j]);
The number of elements that can be present in a 2D 11. }
array will always be equal to (number of rows * number 12. }
of columns).
13.
Thank You

You might also like