C Programming - Multidimensional Arrays, Exercises and Strings
Declaring an Multi Dimensional Array and Display the values in the array
#include<stdio.h>
int main()
{
int a[2][4] = { {22,25,27,29},{33,35,38,39} };
int i = 0, j = 0;
for (i = 0; i < 2; i++)
{
for (j = 0; j < 4; j++)
{
printf("%d", a[i][j]);
printf(" ");
}
printf("\n");
}
system("pause");
return 0;
}
Output
Addition of Two Matrices in C
#include<stdio.h>
int main()
{
int a[2][2] = {0};
int b[2][2] = { 0 };
int result[2][2] = { 0 };
int i = 0, j = 0;
printf("Enter the values for matrix1\n");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("Enter the values for matrix 2\n");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
scanf("%d", &b[i][j]);
}
}
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
result[i][j] = a[i][j] + b[i][j];
}
}
printf("The resultant matrix is\n");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
printf("%d", result[i][j]);
printf(" ");
}
printf("\n");
}
system("pause");
return 0;
}
Output
Check if a matrix is an identity matrix
#include<stdio.h>
int main()
{
int a[2][2] = { 0 };
int i = 0, j = 0,identity=0;
printf("Enter the values for matrix1");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
scanf("%d", &a[i][j]);
}
}
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
if ((i == j) && (a[i][j] != 1))
{
identity = 1;
printf("not an identity matrix");
break;
}
if ((i != j) && (a[i][j] != 0))
{
identity = 1;
printf("not an identity matrix");
break;
}
}
}
if (identity == 0)
{
printf("Its an identity matrix");
}
system("pause");
return 0;
}
Output
Finding the transpose of a matrix
#include<stdio.h>
int main()
{
int a[2][2] = { 0 };
int b[2][2] = { 0 };
int i = 0, j = 0,identity=0;
printf("Enter the values for matrix1");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
scanf("%d", &a[i][j]);
}
}
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
b[i][j] = a[j][i];
}
}
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
printf("%d", b[i][j]);
}
}
system("pause");
return 0;
}
Output
Printing the average marks for each student
#include<stdio.h>
int main()
{
float a[3][5];
int i = 0, j = 0; float sum = 0;
float average = 0;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 5; j++)
{
scanf("%f", &a[i][j]);
}
}
for (i = 0; i < 3; i++)
{
for (j = 0; j < 5; j++)
{
sum += a[i][j];
}
average = (float)sum / 5;
printf("The average %d student is %f\n",i+1, average);
sum = 0;
}
system("pause");
return 0;
}
Output
C Code for Matrix Multiplication
int main()
{
int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k;
printf("Enter rows and column for first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d %d",&r2, &c2);
// Column of first matrix should be equal to column of second matrix and
while (c1 != r2)
{
printf("Error! column of first matrix not equal to row of second.\n\n");
printf("Enter rows and column for first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d %d",&r2, &c2);
}
// Storing elements of first matrix.
printf("\nEnter elements of matrix 1:\n");
for(i=0; i<r1; ++i)
for(j=0; j<c1; ++j)
{
printf("Enter elements a%d%d: ",i+1, j+1);
scanf("%d", &a[i][j]);
}
// Storing elements of second matrix.
printf("\nEnter elements of matrix 2:\n");
for(i=0; i<r2; ++i)
for(j=0; j<c2; ++j)
{
printf("Enter elements b%d%d: ",i+1, j+1);
scanf("%d",&b[i][j]);
}
// Initializing all elements of result matrix to 0
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
result[i][j] = 0;
}
// Multiplying matrices a and b and
// storing result in result matrix
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
for(k=0; k<c1; ++k)
{
result[i][j]+=a[i][k]*b[k][j];
}
// Displaying the result
printf("\nOutput Matrix:\n");
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
printf("%d ", result[i][j]);
if(j == c2-1)
printf("\n\n");
}
return 0;
}
Output