Week 5
a) Write a C program to find both the larges and smallest number in a list of integers.
ALOGORITHM
Step 1: Start
Step 2: Read n
Step 3: Read a[i]
Step 4:repeat step 3 until i=n
Step 5: max=a[0]
min =a[0]
Step 6: if(max<a[i])
max=a[i]
else
if(min>a[i])
min=a[i]
step 7: i+1
Step 8: repeat step 6 until i<n
Step 8: Stop
FLOWCHART
Program :
#include <stdio.h>
void main( )
{
int a[30],max,min,i,n;
printf(" Enter the Range of Numbers : ");
scanf("%d",&n);
printf("\n Enter %d numbers \n",n);
for(i=0;i<n;i++)
scanf(" %d ", &a[ i ]);
max=a[0];
min =a[0];
for(i=0;i<n;i++)
{
if(max<a[i])
max=a[i];
else
if(min>a[i])
min=a[i];
}
printf(" Maximum Value is %d and Minimum Value is %d",max,min);
}
RESULT :
Enter the Range of numbers : 5
Enter 5 numbers
29 3 10 9 1
Maximum Value is 29 and Minimum Value is 1
b) Write a C program that uses functions to perform the following:
i) Addition of Two Matrices
PROGRAM:
#include<stdio.h>
#include <stdio.h>
void scan(int a[][5],int m1,int n1)
{
int i,j;
for(i=0;i<m1;i++)
for(j=0;j<n1;j++)
{
printf("enter the %d,%d element\t",i,j);
scanf("%d",&a[i][j]);
}
}
void print(int a[][5],int m1,int n1)
{
int i,j;
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
printf("%d \t",a[i][j]);
printf("\n");
}
}
void add(int a[][5],int b[][5],int c[][5],int m1,int n1)
{ int i,j;
for(i=0;i<m1;i++)
for(j=0;j<n1;j++)
c[i][j]=a[i][j]+b[i][j];
}
int main()
{ int m1,n1,m2,n2,a[5][5],b[5][5],c[5][5];
printf("\nEnter order of first matrix ");
scanf("%d%d",&m1,&n1);
printf("\nEnter order of second matrix ");
scanf("%d%d",&m2,&n2);
if (m1!=n1 || m2!=n2)
{
printf("\nMatrix Addition is not possible ");
exit(0);
}
printf("\nEnter elements of first matrix : \n");
scan(a,m1,n1);
printf("\nEnter elements of second matrix : \n");
scan(b,m1,n1);
add(a,b,c,m1,n1);
printf("\n The first matrix is\n");
print(a,m1,n1);
printf("\n The second matrix is\n");
print(b,m2,n2);
printf("\n The sum matrix is\n");
print(c,m2,n2);
}
RESULT:
Enter order of first matrix 2 2
Enter order of second matrix 2 2
Enter elements of first matrix :
enter the 0,0 element 2
enter the 0,1 element 2
enter the 1,0 element 2
enter the 1,1 element 2
Enter elements of second matrix :
enter the 0,0 element 2
enter the 0,1 element 2
enter the 1,0 element 2
enter the 1,1 element 2
The first matrix is
2 2
2 2
The second matrix is
2 2
2 2
The sum of matrix is
4 4
4 4
ii) Multiplication of Two Matrices
PROGRAM :
#include<stdio.h>
#include <stdio.h>
void scan(int a[ ][5],int m1,int n1)
{
int i,j;
for(i=0;i<m1;i++)
for(j=0;j<n1;j++)
{
printf("enter the %d , %d element \t",i,j);
scanf("%d",&a[ i ] [ j ]);
}
}
void print(int a[ ][ 5 ],int m1,int n1)
{
int i,j;
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
printf("%d \t",a[ i ] [ j ]);
printf("\n");
}
}
void mul(int a[ ][ 5 ],int b[ ][ 5 ],int c[ ][ 5 ],int m1,int n1,int n2)
{ int i,j,k;
for(i=0;i<m1;i++)
for(j=0;j<n2;j++)
{
c[ i ] [ j ]=0;
for(k=0;k<n1;k++)
c[ i ] [ j ]+=a[ i ] [ k ]*b[ k ] [ j ];
}
}
int main( )
{
int m1,n1,m2,n2,a[5] [5],b[5] [5],c[5] [5];
printf("\nEnter order of first matrix ");
scanf("%d%d",&m1,&n1);
printf("\nEnter order of second matrix ");
scanf("%d%d",&m2,&n2);
if (n1!=m2)
{
printf("\nMatrix multiplication is not possible ");
exit(0);
}
printf("\nEnter elements of first matrix : \n");
scan(a,m1,n1);
printf("\nEnter elements of second matrix : \n");
scan(b,m2,n2);
mul(a,b,c,m1,n1,n2);
printf("\n the first matrix is\n");
print(a,m1,n1);
printf("\n the second matrix is\n");
print(b,m2,n2);
printf("\n the product matrix is\n");
print(c,m1,n2);
}
RESULT:
Enter order of first matrix 2
2
Enter order of second matrix 2
2
Enter elements of first matrix :
enter the 0,0 element 2
enter the 0,1 element 2
enter the 1,0 element 2
enter the 1,1 element 2
Enter elements of second matrix :
enter the 0,0 element 2
enter the 0,1 element 2
enter the 1,0 element 2
enter the 1,1 element 2
The first matrix is
2 2
2 2
The second matrix is
2 2
2 2
The product of matrix is
8 8
8 8
No comments:
Post a Comment