0% found this document useful (0 votes)
21 views22 pages

DS Unit1 Arrays

The document discusses arrays, which are a collection of elements of the same data type stored in contiguous memory locations. It describes one-dimensional and multi-dimensional arrays, how to declare and initialize arrays, perform operations like traversing, inserting, deleting and sorting elements, and how arrays are stored in memory. Multidimensional arrays like 2D and 3D arrays are collections of arrays organized in rows and columns. The memory address of an element is calculated based on its indices and the base address and word size of the array.

Uploaded by

namansehgal3006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
21 views22 pages

DS Unit1 Arrays

The document discusses arrays, which are a collection of elements of the same data type stored in contiguous memory locations. It describes one-dimensional and multi-dimensional arrays, how to declare and initialize arrays, perform operations like traversing, inserting, deleting and sorting elements, and how arrays are stored in memory. Multidimensional arrays like 2D and 3D arrays are collections of arrays organized in rows and columns. The memory address of an element is calculated based on its indices and the base address and word size of the array.

Uploaded by

namansehgal3006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 22

Arrays

Data Structures
Dr. Nivedita Palia
• Introduction of Arrays
• An array is a fixed-size sequential collection of elements of same data type in
continuous memory location.
• The elements of an array are referenced by an index (also known as subscript).

• Types of Arrays: one dimensional and multidimensional


One- Dimensional Arrays
A list of items can be given one variable name using only one subscript and such a
variable is called a one- dimensional array.

Array Declaration: data-type name[size];

Declaration Examples:
float height[50]; int group[10]; char name[10];
Storing values in an array

Compile Time Initialization

Run Time Initialization


Calculating Length of an Array

Consider the linear arrays AAA(5:50) and B(-5:10). Find the number of elements
in each array.
Memory Representation of Linear Array

Q1 Consider a linear array A[5:50]. Suppose BA(A)= 300 and w=4 words per memory
cell for array A. Find the address of A[15] and A[55].
Operations on Arrays

1. Traversing an array
2. Inserting an element in an array
3. Searching an element in an array
4. Deleting an element from an array
5. Merging two arrays
6. Sorting an array in ascending or descending order
Traversing Linear Arrays
Accessing and processing each elements of an array exactly once.
Example: Print an array, count the number of elements in an array

Program for Array Traversal

Q1 Write a program to find the sum


and average of array elements .
Q2 Write a program to print an array in
reverse order
Inserting an element in an array
Insertion at end

Insert an element at particular position

Q1. WAP to insert a number in an array that


is already sorted in ascending order.
Deleting an element from an array
Deletion from the end

Delete an element from a particular location


Merging two unsorted arrays
Merging two sorted Arrays

//Merge two unsorted Arrays if(j<n2)


#include<stdio.h> {
int main() while(j<n2)
{int a[]={10,20,30}, c[k++]=b[j++];
b[]={4,33,35,60,65},n1,n2,m,i,c[20],j,k; }
n1=sizeof(a)/sizeof(a[0]); if(i<n1)
n2=sizeof(b)/sizeof(b[0]); {
m=n1+n2;
while(i<n1)
i=0;
j=0; c[k++]=a[i++];
k=0; }
while(i<n1&&j<n2) for(i=0;i<m;i++)
{ printf("%d\t",c[i]);
if(a[i]<=b[j]) return 0;
c[k++]=a[i++]; }
else
c[k++]=b[j++];
}
Arrays and Pointers
2-D Array

Declaration :
datatype name_of_array[row_size][column_size2];
int marks[3][5];
Memory Representation of 2-D Array
Initialize 2-D Array

#include<stdio.h>
int main()
{int a[5][5],b[5][5],i,j,m,n;
printf("enter the number of rows and column of the first array A");
scanf("%d%d",&m,&n);
printf("Enter the element of first array");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf(" Print an array A\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}}
3-D Arrays
Syntax:
Datatype name_of_array[size1][size2][size3];
int A[2][3][3];
Total elements = 2*3*3=18
Memory Representation
of 3-D Array
Address Calculation for
• Column major : BA+w[(E *L +E )*L +E )]
3-D array
3 2 2 1 1
• Row major: BA+w[(E1*L2+E2)*L3+ E3]
• Ei= Ki-Lbi
• Ei: Effective index
• Li: Length of I index

Q Consider an array B[1:8,-5:5,-10:5]


a) Find the length of each dimension
b) Find the address of B[3,3,3], assuming BA= 400 and w=4 using row major
Address Calculation for
multidimensional array
• Column major :
BA+w[(((…(EnLn-1+En-1)Ln-2)+…E3)*L2+E2)*L1+E1)]
• Row major:
BA+w[(…((E1*L2+E2)*L3+ E3)L4+…+En-1)Ln+En]
END

You might also like