November 23, 2024

Array

Array

An array is a collection of items of the same type. It is used to store and process large volumes of data. An element of the array is accessed by index number. The index will start from 0 (zero).

Declaration of array
syntax:-
Type array -name[array-size]
Ex:- int mark [5]
Here marks is an array -name, and size of marks array is 5.

Initialization of array

Index01234
Marks[5]2334464338

Marks[0]=23
Marks[1]=34
Marks[2]=46
Marks[3]=43
Marlks[4]=38

Types of Array

1)One dimensional array(1-D array)
Ex:- int a[3]

Reading elements into 1-D Array
for (i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}

Printing elements from 1-D Array
for(i=0; i<n;i++)
{
printf(“%d”,a[i]);
}


2) Two-dimensional; array (2-D array)
Ex:- int a[3][3] // (3 rows & 3 columns)

Reading elements in a 2-dimensional array

for (i=0;i<n;i++)
{
for (j=0;j<n;j++);
{
scanf(“%d”,&a[i][j])
}
}
printing element in a 2-Dimensional array
for (i=0;i<n;i++)
{
for (j=0;j<n;j++);
{
printff(“%d”,&a[i][j])
}
printf(“\n”);
}

syntax:-
Type – array _name[row size][column size]
Matrix representation of 2-D Array
j=0 j=1 j=2
a[3][3] i=0 a[0][0] a[0][1] a[1][2]
i=1 a[1][0] a[1][1] a[1][2]
i=2 a[2][0] a[2][1] a[2][2]


Searching:- It is a mechanism by which we can search for an element present in an array or not.

There are two types of searching
i) Linear searching
ii) Binary searching

I) Linear searching:- It is also known as Sequential Search.It starts with comparing the target element with all the elements of the array one by one.If the target element is found after comparison then it will return the position of the Target element.

Example:- a[5]

2312351519
i = 0i=1i=2i=3i=4

Target Element =15
Solution:-
Step 1:- Compare the target element (15) with a[0]
1523
take the next iteration (i=1)
step 2 :- Compare the target element a[1]
1512
again jump to next (i=2)
step 3:-compare the target element with a[2]
1535
again jump to the next iteration (i=3)
Step 4 :- Compare the target element with a[3]
15=15 Matched
print the target element is found at position
i+1=3+1=4

Question :- Wap in C program find the element by linear search

#include<stdio.h>
Main( )
{
int (a[10], i, target,n);
clrscr();
printf(“Enter no of element in array \n”);
scanf(“%d, &n”);
printf (“Enter element \n”);
for (i=0;i<n;i++);
{
scanf (“%d”, %a[i]);
}
printf(“Element are \n”);
for (i=0;i<n;i++)
{
printf(“%d\t”,a[i]”0;
}
printf(“enter target element \n”);
scanf(“%d”,&target);

#logic
for (i=0;i<n;i++)
{
if (a[i]==target)
{
printf(“element is found at position%d”,i++);
break;
}
}
if ==n
{
printf(“element is not found “);
}
getch();
}

II) Binary searching:- It always works with sorted arrays.
i) It starts with comparing the target element with the middle element of the array. If the target element is greater than a middle element of the array then searching will be performed in the right subarray by shifting the first pointer to mid+1 (first=mid+1).

ii) If a target is less than a middle element of the array then a search will be formed in the left subarray’s last pointer to mid-1 (last =mid-1).

iii) if the target element is equal to the middle element of the array then there is no need to shift the last and first pointer .it means the element is found and it will return the position.

Example:- a[7]

1523121028138
i=0123456

Target =28
Step 1:- Arrange the array in a sorted Manner

8101213152328

Step 2:- Set pointer first =0, last= n-1, 7-1=6
mid = first+last/2
= 0+6/2 =3
step 3:- if (a[mid]<target)
= (13<28) true
first = mid+1
Calculate mid again = First+last/2 = 4+6/2 =5
Step 4:- if (a[mid]<target) true
= (23<28)
first = mid+1 = 5+1= 6
Calculate mid again = First+last/2 = 6+6/2 =6
Step 5:- if (a[mid]=target)
a[6]==target
28==28
Element is found at position mid+1=7

Question :- Wap in C program to find the true element by binary searching

#include<stdio.h>
main()
{
int a[10], i,n,first,last,mid;
clrscr();
printf(“enter the size of array \n”);
scanf(“%d”,&n);
for (i=0;i<n;i++)
{
scanf (“%d”,&a[i];
}
printf(“element are \n”);
for (i=0;i<n;i++)
{
printf(“%d\t”,a[i]);
}
printf(“enter target elment \n”)
scanf(“%d”,7target);
#logic
first=0;
last=n-1;
mid= first+last/2;
while(first<=last)
{
if (a[mid]<target)
{
first=mid+1;
}
else if (a[mid]>target)
{
last=mid-1;
}
else
{
printf(“elment is found at position %d”,mid+1)
break;
}
mid= first+last/2;
}
if (first.last)
{
printf(“elment is not found”);
}
getch();
}

Leave a Reply

Your email address will not be published. Required fields are marked *