Practical No 1 :
Write a program to find out largest number from the array and also find its location.
Code :
#include <stdio.h>
int main()
{
int arr[] = {25, 11, 7, 75, 56};
int length = sizeof(arr)/sizeof(arr[0]);
int max = arr[0];
for (int i = 0; i < length; i++) {
if(arr[i] > max)
max = arr[i];
}
printf("Largest element present in given array: %d\n", max);
return 0;
}
Output :
Practical No 2 :
Write a program to traverse an array and find the sum and average of data elements from an array.
Code :
#include<stdio.h>
int main(){
int A[100],sum,i,n;
printf(“Enter The Number of elements you wants :”);
scanf(“%d”,&n);
printf(“Enter %d Elements “,n);
for(i=0;i<n;i++)
{
scanf(“%d”,&A[i]);
}
sum=0;
for(i=0;i<n;i++)
{
sum=sum+i;
}
float avg;
avg=sum/n;
printf(“The Sum Is %d \n “,sum);
printf(“The Average is %f “,avg);
return 0;
}
Output :
Practical No 3 :
Write a Program to
a) Insert an element in an array
Code :
#include <stdio.h>
int main()
{
int arr[100] = { 0 };
int i, x, pos, n = 10;
// initial array of size 10
for (i = 0; i < 10; i++)
arr[i] = i + 1;
printf("The Elements In the Original Array : \n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
// element to be inserted
x = 50;
// position at which element
// is to be inserted
pos = 5;
n++;
// shift elements forward
for (i = n - 1; i >= pos; i--)
arr[i] = arr[i - 1];
// insert x at pos
arr[pos - 1] = x;
printf("After Inserting the element the array will be :\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
Output :
b) Delete an element from an array.
Code :
#include <conio.h>
int main ()
{
int arr[50];
int pos, i, num;
printf (" \n Enter the number of elements in an array: \n ");
scanf (" %d", &num);
printf (" \n Enter %d elements in array: \n ", num);
for (i = 0; i < num; i++ )
{ printf (" arr[%d] = ", i);
scanf (" %d", &arr[i]);
}
printf( " Define the position of the array element where you want to delete: \n ");
scanf (" %d", &pos);
// check whether the deletion is possible or not
if (pos >= num+1)
{
printf (" \n Deletion is not possible in the array.");
}
else
{
// use for loop to delete the element and update the index
for (i = pos - 1; i < num -1; i++)
{
arr[i] = arr[i+1]; // assign arr[i+1] to arr[i]
}
printf (" \n The resultant array is: \n");
// display the final array
for (i = 0; i< num - 1; i++)
{
printf (" arr[%d] = ", i);
printf (" %d \n", arr[i]);
}
}
return 0;
}
Output :
Practical No 4 :
To study and execute the Linear search method.
Code :
#include<stdio.h>
int main()
{
int a[10]={30,20,50,80,67,57,26,90};
int item,n=10,pos;
printf("\n Array Elements Are ...\n");
for(pos=0;pos<n;pos++)
printf("%d\n",a[pos]n);
printf("\n\nEnter the number to search in the array");
scanf("%d",&item);
pos=0;
while(pos<n)
{
if(a[pos]==item)
{
printf("\n%d is found at %d Position",item,pos);
break;
}
pos++;
}
if(pos==n)
printf("\n%dis not found in an array",item);
Output :
Practical No 5 :
To study and execute the Binary Search method.
Code :
#include<stdio.h>
int main()
{
int a[100],item,mid,beg=0,i,n;
printf("Enter No of Emement in an array");
scanf("%d",&n);
int end=n;
printf("\nEnter %d no in Sorted Order",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter The No To Search");
scanf("%d",&item);
while(beg<=end)
{
mid=(beg+end)/2;
if(a[mid]==item)
{
printf("\n %d is found at %d Position",item,mid);
break;
}
else
if(item>a[mid])
{
beg=mid+1;
else
end = mid-1;
}
if(beg>end)
printf("\n%d is not Found",item);
}
Output:
Practical No 6 :
To study and execute Bubble sort method.
Code :
#include<stdio.h>
void print(int a[], int n)
{
int i;
for(i = 0; i < n; i++)
{
printf("%d ",a[i]);
}
}
void bubble(int a[], int n)
{
int i, j, temp;
for(i = 0; i < n; i++)
{
for(j = i+1; j < n; j++)
{
if(a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
void main ()
{
int i, j,temp;
int a[5] = { 10, 35, 32, 13, 26};
int n = sizeof(a)/sizeof(a[0]);
printf("Before sorting array elements are - \n");
print(a, n);
bubble(a, n);
printf("\nAfter sorting array elements are - \n");
print(a, n);
}
Output :