0% found this document useful (0 votes)
4 views21 pages

DSA Unit1 Arrays

The document provides an overview of arrays in programming, focusing on one-dimensional, two-dimensional, and three-dimensional arrays, including their declaration, initialization, and operations such as insertion, deletion, and merging. It also discusses memory representation and address calculation for arrays, both in row-major and column-major formats. Additionally, it includes example programs and exercises related to array manipulation.

Uploaded by

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

DSA Unit1 Arrays

The document provides an overview of arrays in programming, focusing on one-dimensional, two-dimensional, and three-dimensional arrays, including their declaration, initialization, and operations such as insertion, deletion, and merging. It also discusses memory representation and address calculation for arrays, both in row-major and column-major formats. Additionally, it includes example programs and exercises related to array manipulation.

Uploaded by

ekratoz77
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Arrays

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<iostream> {
while(j<n2)
int main() c[k++]=b[j++];
{int a[]={10,20,30}, }
b[]={4,33,35,60,65},n1,n2,m,i,c[20],j,k; if(i<n1)
n1=sizeof(a)/sizeof(a[0]); {
n2=sizeof(b)/sizeof(b[0]); m=n1+n2;
while(i<n1)
i=0; j=0; k=0;
while(i<n1&&j c[k++]=a[i++];
<n2) }
{ for(i=0;i<m;i++)
if(a cout<<c[i];
[i]< return 0;
=b[ }
j])
c[k
+
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<iostream>
using namespace std;
int main()
{int a[5][5],b[5]
[5],i,j,m,n;
cout<<"enter the number of rows and column of the first array A";
cin>>m>>n;
cout<<"Enter the element of first array");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
cout<<" Print an array A\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++) cout<<“\
t"<<a[i][j]; cout<<"\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 2 2 1
3-D array
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