9.Write a C program to perform a binary search for a given key integer in a single dimensional array of numbers in ascending order and report success or failure in the form of a suitable message
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[20],n,mid,l,h,i,key;
printf("Enter the value for n\n");
scanf("%d",&n);
printf("enter %d elements in Ascending order \n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the key element to search\n");
scanf("%d",&key);
l=0;
h=n-1;
while(l<=h)
{
mid=(l+h)/2;
if(a[mid]==key)
{
printf("The key if found at the position %d",mid+1); exit(0);
}
if(key>a[mid])
l=mid+1;
else
h=mid-1;
}
printf("The key is not found\n");
return 0;
}
10.Write a C program to input N integer numbers into a single dimension array, sort them in to ascending order using selection sort technique, and then to print both the given array and the sorted array with suitable headings.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[50],n,temp,i,j,pos;
printf("Enter the value of n\n");
scanf("%d",&n);
printf("Enter the numbers\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Entered values are\n");
for(i=0;i<n;i++)
printf("%d\t", a[i]);
for(i=0;i<n-1;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
If(a[j]<a[pos])
pos=j;
}
temp=a[i];
a[i]=a[pos];
a[pos]=temp;
}
printf("\nThe sorted values are\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
return 0;
}
11.Write a C program to transpose a matrix of order M x N and find the trace of the resultant matrix.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j,m,n,a[10][10],b[10][10],trace=0;
printf("Enter the size of the matrix\n");
scanf("%d%d",&m,&n);
printf("Enter %d x %d matrix\n",m,n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("The entered matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
for(i=0;i<m;i++)
for(j=0;j<n;j++)
b[j][i]=a[i][j];
printf("The transpose of the matrix is\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
if(m==n)
{
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(i==j)
trace=trace+b[i][j];
}
}
printf("The trace is %d\n",trace);
}
else
printf(" cant perform trace because its not a square matrix\n");
return 0;
}13.Write a C program using functions readmat ( ), rowsum ( ), colsum ( ), totsum ( ) and printmat( ) to read the values into a two dimensional array A, find the sum of all the elements of a row, sum of all the elements of a column, find the total sum of all the elements of the two dimensional array A and print the results.
#include <stdio.h>
#include <stdlib.h>
int m,n,i,j,a[10][10];
void readmat();
void colsum();
void totalsum();
void rowsum();
void printmat();
int main()
{
printf("Enter the order of the matrix\n");
scanf("%d%d",&m,&n);
printf("Enter the %d x %d matrix\n", m,n); readmat();
printf("The Entered matrix is\n");
printmat();
rowsum();
colsum();
totsum();
return 0;
}
void readmat()
{
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
void printmat()
{int i,j; for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
}
void colsum()
{
int sum=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
sum=sum+a[j][i];
}
printf("the sum of elements of column %d is %d\n", i+1,sum);
sum=0;
}
}
void rowsum()
{
int sum=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
sum=sum+a[i][j];
}
printf("the sum of elements of row %d is %d\n", i+1,sum);
sum=0;
}
}
void totsum()
{
int sum=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
sum=sum+a[i][j];
}
}
printf("the sum of all elements in matrix is %d\n",sum);
}14.Write a C program to perform a linear search for a given key integer in a single dimensional array of numbers and report success or failure in the form of a suitable message using functions.