SlideShare a Scribd company logo
/* Write a C program that uses functions to perform the following:

            i) Addition of Two Matrices

            ii) Multiplication of Two Matrices

*/



#include<stdio.h>



void main()

{

int ch,i,j,m,n,p,q,k,r1,c1,a[10][10],b[10][10],c[10][10];

clrscr();

printf("************************************");

printf("nttMENU");

printf("n**********************************");

printf("n[1]ADDITION OF TWO MATRICES");

printf("n[2]MULTIPLICATION OF TWO MATRICES");

printf("n[0]EXIT");

printf("n**********************************");

printf("ntEnter your choice:n");

scanf("%d",&ch);



if(ch<=2 & ch>0)

{

printf("Valid Choicen");
}



switch(ch)

{

    case 1:

    printf("Input rows and columns of A & B Matrix:");

    scanf("%d%d",&r1,&c1);

    printf("Enter elements of matrix A:n");

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

     {

         for(j=0;j<c1;j++)

         scanf("%d",&a[i][j]);

     }

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

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

     {

         for(j=0;j<c1;j++)

         scanf("%d",&b[i][j]);

     }

    printf("n =====Matrix Addition=====n");

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

     {

         for(j=0;j<c1;j++)

         printf("%5d",a[i][j]+b[i][j]);
printf("n");

    }

break;



case 2:

printf("Input rows and columns of A matrix:");

scanf("%d%d",&m,&n);

printf("Input rows and columns of B matrix:");

scanf("%d%d",&p,&q);

if(n==p)

{

printf("matrices can be multipliedn");

printf("resultant matrix is %d*%dn",m,q);

printf("Input A matrixn");

read_matrix(a,m,n);

printf("Input B matrixn");

/*Function call to read the matrix*/

read_matrix(b,p,q);

/*Function for Multiplication of two matrices*/

printf("n =====Matrix Multiplication=====n");

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

    for(j=0;j<q;++j)

    {

        c[i][j]=0;
for(k=0;k<n;++k)

                c[i][j]=c[i][j]+a[i][k]*b[k][j];

        }



    printf("Resultant of two matrices:n");

        write_matrix(c,m,q);

        }

        /*end if*/

    else

    {

    printf("Matrices cannot be multiplied.");

    }

    /*end else*/

    break;



    case 0:

    printf("n Choice Terminated");

    exit();

    break;



    default:

    printf("n Invalid Choice");

}

getch();
}



/*Function read matrix*/

int read_matrix(int a[10][10],int m,int n)

    {

        int i,j;

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

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

                   scanf("%d",&a[i][j]);

    return 0;

    }



    /*Function to write the matrix*/

int write_matrix(int a[10][10],int m,int n)

    {

        int i,j;

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

         {

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

                   printf("%5d",a[i][j]);

                   printf("n");

         }

    return 0;

    }
/* Write a C program to find both the largest and smallest number in a list of integers*/




main( )

{

                   float largest(float a[ ], int n);

                   float value[4] = {2.5,-4.75,1.2,3.67};

                   printf("%fn", largest(value,4));

}

float largest(float a[], int n)

{

                   int i;

                   float max;

                   max = a[0];

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

                                       if(max < a[i])

                                       max = a[i];

                   return(max);
}

More Related Content

PPTX
Computer programing w
DOCX
One dimensional operation of Array in C- language
PPTX
Function basics
Computer programing w
One dimensional operation of Array in C- language
Function basics

What's hot (20)

PPTX
Double linked list
PDF
Insertion sort
DOCX
Pratik Bakane C++
PPTX
Double linked list
PDF
Rcpp11 genentech
PDF
C++ programs
DOCX
Pratik Bakane C++
DOCX
Pratik Bakane C++
DOCX
Pratik Bakane C++
PPTX
งานนำเสนอ อาจารย์ลาวัลย์
PPT
C questions
PDF
SATySFiのこれからの課題たち
PDF
Odd number
PDF
Implementing string
DOCX
Metnum
DOCX
Pratik Bakane C++
PPT
Cpp tutorial
Double linked list
Insertion sort
Pratik Bakane C++
Double linked list
Rcpp11 genentech
C++ programs
Pratik Bakane C++
Pratik Bakane C++
Pratik Bakane C++
งานนำเสนอ อาจารย์ลาวัลย์
C questions
SATySFiのこれからの課題たち
Odd number
Implementing string
Metnum
Pratik Bakane C++
Cpp tutorial
Ad

Similar to week-5x (20)

PPTX
C Programming Language Part 8
PDF
Data Structure using C
DOCX
Chapter 8 c solution
DOCX
ADA FILE
DOCX
DOC
Basic c programs updated on 31.8.2020
PDF
C Prog - Pointers
DOCX
C lab manaual
PDF
C lab programs
PDF
C lab programs
PPTX
L25-L26-Parameter passing techniques.pptx
DOCX
SaraPIC
DOC
Daapracticals 111105084852-phpapp02
PPT
lets play with "c"..!!! :):)
PDF
C Programming lab
DOC
C basics
DOCX
Data structure new lab manual
C Programming Language Part 8
Data Structure using C
Chapter 8 c solution
ADA FILE
Basic c programs updated on 31.8.2020
C Prog - Pointers
C lab manaual
C lab programs
C lab programs
L25-L26-Parameter passing techniques.pptx
SaraPIC
Daapracticals 111105084852-phpapp02
lets play with "c"..!!! :):)
C Programming lab
C basics
Data structure new lab manual
Ad

More from KITE www.kitecolleges.com (20)

Recently uploaded (20)

PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
RMMM.pdf make it easy to upload and study
PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
PDF
IGGE1 Understanding the Self1234567891011
PDF
Indian roads congress 037 - 2012 Flexible pavement
PPTX
UNIT III MENTAL HEALTH NURSING ASSESSMENT
PDF
SOIL: Factor, Horizon, Process, Classification, Degradation, Conservation
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PDF
1_English_Language_Set_2.pdf probationary
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Unit 4 Skeletal System.ppt.pptxopresentatiom
PPTX
History, Philosophy and sociology of education (1).pptx
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
Weekly quiz Compilation Jan -July 25.pdf
RMMM.pdf make it easy to upload and study
A powerpoint presentation on the Revised K-10 Science Shaping Paper
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
IGGE1 Understanding the Self1234567891011
Indian roads congress 037 - 2012 Flexible pavement
UNIT III MENTAL HEALTH NURSING ASSESSMENT
SOIL: Factor, Horizon, Process, Classification, Degradation, Conservation
LDMMIA Reiki Yoga Finals Review Spring Summer
1_English_Language_Set_2.pdf probationary
A systematic review of self-coping strategies used by university students to ...
Final Presentation General Medicine 03-08-2024.pptx
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
Complications of Minimal Access Surgery at WLH
Unit 4 Skeletal System.ppt.pptxopresentatiom
History, Philosophy and sociology of education (1).pptx

week-5x

  • 1. /* Write a C program that uses functions to perform the following: i) Addition of Two Matrices ii) Multiplication of Two Matrices */ #include<stdio.h> void main() { int ch,i,j,m,n,p,q,k,r1,c1,a[10][10],b[10][10],c[10][10]; clrscr(); printf("************************************"); printf("nttMENU"); printf("n**********************************"); printf("n[1]ADDITION OF TWO MATRICES"); printf("n[2]MULTIPLICATION OF TWO MATRICES"); printf("n[0]EXIT"); printf("n**********************************"); printf("ntEnter your choice:n"); scanf("%d",&ch); if(ch<=2 & ch>0) { printf("Valid Choicen");
  • 2. } switch(ch) { case 1: printf("Input rows and columns of A & B Matrix:"); scanf("%d%d",&r1,&c1); printf("Enter elements of matrix A:n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) scanf("%d",&a[i][j]); } printf("Enter elements of matrix B:n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) scanf("%d",&b[i][j]); } printf("n =====Matrix Addition=====n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) printf("%5d",a[i][j]+b[i][j]);
  • 3. printf("n"); } break; case 2: printf("Input rows and columns of A matrix:"); scanf("%d%d",&m,&n); printf("Input rows and columns of B matrix:"); scanf("%d%d",&p,&q); if(n==p) { printf("matrices can be multipliedn"); printf("resultant matrix is %d*%dn",m,q); printf("Input A matrixn"); read_matrix(a,m,n); printf("Input B matrixn"); /*Function call to read the matrix*/ read_matrix(b,p,q); /*Function for Multiplication of two matrices*/ printf("n =====Matrix Multiplication=====n"); for(i=0;i<m;++i) for(j=0;j<q;++j) { c[i][j]=0;
  • 4. for(k=0;k<n;++k) c[i][j]=c[i][j]+a[i][k]*b[k][j]; } printf("Resultant of two matrices:n"); write_matrix(c,m,q); } /*end if*/ else { printf("Matrices cannot be multiplied."); } /*end else*/ break; case 0: printf("n Choice Terminated"); exit(); break; default: printf("n Invalid Choice"); } getch();
  • 5. } /*Function read matrix*/ int read_matrix(int a[10][10],int m,int n) { int i,j; for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); return 0; } /*Function to write the matrix*/ int write_matrix(int a[10][10],int m,int n) { int i,j; for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%5d",a[i][j]); printf("n"); } return 0; }
  • 6. /* Write a C program to find both the largest and smallest number in a list of integers*/ main( ) { float largest(float a[ ], int n); float value[4] = {2.5,-4.75,1.2,3.67}; printf("%fn", largest(value,4)); } float largest(float a[], int n) { int i; float max; max = a[0]; for(i = 1; i < n; i++) if(max < a[i]) max = a[i]; return(max);
  • 7. }