Powered By Blogger

Friday, April 27, 2012

Program : 8


PROGRAM - 8

a)  Write a program to find the largest and smallest number in a list of integers.

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
    int rng[50],i,n,lrg,sml;
    clrscr();
    printf("enter the range");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
     printf("enter %d element \n",i+1);
     scanf("%d", &rng[i]);
    }
    lrg=rng[0];
    sml=rng[0];
    for(i=0;i<n;i++)
    {
     if(rng[i]>=lrg)
     lrg=rng[i];
     if(rng[i]<=sml)
     sml=rng[i];
    }
    printf("the smallest number in range %d",sml);
    printf("\nthe largest number in range %d",lrg);
    getch();
}
/*
INPUT:enter the range:5
enter 1 element 5
enter 2 element 8
enter 3 element 9
enter 4 element 23
enter 5 element 1
OUTPUT:
the smallest number in range 1
the largest number in range 23
*/

b)  Write a program to perform the following:
ü  Addition of two matrices.
ü  Multiplication of two matrices.

Program for matrix addition:

#include<stdio.h>
#include<conio.h>
main()
{
     int a[10][10],b[10][10],c[10][10],i,j,r1,r2,c1,c2;
     clrscr();
     printf("enter rows and columns of A&B\n");
     scanf("%d %d %d %d",&r1,&c1,&r2,&c2);
     if(r1==r2&&c1==c2)
     {
        printf("Enter matrix A elements\n");
        for(i=0;i<r1;i++)
        {
         for(j=0;j<c1;j++)
         scanf("%d",&a[i][j]);
        }
        printf("Enter matrix B elements\n");
        for(i=0;i<r2;i++)
        {
         for(j=0;j<c2;j++)
         scanf("%d",&b[i][j]);
        }
        printf("Sum of matrices is\n");
        for(i=0;i<r1;i++)
        {
        for(j=0;j<c1;j++)
        {
          c[i][j]=a[i][j]+b[i][j];
          printf("%d\t",c[i][j]);
        }
        printf("\n");
        }
     }
     getch();
}
/*
INPUT:enter rows and columns of matrix A3
3
enter rows and columns of matrix B3
3
Enter matrix A elements
1    2    3    4    5    6    7    8    9
Enter matrix B elements
9    8    7    6    5    4    3    2    1
OUTPUT:
Sum of matrices is
10      10      10
10      10      10
10      10      10

*/

Program For Matrix Multiplication:

#include<stdio.h>
#include<conio.h>
main()
{
     int a[10][10],b[10][10],c[10][10],i,j,k,r1,r2,c2,c1;
     clrscr();
     printf("enter the rows and columns of matrix A");
     scanf("%d%d",&r1,&c1);
     printf("enter the rows and columns of matrix B");
     scanf("%d%d",&r2,&c2);
     if(c1==r2)
     {
        printf("enter the elements of matrix A");
        for(i=0;i<r1;i++)
        {
          for(j=0;j<c1;j++)
          scanf("%d",&a[i][j]);
        }
        printf("enter the elements of matrix B");
        for(i=0;i<r2;i++)
        {
          for(j=0;j<c2;j++)
          scanf("%d",&b[i][j]);
        }
        printf("multiplication of two matrices is\n");
        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("%d\t",c[i][j]);
          }
          printf("\n");
        }
     }
     else
     printf("multiplication is not possible");
     getch();
}
/*
INPUT:enter the rows and columns of matrix A3
3
enter the rows and columns of matrix B3
3
enter the elements of matrix A1
2    3    4    5    6    7    8    9
enter the elements of matrix B1
0    0    0    1    0    0    0    1
OUTPUT:
mulptiplication of two matrices is
1       2       3
4       5       6
7       8       9
*/

No comments:

Post a Comment