码迷,mamicode.com
首页 > 编程语言 > 详细

C数组实现矩阵的转置

时间:2015-04-14 18:04:48      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:c   实例   矩阵转置   数组   

直接上代码,在代码中有对矩阵的学习,包括初始化学习以及如何使用等。

#include <stdio.h>

/**
 * 给出提示,要求输入数组A
 * ,通过二维数组,进行数组的转置
 * 得出数组B,输出结果
 *
 * 该实例主要是为了进行学习二维数组
 * @brief main
 * @return
 */
int main(void)
{
    /**
     * 二维数组的初始化:
     * 1:分行给二维数组赋值
     *  static int a[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
     *
     * 2:将所有数据写在一个大括号中
     * static int a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
     *
     * 3:对数组进行部分赋值
     * static int a[3][4] = {{1},{2},{3}};
     * 相当于该数组为
     *  1 0 0 0
     *  2 0 0 0
     *  3 0 0 0
     */

    //下面进行实例编写
    int row,colume;

    printf("Please the number of row and colume of the array(divided by ‘,‘):\n");
    scanf("%d,%d",&row,&colume);  //获取输入的行数和列数

    //定义数组A
    int array[row][colume];

    int i,j;

    //获取用户的输入来填充数组A
    for(i = 0;i < row;i++){
        for(j = 0;j < colume;j++){
            printf("Please enter the number in (%d,%d):\n",i,j);

            scanf("%d",&array[i][j]);
        }
    }

    //定义数组B
    int MatrixB[colume][row];

    //进行转置
    /**
     *  两个数组如果相互转置的话,
     *  则一个数组的行等于另一个数组的列
     *  一个数组的列等于另一个数组的行
     *  注意:
     *  转置之后的矩阵的行数和列数为转置之前的列数和行数
     */
    for(i = 0;i < colume;i++){
        for(j = 0;j < row;j++){
            MatrixB[i][j] = array[j][i];
        }
    }

    //输出矩阵B
    for(i = 0;i < colume;i++){
        for(j = 0;j < row;j++){
            printf("%d\t",MatrixB[i][j]);
        }

        printf("\n");
    }

    return 0;
}

C数组实现矩阵的转置

标签:c   实例   矩阵转置   数组   

原文地址:http://blog.csdn.net/hongbochen1223/article/details/45045561

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!