Ds Practical Print
Ds Practical Print
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];
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
Code :
#include <stdio.h>
int main()
{
int arr[100] = { 0 };
int i, x, pos, n = 10;
// element to be inserted
x = 50;
n++;
// insert x at pos
arr[pos - 1] = x;
return 0;
}
Output :
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( " 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]
}
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;
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;
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 :