0% found this document useful (0 votes)
23 views3 pages

Exercise Programs

Uploaded by

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

Exercise Programs

Uploaded by

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

Exercise programs:

C program to find given key element in an array using linear search.


#include <stdio.h>
int main()
{
int n, i, key;
int a[20];
int found = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter %d elements ", n);
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("Enter the element to search: ");
scanf("%d", &key);
for (i = 0; i < n; i++)
{
if (a[i] == key)
{
printf("Linear Search: Element found at position %d\n", i+1);
found = 1;
break;
}
}
if (found==0)
{
printf("Linear Search: Element not found\n");
}
}

C program to find given key element in an array using Binary search.


#include <stdio.h>
int main()
{
int n, i, key, mid;
int a[20];
int first, last, found;

printf("Enter the number of elements: ");


scanf("%d", &n);
printf("Enter %d elements in sorted order: ", n);
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("Enter the element to search: ");
scanf("%d", &key);
first = 0, last = n - 1;
found = 0;
while (first <= last)
{
mid = (first + last) / 2;

if (a[mid] == key)
{
printf("Binary Search: Element found at position %d\n", mid);
found = 1;
break;
}
else if (a[mid] < key)
{
first = mid + 1;
}
else
{
last = mid - 1;
}
}
if (found==0)
{
printf("Binary Search: Element not found\n");
}

return 0;
}

Program to perform addition and subtraction of Matrices in C


#include<stdio.h>

int main()
{
int n, m, i, j, a[10][10], b[10][10], s[10][10], d[10][10];
printf("\nEnter the number of rows and columns of the first matrix \n\n");
scanf("%d%d", &m, &n);

printf("\nEnter the %d elements of the first matrix \n\n", m*n);


for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
scanf("%d", &a[i][j]);
}
}

printf("\nEnter the %d elements of the second matrix \n\n", m*n);


for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
scanf("%d", &b[i][j]);
}
}
printf("\n\nThe first matrix is: \n\n");
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
printf("%d\t", a[i][j]);
}
printf("\n");
}

printf("\n\nThe second matrix is: \n\n");


for(i = 0; i < m; i++) // to iterate the rows
{
for(j = 0; j < n; j++) // to iterate the columns
{
printf("%d\t", b[i][j]);
}
printf("\n");
}
/* finding the SUM of the two matrices */
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
s[i][j] = a[i][j] + b[i][j];
}
}
printf("\n\nThe sum of the two entered matrices is: \n\n");
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
printf("%d\t", s[i][j]);
}
printf("\n");
}
/* finding the DIFFERENCE of the two matrices */
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
d[i][j] = a[i][j] - b[i][j];
}
}
printf("\n\nThe difference(subtraction) of the two entered matrices is: \n\n");
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
printf("%d\t", d[i][j]);
}
printf("\n");
}

printf("\n\n\t\t\tCoding is Fun !\n\n\n");


return 0;
}

You might also like