Passing 2–D Array to a function

The argument of 2 – D array can be passed to a function in the same way 1-D arrays are passed.

Program – Add the diagonal elements of a matrix

#include<stdio.h>
#include<stdlib.h>
void main ()
{
      int a [10] [10], j, m, n, sum;
      printf (“How many rows?”);
      scanf (“%d”, &m);
      printf (How many columns?”);
      scanf (“%d”, &n);
      if (m! =n)
      {
    printf (“sum of diagonal is not possible”);
    exit (0);
      }
      printf (“\nEnter &d *d matrix \ n”, m, n)’
      for (i = 0; i<m; i++)
        for (j = 0; j<n; j++)
         scanf (“%d”, &a [i] [j]);
      printf (“\nMatrix is \n”):
      for (I = 0; i<m; i++)
      {
    for (j = 0; j<n; j++)
    printf (“%4d”, a[i], [j]);
    ptintf (“\n”)
      }
      sum = diagonal _sum (a, m);
      printf (“\nSum of diagonal = %d”, sum);
}
Int diagonal _sum (int x [] [10], int p)
{
int s = 0, i,
for (i = 0; i<p; i++)
      s = s + x[i] [i];
return s;
}

Output

How many rows ?3
How many columns ?4
Sum of diagonal is not possible
Sum of diagonal is not possible



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext