EXPERIMENT NO.
Aim / Title: Implementation of 2-D array
1) Program to Find Transpose of a Matrix
2) Program to Multiply two Matrices
Problem Statement: To Implement of 2-D array
Objectives: To write and execute programs in C++ to solve problems using data structures such
as arrays in 2 dimension.
Outcomes: Implement operations like Multiply & transpose of given matrix.
Pre-requisite: Basic knowledge of C/C++ programming language
Hardware requirements: PC i3 and above.
Software requirements: software for C/C++ (any software like Turbo/Borland C complier. ,
DevC++, Codeblock etc)
Theory:
The simplest form of multidimensional array is the two-dimensional array. A two-dimensional
array is, in essence, a list of one-dimensional arrays. To declare a two-dimensional integer array
of size [x][y], you would write something as follows −
type arrayName [ x ][ y ];
Where type can be any valid C data type and arrayName will be a valid C identifier. A two
dimensional array can be considered as a table which will have x number of rows and y number
of columns. A two-dimensional array a, which contains three rows and four columns can be
shown as follows −
Thus, every element in the array a is identified by an element name of the form a[ i ][ j ], where
'a' is the name of the array, and 'i' and 'j' are the subscripts that uniquely identify each element in
'a'.
Initializing Two-Dimensional Arrays
Multidimensional arrays may be initialized by specifying bracketed values for each row.
Following is an array with 3 rows and each row has 4 columns.
int a[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */ };
The nested braces, which indicate the intended row, are optional. The following initialization is
equivalent to the previous example −
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
Accessing Two-Dimensional Array Elements
An element in a two-dimensional array is accessed by using the subscripts, i.e., row index and
column index of the array. For example −
int val = a[2][3];
Initializing Two-Dimensional Arrays
Multidimensional arrays may be initialized by specifying bracketed values for each row.
Following is an array with 3 rows and each row has 4 columns.
int a[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */ };
The nested braces, which indicate the intended row, are optional. The following initialization is
equivalent to the previous example −
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
Accessing Two-Dimensional Array Elements
An element in a two-dimensional array is accessed by using the subscripts, i.e., row index and
column index of the array. For example −
int val = a[2][3];
Instructions: To perform all other arithmetic operation like addition & subtraction in matrix.
Program:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define CORE 4
#define MAX 4
pthread_t thread[CORE * 2];
int mat_A[MAX][MAX], mat_B[MAX][MAX], sum[MAX][MAX],
sub[MAX][MAX];
void* addition(void* arg)
{
int i, j;
int core = (int)arg;
for (i = core * MAX / 4; i < (core + 1) * MAX / 4;
i++) {
for (j = 0; j < MAX; j++) {
sum[i][j] = mat_A[i][j] + mat_B[i][j];
}
}
}
void* subtraction(void* arg)
{
int i, j;
int core = (int)arg;
for (i = core * MAX / 4; i < (core + 1) * MAX / 4;
i++) {
for (j = 0; j < MAX; j++) {
sub[i][j] = mat_A[i][j] - mat_B[i][j];
}
}
}
int main()
{
int i, j, step = 0;
for (i = 0; i < MAX; i++) {
for (j = 0; j < MAX; j++) {
mat_A[i][j] = rand() % 10;
mat_B[i][j] = rand() % 10;
}
}
printf("\nMatrix A:\n");
for (i = 0; i < MAX; i++) {
for (j = 0; j < MAX; j++) {
printf("%d ", mat_A[i][j]); }
printf("\n");
}
printf("\nMatrix B:\n");
for (i = 0; i < MAX; i++) {
for (j = 0; j < MAX; j++) {
printf("%d ", mat_B[i][j]);
}
printf("\n");
}
for (i = 0; i < CORE; i++) {
pthread_create(&thread[i], NULL, &addition,
(void*)step);
pthread_create(&thread[i + CORE], NULL,
&subtraction, (void*)step);
step++;
}
for (i = 0; i < CORE * 2; i++) {
pthread_join(thread[i], NULL);
}
printf("\n Sum of Matrix A and B:\n");
for (i = 0; i < MAX; i++) {
for (j = 0; j < MAX; j++) {
printf("%d ", sum[i][j]); }
printf("\n");
}
printf("\n Subtraction of Matrix A and B:\n");
for (i = 0; i < MAX; i++) {
for (j = 0; j < MAX; j++) {
printf("%d ", sub[i][j]); }
printf("\n");
}
return 0;
}
Output:
Conclusion:
Sample Viva Questions and Answers:
1) What is the Logic of multiplication.
2) C Program to find Sum of all elements of each row of a matrix.
3) C Program to find sum and subtraction of two matrices.
4) When ArrayIndexOutOfBoundsException occurs?
5) If you do not initialize an array what will happen?
Roll Name of Date of Date of Grade Sign of Sign of
No. Student Performance Evaluation Student Faculty