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

Ex4 Arrays 1D and 2D, Multi-Dimensional Arrays, Traversal

The document discusses various operations on arrays and matrices in C programming language like finding maximum and minimum of an array, sorting an array, linear search, binary search, addition of matrices and multiplication of matrices.

Uploaded by

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

Ex4 Arrays 1D and 2D, Multi-Dimensional Arrays, Traversal

The document discusses various operations on arrays and matrices in C programming language like finding maximum and minimum of an array, sorting an array, linear search, binary search, addition of matrices and multiplication of matrices.

Uploaded by

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

Ex.

No: 4 Arrays 1D and 2D, Multi-dimensional arrays, traversal

a) Maximum and Minimum of an array

Aim:
To write a c program to find the maximum and minimum number of an array.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, n,arr[100],min,max;
clrscr();
printf("Enter total number of elements: ");
scanf("%d", &n);
printf("\n");
for(i = 0; i < n; i++)
{
printf("Enter Number %d: ", i+1);
scanf("%d", &arr[i]);
}
min=max=arr[0];
for(i = 1; i < n; i++)
{
if(arr[i] < min)
min = arr[i];
if(arr[i] > max)
max = arr[i];
}
printf("\nMaximum Element = %d", max);
printf("\nMinimum Element = %d", min);
getch();
}
b) Sorting of given “N” numbers (Selection sort)

Aim:
To write a C program to sort n numbers using selection sort.

Program:
#include <stdio.h>
#include <conio.h>
#define MAX 20
void main()
{
int arr[MAX], i,j,n,p,temp;
clrscr();
printf("\nEnter the number of elements : ");
scanf("%d",&n);
printf("\nEnter the elements : ");
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for(i = 0; i< n - 1 ; i++)
{
/*Find the smallest element*/
p = i;
for(j = i + 1; j < n ; j++)
{
if(arr[j] < arr[p])
p = j;
}
if( i!= p )
{
temp = arr [i];
arr[i] = arr[p];
arr[p] = temp ;
}
}
printf("Sorted list is : ");
for (i = 0; i < n; i++)
printf("%d \t", arr[i]);
}

c) Linear Search (Sequential Search)

Aim:
To write a c program to search the element in an array using linear search operation.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[20], n, i, num, found=0;
clrscr();
printf("Enter the number of elements in the array : ");
scanf("%d",&n);
printf("Enter the elements: ");
for(i=0; i<n; i++)
scanf("%d", &arr[i]);

printf("Enter the element to be searched : ");


scanf("%d",&num);
for(i=0; i < n; i++)
{
if(arr[i]==num)
{
found=1;
printf("%d found at position %d\n", num, i+1);
break;
}
}/*End of for*/
if(found == 0)
printf("%d not found in array\n", num);
getch();
}

d) Binary Search

Aim:
To write a C program to search an element in an array using binary search.

Program:
#include <stdio.h>
#include <conio.>
void main()
{
int arr[20],start,end,middle,n,i,num;
clrscr();
printf("Enter the number of elements in the array : ");
scanf("%d",&n);
printf("\nEnter the elements in sorted order: ");
for(i=0; i < n; i++)
scanf("%d",&arr[i]);

printf("Enter the element to be searched : ");


scanf("%d",&num);
start=0;
end=n-1;
middle=(start+end)/2;
while(num != arr[middle] && start <= end)
{
if(num > arr[middle])
start=middle+1;
else
end=middle-1;
middle=(start+end)/2;
}
if(num==arr[middle])
printf("%d found at position %d\n",num,middle+1);
if(start>end)
printf("%d not found in array\n",num);
getch();
}/*End of main()*/

e) Matrix Addition
Aim:
To write a C program for matrix addition.
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,r1,c1,r2,c2;
clrscr();
printf("\n**** Size of Matrix A and Matrix B must be equal ****\n\n");
printf("\n Enter size r1 and c1 for matrix A : ");
scanf("%d%d",&r1,&c1);
printf("\n Enter size r2 and c2 for matrix B : ");
scanf("%d%d",&r2,&c2);
if(r1!=r2 || c1!=c2)
{
printf("\n**** Matrix Addition is not Possible *****\n\n\n");
exit(0);
}
printf("\n Enter the elements of matrix A: \n");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);

printf("\n Enter the elements of matrix B: \n");


for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);

for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
c[i][j]=a[i][j]+b[i][j];

printf("\n Added Matrix is : \n");


for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
getch();
}

f) Matrix Multiplication

Aim:
To write a C program for matrix multiplication

Program:
#include<stdio.h>
#include<stdlib.h>
void main()
{
int a[5][5],b[5][5],c[5][5]={0},i,j,k,r1,c1,r2,c2;
clrscr();
printf("\n\n**** column1 and row2 should be equal ****\n\n");
printf("\nEnter row1 and column1 size for matrix A: ");
scanf("%d%d",&r1,&c1);
printf("\nEnter row2 and column2 size for matrix B: ");
scanf("%d%d",&r2,&c2);
if(c1!=r2)
{
printf("\n** Matrix Multiplication is not Possible **\n\n\n");
exit(0);
}
printf("\nEnter the elements of matrix A:\n");
for (i=0;i<r1;i++)
for (j=0;j<c1;j++)
scanf("%d",&a[i][j]);

printf("\nEnter the elements of matrix B:\n");


for (i=0;i<r2;i++)
for (j=0;j<c2;j++)
scanf("%d",&b[i][j]);

for (i=0;i<r1;i++)
for (j=0;j<c2;j++)
{
c[i][j]=0;
for (k=0;k<c1;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
printf("\nMultiplied Matrix is:\n\n");
for (i=0;i<r1;i++)
{
for (j=0;j<c2;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}

You might also like