zoukankan      html  css  js  c++  java
  • 编程基本功——矩阵的转置运算

    一、分析

        可以用一个二维数组存放矩阵的数据,通过将二维数组的指针作为参数传递实现矩阵转置。

        二维数组指针的传递,实参是数组名,形参一定是如(*a)[n]形式的,其中n表示该二维数组每行的元素个数,也就是列数。

    二、源码

       1: #include "stdio.h"
       2:  
       3: void InputMatrix(int (*a)[4], int , int );
       4: void OutputMatrix(int (*b)[3], int , int );
       5: void MatrixTranspose(int (*a)[4], int (*b)[3] );
       6:  
       7: int main()
       8: {
       9:     int a[3][4], b[4][3];
      10:     printf("please input 3*4 matrix\n");
      11:     InputMatrix(a, 3, 4);
      12:     MatrixTranspose(a, b);
      13:     printf("The Transpose Matrix is\n");
      14:     OutputMatrix(b, 4, 3);
      15:     getchar();
      16:     return 0;
      17: }
      18:  
      19: void InputMatrix(int (*a)[4], int n, int m)
      20: {
      21:     int i , j;
      22:     for (i = 0; i < n; ++i)
      23:     {
      24:         for (j = 0; j < m; ++j)
      25:         {
      26:             scanf("%d", *(a+i)+j);
      27:         }
      28:     }
      29: }
      30:  
      31: void OutputMatrix(int (*b)[3], int n, int m)
      32: {
      33:     int i , j;
      34:     for (i = 0; i < n; ++i)
      35:     {
      36:         for (j = 0; j < m; ++j)
      37:         {
      38:             printf("%d ", *(*(b+i)+j));
      39:         }
      40:         printf("\n");
      41:     }
      42: }
      43:  
      44: void MatrixTranspose(int (*a)[4], int (*b)[3] )
      45: {
      46:     int i, j;
      47:     for (i = 0; i < 4; ++i)
      48:     {
      49:         for (j = 0; j <3; ++j)
      50:         {
      51:             b[i][j] = a[j][i];
      52:         }
      53:     }
      54: }
  • 相关阅读:
    C++——STL内存清除
    c++——智能指针学习(unique_ptr)
    linux下将tomcat加入服务
    linux下oracle远程连接的问题
    oracle计算容量的方式
    oracle删除表的方式
    阻塞与非阻塞的区别
    java中queue的使用
    yum源
    VMware Tools 安装
  • 原文地址:https://www.cnblogs.com/steven_oyj/p/1742481.html
Copyright © 2011-2022 走看看