KEMBAR78
PST Arrays | PDF | Computer Engineering | Software Development
0% found this document useful (0 votes)
31 views21 pages

PST Arrays

An array is a collection of data items of the same type stored in consecutive memory locations, allowing random access via index numbers. Arrays can be single-dimensional or multi-dimensional, with single-dimensional arrays being the simplest form requiring one subscript for access. They are widely used for storing lists of values, performing matrix operations, and implementing various data structures and algorithms.

Uploaded by

soumyamkdesai40
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)
31 views21 pages

PST Arrays

An array is a collection of data items of the same type stored in consecutive memory locations, allowing random access via index numbers. Arrays can be single-dimensional or multi-dimensional, with single-dimensional arrays being the simplest form requiring one subscript for access. They are widely used for storing lists of values, performing matrix operations, and implementing various data structures and algorithms.

Uploaded by

soumyamkdesai40
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
You are on page 1/ 21

Array:-

• A collection of data items of the same data type which are stored in
consecutive memory location and referred by the common name is
known as Array.
• The array is the simplest data structure where each data element can be
randomly accessed by using its index number.
• An array name must be unique.
• It can be used to store the collection of primitive data types such as int,
char, float, etc.,
Types of Array:-
• Single Dimensional Array
• Multi Dimensional Array
Single Dimensional Array:-
• An array with n elements where each element is stored in consecutive
memory location is called as Single Dimensional Array or Linear Array
or One Dimensional Array.
• Single Dimensional Array is the simplest form of an array that requires
only one subscript to access an array element.
• Array must have been declared before it is used in the program.
Declaration of Single Dimensional Array
Syntax:-
data_type array_name[array_size];
Example:-
int marks[5];
Here, int is the data_type, marks are the array_name, and 5 is the array_size.
Initialization of Single Dimensional Array:-
The simplest way to initialize an array is by using the index of each
element. We can initialize each element of the array by using the index.
• marks[0]=80;//initialization of array
• marks[1]=60;
• marks[2]=70;
• marks[3]=85;
• marks[4]=75;
Memory Representation of 1D Array:-
• int value[5]={ 85, 98, 79, 88, 90};
Initialization of Single Dimensional Array:-
• Single Dimensional array can be initialized in two ways:-
a) At Compile Time
b) At Runtime
At Compile Time:-
• Initializing an array with size and initial values:-
int a[5]={10, 11, 12, 13, 14};
• Initializing an array without specifying size and initial values:-
int a[ ]={10, 11, 12, 13, 14};
• Partial array initialization:-
int a[5]= { 10,11};
At Runtime:-
void main()
{
int i;
int a[5];
printf(“Enter elements of an array:”);
for(i=0; i<5; i++)
{
scanf(“%d”, &a[i]);
}
printf(“Array Elements are:”);
for(i=0; i<5; i++)
{
printf(“%d”,a[i]);
}
Accessing Single Dimensional Array:-
• Each element of an array is accessed via an index or subscript.
• An index is the integer value specified within the square brackets.
• Single Dimensional Array will always have only one subscript/index.
• Example:-
int regno[5]={ 10, 11, 12, 13,14}
printf(“%d”, regno[0]);
printf(“%d”, regno[1]);
Example:-
#include<stdio.h>
#include<conio.h>
int main(){
int i=0;
int marks[5];
Output
marks[0]=80;
80
marks[1]=60;
60
marks[2]=70;
70
marks[3]=85;
85
marks[4]=75;
75
for(i=0;i<5;i++){
printf("%d \n",marks[i]);
}
return 0;
}
Example:-
#include<stdio.h> Output
int main(){ 20
30
int i=0; 40
int marks[5]={20,30,40,50,60}; 50
60
for(i=0;i<5;i++)
{
printf("%d \n",marks[i]);
}
return 0;
}
Multidimensional Array:-
• Multidimensional Array is defined as an array of arrays.
• Data in multidimensional arrays are stored in tabular form.
• Syntax:-
data_type array_name[size1][size2]…..[sizeN];
data_type: Type of data to be stored in the array.
array_name: Name of the array.
size1,size2,…..,sizeN: Size of the dimensions
Two dimensional array:-
int two_d[10][20];
Three dimensional array:-
int three_d[10][20][30];
Two Dimensional Array:-
• Two Dimensional Array is a fixed length, homogenous data type, row and column based data
structure which is used to store similar data type element in a row and column based structure.
• Two Dimensional Array is referred to as matrix or table.
• Declaration of Two Dimensional Array
Syntax:-
data_type array_name[exp1][exp2];
where
data type is any basic data type such as int, float, char, etc.
exp1 and exp2 are positive integers expression that indicate the number of array elements associated
with each subscript.
Example:-
char name[2][3];
A 2-D array called name of type char, 2 rows and 3 columns.
This array will contain 2 * 3 = 6 elements.
Data stored in Two Dimensional Array:-
Initialization of Two Dimensional Array:-
Example:-
#include <stdio.h>
int main () Output:-
{ a[0][0]: 0
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}}; a[0][1]: 0
a[1][0]: 1
int i, j; a[1][1]: 2
for ( i = 0; i < 5; i++ ) a[2][0]: 2
{ a[2][1]: 4
a[3][0]: 3
for ( j = 0; j < 2; j++ ) a[3][1]: 6
{ a[4][0]: 4
printf("a[%d][%d] = %d\n", i,j, a[i][j] ); a[4][1]: 8
}
}
return 0;
}
Three Dimensional Array
• A Three Dimensional Array or 3D array in C is a collection of two-
dimensional arrays. It can be visualized as multiple 2D arrays stacked
on top of each other.
• Declaration of Three-Dimensional Array
Syntax:
data_type array_name[x][y][z];
data_type: Type of data to be stored in each element.
array_name: name of the array
x: Number of 2D arrays.
y: Number of rows in each 2D array.
z: Number of columns in each 2D array.
Graphical Representation of Three-Dimensional Array of Size 3 x 3 x 3
Initialization of Three-Dimensional Array in C
• A 3D array in C can be initialized by using:
Initializer List
Loops
• Initialization of 3D Array using Initializer List
Method 1:
int x[2][3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23};
Method 2:
int x[2][3][4] = { { {0,1,2,3}, {4,5,6,7}, {8,9,10,11} }, {
{12,13,14,15}, {16,17,18,19}, {20,21,22,23} } };
• Initialization of 3D Array using Loops
Int x[2][3][4];
for (int i=0; i<2; i++)
{
for (int j=0; j<3; j++)
{
for (int k=0; k<4; k++)
{
x[i][j][k] = (some_value);
}
}
}
Accessing elements in Three-Dimensional
Array
• Syntax:
array_name[x][y][z]where,
x: Index of 2D array.
y: Index of that 2D array row.
z: Index of that 2D array column.
Example:-
#include <stdio.h>
int main(void)
{ Output
int x[2][3][2] = { { { 0, 1 }, { 2, 3 }, { 4, 5 } }, Element at x[0][0][0] = 0
{ { 6, 7 }, { 8, 9 }, { 10, 11 } } }; Element at x[0][0][1] = 1
Element at x[0][1][0] = 2
for (int i = 0; i < 2; ++i)
Element at x[0][1][1] = 3
Element at x[0][2][0] = 4
{
Element at x[0][2][1] = 5
for (int j = 0; j < 3; ++j) Element at x[1][0][0] = 6
{ Element at x[1][0][1] = 7
for (int k = 0; k < 2; ++k) { Element at x[1][1][0] = 8
printf("Element at x[%i][%i][%i] = %d\n", i, Element at x[1][1][1] = 9
j, k, x[i][j][k]);
Element at x[1][2][0] = 10
Element at x[1][2][1] = 11
}
}
}
return (0);
Advantages and Disadvantages of Arrays:-
Advantages Disadvantages
It represents multiple data items of the The number of elements to be stored in
same type using a single name. an array should be known in advance.
The elements can be accessed randomly An array is a static structure.
by using the index number or subscript.
No memory overflow or shortage of Insertion and deletion are quite difficult
memory in arrays. in arrays.
The other data structures like linked lists, Allocating more memory then the
stacks, queues, trees, graphs etc can be requirement leads to wastage of memory
implemented using arrays. space.
Two dimensional arrays are used to
represent matrices.
Applications of Arrays:-
• Arrays are used to store list of values.
• Arrays are used to Perform Matrix Operations.
• Arrays are used to implement Searching and sorting algorithm.
• Arrays are used to implement other Data structure like Stacks and
queues.
• Arrays help to maintain large data under a single variable name. This a
confusion of using multiple variables.

You might also like