C Programming : Learn the Concept of Matrix

in c-programming •  2 years ago 

Welcome back to my blog,
We have learnt alot about Arrays and as we have learnt enough today we will move on to a next level that is now we will see how to initialize or create a 2D Array or a Matrix using C Programming language.

You can see the program below :

Screenshot 2023-01-24 203332.png

You can copy the code below :

#include <stdio.h>
#include <stdlib.h>

int main() {
int rows, columns;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &columns);

int matrix[rows][columns];

for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
printf("Enter element at position (%d, %d): ", i+1, j+1);
scanf("%d", &matrix[i][j]);
}
}

printf("The matrix is:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}

return 0;
}

Explanation of the above Program

image.png

Source

This C program creates a matrix by first prompting the user to enter the number of rows and columns for the matrix. It then uses two nested for loops to fill the matrix with elements entered by the user. The first for loop will iterate over the rows while second for loop will iterate over the columns.

The program uses the scanf() function to read the number of rows and columns entered by the user, and to read the elements of the matrix. The scanf() function takes two arguments:

the first is a format string that tells it what type of data to expect, and the second is the memory address of a variable where the data will be stored. In this case, the format string is "%d", which tells scanf() to expect an integer, and the memory address of the variables rows and columns for the number of rows and columns, and matrix[i][j] for the elements of the matrix.

The program then uses a nested for loop to print the matrix, using the printf() function. The first for loop will iterate over the rows while second for loop will iterate over the columns.

The printf() function is used to print the value of each element of the matrix, followed by a space. After each row, the program prints a newline character \n to start a new row on the next line.

Output of the Above Program

Screenshot 2023-01-24 152842.png

The program prompts the user to enter the number of rows and columns for the matrix, and then uses nested loops to fill the matrix with elements entered by the user. Finally, it prints out the matrix with the elements entered by the user

If you also want to learn coding then find your teacher according to your language. For me i am comfortable with hindi language and follow hindi teachers.

You can refer to my teacher below in case you are also comfortable with hindi.

Source

I will soon call myself a trader and a small programmer together.

Happy trading and keep learning what you love.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE BLURT!