Lab Programs Unit 3,4,5
Lab Programs Unit 3,4,5
#include<stdio.h>
int main() {
int i
int marks [6];
printf(“enter array elements”);
for(i=0;i<=5;i++)
{
scanf("%d",&marks[i]);
}
//printing elements of array with loop
for(i=0;i<=5;i++)
{
printf("%d \n",marks[i]);
}
return 0;
}
}
9. program to find a element from an array using binary search
#include <stdio.h>
int main() {
int i,n, key, pos=0, mid = 0, low = 0;
printf("enter n value");
scanf("%d", &n);
int a[n];
int high=n-1;
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("enter key");
scanf("%d", &key);
while (low <=high)
{
mid = (low + high) / 2;
if (key==a[mid])
{
pos = mid;
break;
}
else if (key <a[mid]) {
high = mid-1;
}
else {
low= mid+1;
}
}
printf("element found at position %d", pos);
return 0;
}
Exercise-7
12. program to read array elements from keyboard and print
array elements(2 Dimensional array)
#include<stdio.h>
int main()
{
int a[10][10],i,j;
printf("Enter elements into A matrix");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Elements of matrix A\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(" %d",a[i][j]);
}
printf("\n");
}
return 0;
}
13. program for matrix addition
#include <stdio.h>
int main()
{
int a[2][2],b[2][2],d[2][2],r,c;
printf("eneter elements of A matrix ");
for(r=0;r<2;r++)
{
for(c=0;c<2;c++)
{
scanf("%d",&a[r][c]);
}
}
printf("enter elements of B matrix");
for(r=0;r<2;r++)
{
for(c=0;c<2;c++)
{
scanf("%d",&b[r][c]);
}
}
printf("resultant matrix of c:\n");
for(r=0;r<2;r++)
{
for(c=0;c<2;c++)
{
d[r][c]=a[r][c]+b[r][c];
}
}
for(r=0;r<2;r++)
{
for(c=0;c<2;c++)
{
printf("%d\t",d[r][c]);
}
printf("\n");
}
}