Skip to main content

Examples of Multi-dimensional arrays in C programming



/* Program to read & display 2×3 matrix */
#include<stdio.h>
#include<conio.h>
void main( )
{
int matrix [2] [3], i, j ;
clrscr( ) ;
for (i=0 ; i<2 ; i++)
{         
for (j = 0 ; j<3 ; j++)
{
printf(“Enter matrix [%d] [%d] : \t”, i, j) ;
scanf(“%d”, &matrix [i] [j] ) ;
}
}
printf (“\n Entered matrix is : \n”) ;
for (i=0 ; i<2 ; i++)
{
printf(“%d\t”, matrix [i] [j]) ;
printf(“\n”) ;
}
getch( )
}
Output:
Enter matrix [0] [0] : u1
Enter matrix [0] [1] : u2
Enter matrix [0] [2] : u3
Enter matrix [1] [0] : u4
Enter matrix [1] [1] : u5
Enter matrix [1] [2] : u6
Entered matrix is
1          2          3
4          5          6

/* Program to read two matrices and display their sum */

#include<stdio.h>
#include<conio.h>
main( )
{
int a[3] [3], b[3] [3], s[3] [3], i, j ;
clrscr( ) ;
printf(“Enter first matrix : \n”) ;
for (i=0 ; i<3 ; i++)
for (j=0 ; j<3 ; j++)
scanf(“%d”, &a[i] [j]) ;
printf(“Enter second matrix : \n”) ;
for (i=0 ; i<3 ; i++)
for (j=0 ; j<3 ; j++)
scanf(“%d”, &b[i] [j]) ;
for (i=0 ; i<3 ; i++)
for (j=0 ; j<3 ; j++)
s[i] [j] = a[i] [j] + b[i] [j] ;
printf(“\n The sum matrix is : \n”) ;
for (i=0 ; i<3, i++)
{
 for(j=0 ; j<3 ; j++)
{
printf(“\t%d”, s[i] [j]) ;
}
}
getch( ) ;
}
Output:
Enter first matrix:
1          2         3
4          5          6
7          8          9
Enter second matrix:
9          8         7
6          5          4
3          2         1
The sum matrix is
10        10       10
10        10       10
10         10       10

/* Program to read two matrices and multiply them if possible */
#include<stdio.h>
#include<conio.h>
main( )
{
int a[10] [10], b[10] [10], s[10] [10] ;
int m, n, l, p, i, j, k ;
 printf(“Enter row of first matrix(<=10) : \t”) ;
scanf(“%d”, &m) ;
printf(“Enter column of first matrix(<=10) : \t”) ;
scanf(“%d”, &n) ;
printf(“Enter row of second matrix(<=10) : \t”) ;
scanf(“%d”, &l) ;
printf(“Enter column of second matrix(<=10) : \t”) ;
scanf(“%d”, &p) ;
if (n!=l)
printf(“Multiplication is not possible :”) ;
else
{
printf(“Enter the first matrix : \n”) ;
for (i=0 ; i<=m-1 ; i++)
{
for (j=0 ; j<=n-1 ; j++)
{
printf(“Enter a[%d] [%d] : \t”, i, j) ;
scanf (“%d”, &a [i] [j] ;
}
}
printf(“Enter the second matrix : \n”) ;
for (i=0 ; i<=l-1 ; i++)
{
for (j=0 ; j<=p-1 ; j++)
{
printf (“Enter b[%d] [%d] :\t”, i, j) ;
scanf(“%d”, &b[i] [j] );
}
}
for (i=0 ; i<=m-1 ; i++)
for (j=0 ; j<=p-1 ; j++)
s[i] [j] = 0 ;
for (i=0 ; i<=m-1 ; i++)
for (j=0 ; j<=p-1 ; j++)
for (k=0 ; k<=n-1 ; k++)
s[i] [j] = s[i] [j] + a[i] [k] * b[k] [j] ;
printf (“The matrix multiplication is : \n”) ;
for (i=0 ; i<=m-1 ; i++)
{
for (j=0 ; j<=p-1 ; j++)
{
printf(“%d\t”, s[i] [j],
}
printf(“\n”) ;
}
} /* end of else */
getch( ) ;
} /* end of main( ) */

Output:
Enter row of the first matrix (<=10) : 2
Enter column of the first matix (<=10) : 1
Enter row of the second matrix (<=10) : 1
Enter column of the second matrix (<=10) : 2

Enter the first matrix:
Enter a[0] [0] : 2
Enter a[1] [0] : 2

Enter the second matrix:
Enter b[0] [0] : 3
Enter b[0] [1] : 3
The matrix multiplication is :
6          6

6          6

Comments

Popular posts from this blog

Database Management System(DBMS)

Data and Information  Data is an unprocessed raw fact which does  not give complete meaning about any object,person, entity etc. But when various inter-related data arranged in a row, it gives a complete meaning which is referred to as information. For Example: ID Name Address Program 1101 Ram Shrestha Biratnagar BScIT         is information of a specific person. Database Database is a repository(storage) for the organized collection of related information  of data items. It Consist of meaningful arrangement of data, relationship, constraints, schema etc. In modern days we define database as a computerized record-keeping system used to store information and allows users to retrieve and update these information on their request. Some example of databases are telephone directory, data of SLC result, student and employee records etc. Database Management System(DBMS) Database is the softw...

What is Java Applets?

An applet is a special kind of Java program that is designed to be transmitted over the Internet and automatically executed by a Java-compatible web browser. Furthermore, an applet is downloaded on demand, without further interaction with the user. If the user clicks a link that contains an applet, the applet will be automatically downloaded and run in the browser. Applets are intended to be small programs. They are typically used to display data provided by the server, handle user input, or provide simple functions, such as a loan calculator, that execute locally, rather than on the server. In essence, the applet allows some functionality to be moved from the server to the client.

What is manipulator in C++?

Answer: Manipulators are special functions that can be included in the I/O statement to alter the format parameters of a stream. Table-2 shows some important manipulator functions that are frequently used. To access these manipulators, the file < iomanip.h > should be included in program.                               Table-2               Manipulators                                                    Equivalent ios function               set...