Determines whether a matrix of any size is a unit matrix

Source: Internet
Author: User

"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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.