Exercise 1:
i) Write a program to reverse an array.
// C Program to Reverse an Array by Printing it from The Last Element to the First Element
#include <stdio.h>
#define N 1000
int main()
{
int arr[N];
int n;
// Inputting the size of the array
printf("Enter the size of the array: ");
scanf("%d", &n);
// Inputting the array
printf("Enter an array: ");
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
// Printing the reverse of the array
printf("Reversed array: ");
for (int i = n-1; i >= 0; i--)
{
printf("%d ", arr[i]);
}
return 0;
}
ii) C Programs to implement the Searching Techniques – Linear & Binary Search.
// C Program to for Linear Search:
#include<stdio.h>
#define N 100
int main()
{
int arr[N],n,i;
printf("Enter the Size of the Array: ");
scanf("%d", &n);
printf("Give the elements on to the array:");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]); }
printf("Elements in the Array:\n");
for(i=0;i<n;i++)
{
printf("%d\t",arr[i]);
}
int sEle;
printf("Enter the Element to be searched:\n");
scanf("%d",&sEle);
for(i=0;i<n;i++)
{
if(sEle == arr[i])
{
printf("Element %d is found at %d position !!",sEle,(i+1));
break;
}
}
if(i==n)
{
printf("Element %d is Not found",sEle);
}
}
// C Program to for Binary Search:
#include<stdio.h>
#define N 100
int main()
{
int arr[N],n,i;
printf("Enter the Size of the Array: ");
scanf("%d", &n);
printf("Give the elements on to the array::");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("Elements in the Array:\n");
for(i=0;i<n;i++)
{
printf("%d\t",arr[i]);
}
int sEle;
printf("\nEnter the Element to be searched::\n");
scanf("%d",&sEle);
int first = 0;
int last = n-1;
int middle = (first+last)/2;
while(first<=last)
{
if(arr[middle]<sEle)
{
first = middle+1;
}
else if (arr[middle] == sEle)
{
printf("Element %d is found at %d",sEle,middle+1);
break;
}
else
{
last=middle-1;
}
middle = (first+last)/2;
}
if(first>last)
{
printf("Element %d is Not found",sEle);
}
}
iii) C Programs to implement Sorting Techniques – Bubble, Selection and Insertion Sort.
// C Program for Bubble Sort:
#include<stdio.h>
int main()
{
int a[10],i,j,temp,n;
printf("\n Enter the max no.of Elements to Sort: \n");
scanf("%d",&n);
printf("\n Enter the Elements : \n");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
for(i=0; i<n; i++)
for(j=i+1; j<n; j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
for(i=0; i<n; i++)
{
printf("%d\t",a[i]);
}
return 0;
}
// C Program for Selection Sort:
#include<stdio.h>
#include<conio.h>
void main()
{
int size,i,j,temp,list[100];
clrscr();
printf("Enter the size of the List: ");
scanf("%d",&size);
printf("Enter %d integer values: ",size);
for(i=0; i<size; i++)
scanf("%d",&list[i]);
//Selection sort logic
for(i=0; i<size; i++)
{
for(j=i+1; j<size; j++)
{
if(list[i] > list[j])
{
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
}
}
printf("List after sorting is: ");
for(i=0; i<size; i++)
printf(" %d",list[i]);
getch();
}
// C Program for Insertion Sort:
#include <stdio.h>
int main(void)
{
int n, i, j, temp;
int arr[64];
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for (i = 1; i < n; i++)
{
j = i;
while (j > 0 && arr[j - 1] > arr[j])
{
temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
j--;
}
}
printf("Sorted list in ascending order:\n");
for (i = 0; i < n; i++)
{
printf("%d\n", arr[i]);
}
return 0;
}
Comments
Post a Comment