0% found this document useful (0 votes)
8 views7 pages

Pps Sess2

The document contains multiple C programming exercises including finding the minimum and maximum elements in an array, searching for an element using linear and binary search, adding and multiplying two matrices, finding the sum of diagonal elements in a matrix, and sorting an array using bubble sort. Each exercise includes the code implementation and expected output. The code snippets demonstrate basic array manipulation and matrix operations.

Uploaded by

Shruti Prasad
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)
8 views7 pages

Pps Sess2

The document contains multiple C programming exercises including finding the minimum and maximum elements in an array, searching for an element using linear and binary search, adding and multiplying two matrices, finding the sum of diagonal elements in a matrix, and sorting an array using bubble sort. Each exercise includes the code implementation and expected output. The code snippets demonstrate basic array manipulation and matrix operations.

Uploaded by

Shruti Prasad
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/ 7

SHRUTI PRASAD 22CE055

23. WAP to find the minimum and maximum element of the array.

CODE:

#include <stdio.h>
#include<conio.h>

int main()
{
int a[10], n, i, max, min;
clrscr();

printf("enter size of array: ");


scanf("%d", &n);

for (i=0; i<n; i++)


{
printf (" enter %d element: ", i+1);
scanf ("%d", &a[i]);
}
max=min= a[0];

for (i=0; i<5; i++)


{
if(max<a[i])
{
max=a[i];
}
if (min>a[i])
{
min=a[i];
}
}
printf("maximum=%d", max);
printf("minumum=%d", min);
getch();
}

OUTPUT:
SHRUTI PRASAD 22CE055
24.WAP to search an element in an array using Linear Search.

CODE:

#include <stdio.h>
#include<conio.h>
void main()
{
int i, x, n, flag=0, a[10];
clrscr();

printf("enter array size: ");


scanf("%d", &n);

for (i=0; i<n; i++)


{
printf("enter %d element: ", i+1);
scanf("%d", &a[i]);
}
printf ("enter element to search: ");
scanf ("%d", &x);

for (i=0; i<n; i++)


{
if (a[i]==x)
{
flag=1;
break;
}
}
if (flag==0)
{
printf ("element not found");
}
else
{
printf(“element found at: %d”, i+1);
}
getch();
}

OUTPUT:
SHRUTI PRASAD 22CE055
26.WAP to add and multiply two matrices of order nxn.

CODE:

#include <stdio.h>
#include <conio.h>
void main()
{
int i, j, k, n, a[10][10], b[10][10], sum[10][10], prod[10][10];
clrscr();
printf("enter order of matrices: ");
scanf("%d", &n);

printf("enter array1 elements: ");


for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
scanf ("%d", &a[i][j]);
}
}
printf("enter array2 elements: ");
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
scanf ("%d", &b[i][j]);
}
}
printf("sum of arrays:\n");
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
sum[i][j] = a[i][j]+b[i][j];
}
}
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
printf("%d ", sum[i][j]);
}
}
printf(“\n product of arrays:\n");
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
prod[i][j]=0;
SHRUTI PRASAD 22CE055
for (k=0; k<n; k++)
{
prod[i][j] = prod[i][j] + a[i][k]*b[k][j];
}
}
}
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
printf("%d ", prod[i][j]);
}
}
getch();
}
OUTPUT:
SHRUTI PRASAD 22CE055
(Additional) WAP to search an element in an array using Binary Search.

CODE:
#include<stdio.h>
#include<conio.h>
int main()
{
int ar[100],n, low, high, mid, item;
clrscr();
printf("Enter the no of elements in array--");
scanf("%d",&n);
printf("Enter elements of array:\n");
for(int i=0; i<n; i++)
{
printf ("enter the %d element: " i+1);
scanf ("%d",&ar[i]);
}
printf ("Enter the elements to be searched: ");
scanf (“%d",&item);

low=0;
high=n-1;
mid=(low+high)/2;
while(low<=high)
if(ar[mid]==item)
{
printf("%d element is present at %d",item, mid+1);
break;
}
else if (ar[mid]<item)
{
low=mid+1;
mid=(low+high)/2;

}
else
{
high=mid-1;
mid=(low+high)/2;
}
getch();
}

OUTPUT:
SHRUTI PRASAD 22CE055

27. WAP that finds the sum of diagonal elements of a mxn matrix.

CODE:
#include <stdio.h>
#include <conio.h>
void main()
{
int i, j, r, c, sum, arr[10][10];
clrscr();
printf("enter no. of rows and columns of matrix: ");
scanf("%d %d", &r, &c);

printf("enter elements of array\n");


for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
scanf("%d", &arr[i][j]);
}
}
sum=0;
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
if(i==j)
{
sum = sum + arr[i][j];
}
}
}
printf("\nsum of main diagonal elements is %d", sum);
getch();
}

OUTPUT:
SHRUTI PRASAD 22CE055

25. WAP to sort the elements of the array in ascending order using Bubble Sort technique.

CODE:
#include<stdio.h>
#include<conio.h>
int main()
{
int a[10], n, temp;
clrscr();
printf("Enter the no of elements in array--");
scanf("%d",&n);
printf("Enter elements of array\n");
for(int i=0;i<n;i++)
{
printf("enter the %d element: " i+1);
scanf("%d", &a[i]);
}
printf("\n");
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-1;j++)
{
if(a[j+1]<a[j])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("sorted array:\n");
for(int i=0;i<n;i++)
{
printf("%d ",a[i]);
}
getch();
}

OUTPUT:

You might also like