Explanation
The program takes the size of a matrix and the values of the matrix from the user, then prints the matrix.
Header
The code begins with including the standard input-output header file using #include <stdio.h>
.
Variable Declaration
The main()
function is defined, and several variables are declared:
int d
: Stores the number of rows in the matrix.int n
: Stores the number of columns in the matrix.int e[d][n]
: Declares a 2D array withd
rows andn
columns to hold the matrix values.
Program Description
The program prints a description message for the user to understand its functionality.
User Input
The user is prompted to enter the number of rows and columns, and then the elements of the matrix:
printf("Enter the number of rows: ");
: Prompts the user to enter the number of rows.scanf("%d", &d);
: Reads the number of rows and stores it ind
.printf("Enter the number of columns: ");
: Prompts the user to enter the number of columns.scanf("%d", &n);
: Reads the number of columns and stores it inn
.int e[d][n];
: Declares a 2D array withd
rows andn
columns.printf("Enter the elements of the matrix:\n");
: Prompts the user to enter the elements of the matrix.for (int i = 0; i < d; i++)
: Outer loop to iterate over each row.for (int j = 0; j < n; j++)
: Inner loop to iterate over each column.scanf("%d", &e[i][j]);
: Reads each element of the matrix and stores it in the array.
Matrix Printing
The program prints the matrix:
for (int i = 0; i < d; i++)
: Outer loop to iterate over each row.for (int j = 0; j < n; j++)
: Inner loop to iterate over each column.printf("%d ", e[i][j]);
: Prints each element of the matrix followed by a space.printf("\n");
: Prints a newline after each row.
Program End
The program ends with return 0;
to indicate successful execution.
Code
/*
* -----------------------------------------------------------
* Logic Building with Computer Programming (CSU1128)
* Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
*
* This code is a part of the educational initiative by dmj.one
* with aim of empowering and inspiring learners in the field of
* Computer Science and Engineering through the respective courses.
*
* (c) 2022, Divya Mohan for dmj.one. All rights reserved.
* -----------------------------------------------------------
*/
#include <stdio.h>
#include <string.h>
int main()
{
printf("\n\n Program to take the matrix size and then take the value of matrix entered by the user and print them. Example: 2 2 |1 2 3 4| = |1 2 3 4| \n\n");
// Read the number of rows and columns from the user
printf("Enter the number of rows: ");
int d;
scanf("%d", &d);
printf("Enter the number of columns: ");
int n;
scanf("%d", &n);
// Declare a 2D array with d rows and n columns
int e[d][n];
// Read the elements of the matrix from the user
printf("Enter the elements of the matrix:\n");
for (int i = 0; i < d; i++)
{
for (int j = 0; j < n; j++)
{
scanf("%d", &e[i][j]);
}
}
// Print the values of the array
for (int i = 0; i < d; i++)
{
for (int j = 0; j < n; j++)
{
printf("%d ", e[i][j]);
}
printf("\n"); // Print a newline after each row
}
return 0;
}
Output
Program to take the matrix size and then take the value of matrix entered by the user and print them. Example: 2 2 |1 2 3 4| = |1 2 3 4|
Enter the number of rows: 2
Enter the number of columns: 2
Enter the elements of the matrix:
5 6 3 2
5 6
3 2