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

Difference between Binary mode and Text Mode in C programming

There are mainly three difference between binary and text mode. They are as follow.               i.       In text mode, a special character EOF whose ASCII value is 26 is inserted after the last character in the file to mark the end of file. But, there is no such special character present in the binary mode. The binary mode files keep track of the end of file from the number of characters present in the directory entry of the file.             ii.       In text mode of file, text and numbers are stored as one character per byte. For example, the number 1234 occupies two bytes in memory but it occupies 4 bytes (one byte per character) in the file. But, in binary mode, the number occupies the number of bytes as it occupies in the memory. Thus,  the above number occupies two bytes in file also in the case o...

Types of Functions in C Programming

C program has two types of functions: 1. Library Functions 2. User defined functions Library Functions: These are the functions which are already written, compiled and placed in C Library and they are not required to be written by a programmer. The function’s name, its return type, their argument number and types have been already defined. We can use these functions as required. For example: printf(), scanf(), sqrt(), getch(), etc. User defined Functions: These are the functions which are defined by user at the time of writing a program. The user has choice to choose its name, return type, arguments and their types. The job of each user defined function is as defined by the user. A complex C program can be divided into a number of user defined functions. For example: #inlcude<stdio.h> double convert (int) ; /* function proto type */ main() { int c ; /* double d ; */ printf (“Enter temperature in Celsius: ”) ; scanf (“%d”, &c) ; /* d =...

CPU (Central Processing Unit)

PGA reffered as pin grid array in which pins of CPU are lined up in a straight format. SPGA referred as staggered pin grid array. in which pins of CPU are arranged staggered format. LGA reffered to as Land Grid Array in which pins are available within inside the socket but not in the CPU in other words in line grid array, in CPU ther are no pinsbut insteadpins areattached with in a socket which contact with with the CPU. for e.g. LGA775 socket(no pins on cpu) has better cooling system. better contact and better locking(climbing). LGA1155: Letest generation I socket, also reffered as sandy bridge, Turbo boost overclocking. More resent than LGA 1156 socket. LGA1156: ability to north bridge Doul channel DDR3 optional integreted graphics PCI express LGA1366: hi end core i series socket integreted tiple channal memmory controller external control bridge(HUB) Upgrading CPU: need to check its core suppert check multiprocessor supports or not check on the ...