"C and Pointers" chapter 8th programming exercises 4th:
Modify the Identity_matrix function in the previous question, which can be extended to an array to accept arbitrary-sized matrix parameters. The 1th parameter of the function should be an integer pointer, and you need a 2nd parameter to specify the size of the matrix.
1 /*2 * * Determine if a matrix is a unit matrix3 * * Matrix is arbitrary size4 */5 6#include <stdio.h>7 #defineSIZE 48 9 /*Ten * * Function accepts arbitrary size matrix parameters to determine if it is a unit matrix One * * Formal parameters: A * * The 1th parameter is an integer pointer, pointing to the first element in the matrix &matric[0][0] - * * The 2nd parameter is the size of a matrix - * * return: the * * is the unit matrix, returns 1 - * * Not a unit matrix, return 0 - */ - int +Indentity_matrix_anysize (int*matrix,intmtx_size) - { + intRow, col; A for(row =0; Row < mtx_size; ++row) at for(col =0; Col < mtx_size; ++Col) - { - if(row = = Col && * (Matrix + row * mtx_size + col)! =1 ) - return 0; - if(Row! = Col && * (Matrix + row * mtx_size + col)! =0 ) - return 0; in } - return 1; to } + - int the Main () * { $ intMatrix[size][size];Panax Notoginseng - intI, J; the for(i =0; i < SIZE; ++i) + for(j =0; J < SIZE; ++j) Ascanf"%d", &matrix[i][j]); the +printf"%d", Indentity_matrix_anysize (matrix[0], SIZE)); - $ return 0; $}
Determines whether a matrix of any size is a unit matrix