This program shows how to dynamically construct a two-dimensional array that can specify the number of rows and columns of an array:
1 int**make2darray (intRowsintcols) {2 int**x,i;3X=malloc (rows*sizeof(*x));4 for(i=0; i<rows;i++){5X[i]=malloc (cols*sizeof(**x));6 }7 returnx;8}
A two-dimensional array can be manipulated as a matrix with special matrices such as triangular matrices, sparse matrices, and unit matrices.
The program is used to determine whether a matrix is a unit matrix:
1#include <stdio.h>2 #defineN 43 intMainvoid)4 {5 intI,j,a[n][n];6 intCount=0;7 for(i=0; i<n;i++){8 for(j=0; j<n;j++){9scanf"%d",&A[i][j]); Ten } One } A for(i=0; i<n;i++){ - for(j=0; j<n;j++){ - if(i==j) { the if(a[i][j]==1){ -count++; - } - } + Else{ - if(a[i][j]==0){ +count++; A } at } - - } - } - if(count==n*N) { -printf"Yes"); in } - Else{ toprintf"No"); + } - return 0; the}
where n in the macro definition is the column of the matrix, you can set your own size according to your own needs.
2016-10-24 12:12:49
Two-dimensional arrays