Create a base matrix in C (user input!)

I'm trying to ask the user to enter the number of columns and rows that they want in the matrix, and then enter the values ​​in the matrix ... I'm going to let them insert numbers one row at a time.

How to create such a function?

#include<stdio.h> main(){ int mat[10][10],i,j; for(i=0;i<2;i++) for(j=0;j<2;j++){ scanf("%d",&mat[i][j]); } for(i=0;i<2;i++) for(j=0;j<2;j++) printf("%d",mat[i][j]); } 

This works for entering numbers, but displays them all on one line ... The problem here is that I don’t know how many columns or rows the user needs, so I can’t print% d% d% d in matrix form ...

Any thoughts?

Thanks:)

+4
source share
7 answers

What about the next one?

First, ask the user about the number of rows and columns, save this, say, nrows and ncols (i.e. scanf("%d", &nrows); ), and then allocate memory for a 2D array of size nrows x ncols. Thus, you can have a size matrix defined by the user and not be fixed in any format you measured!

Then save the elements with for(i = 0;i < nrows; ++i) ... and show the elements in the same way, except that you drop newline characters after each line, i.e.

 for(i = 0; i < nrows; ++i) { for(j = 0; j < ncols ; ++j) { printf("%d\t",mat[i][j]); } printf("\n"); } 
+10
source

You need to dynamically highlight your matrix. For instance:

 int* mat; int dimx,dimy; scanf("%d", &dimx); scanf("%d", &dimy); mat = malloc(dimx * dimy * sizeof(int)); 

This creates a linear array that can hold the matrix. At this point, you can decide whether you want to access the column or row first. I would suggest making a quick macro that calculates the correct offset in the matrix.

+2
source

required

 for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf("%d",mat[i][j]); } printf("\n"); } 
+1
source
 #include<stdio.h> int main(void) { int mat[10][10],i,j; printf("Enter your matrix\n"); for(i=0;i<2;i++) for(j=0;j<2;j++) { scanf("%d",&mat[i][j]); } printf("\nHere is your matrix:\n"); for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf("%d ",mat[i][j]); } printf("\n"); } } 
+1
source

This is my answer

 #include<stdio.h> int main() {int mat[100][100]; int row,column,i,j; printf("enter how many row and colmn you want:\n \n"); scanf("%d",&row); scanf("%d",&column); printf("enter the matrix:"); for(i=0;i<row;i++){ for(j=0;j<column;j++){ scanf("%d",&mat[i][j]); } printf("\n"); } for(i=0;i<row;i++){ for(j=0;j<column;j++){ printf("%d \t",mat[i][j]);} printf("\n");} } 

I just select the approximate value for row and column. My selected row or column does not intersect the value. Then I look at the matrix element, then make it the size of the matrix.

+1
source
 int rows, cols , i, j; printf("Enter number of rows and cols for the matrix: \n"); scanf("%d %d",&rows, &cols); int mat[rows][cols]; printf("enter the matrix:"); for(i = 0; i < rows ; i++) for(j = 0; j < cols; j++) scanf("%d", &mat[i][j]); printf("\nThe Matrix is:\n"); for(i = 0; i < rows ; i++) { for(j = 0; j < cols; j++) { printf("%d",mat[i][j]); printf("\t"); } printf("\n"); } 

}

0
source
 //R stands for ROW and C stands for COLUMN: //i stands for ROW and j stands for COLUMN: #include<stdio.h> int main(){ int M[100][100]; int R,C,i,j; printf("Please enter how many rows you want:\n"); scanf("%d",& R); printf("Please enter how column you want:\n"); scanf("%d",& C); printf("Please enter your matrix:\n"); for(i = 0; i < R; i++){ for(j = 0; j < C; j++){ scanf("%d", &M[i][j]); } printf("\n"); } for(i = 0; i < R; i++){ for(j = 0; j < C; j++){ printf("%d\t", M[i][j]); } printf("\n"); } getch(); return 0; } 
-2
source

Source: https://habr.com/ru/post/1308938/


All Articles