注册 登录
编程论坛 C++教室

新手上路,各位帮我看看这到底哪出错了。

白小乙 发布于 2012-10-24 15:37, 480 次点击
#include<iostream>
using namespace std;
#include<stdlib.h>            //包含函数srand的头文件
#include<time.h>              //包含函数time的头文件
#define M 5                     //定义两个常量
#define N 5
int a[M][N],c[M][N];           //定义二维数组a、c为全局变量,用来存放矩阵
void trans(int b[M][N]);       //函数声明
void main()
{int i,j;
srand((unsigned)time(NULL));    //产生一个时间
for(i=0;i<M;i++)                //随机产生数组a中每一个元素的值
for(j=0;j<N;j++)
*(*(a+i)+j)=rand()%100;          //将一个0~99之间的随机数赋予当前元素
cout<<"自动生成矩阵为: \n";
for(i=0;i<M;i++)                //输出自动生成5*5阶矩阵
{for(j=0;j<N;j++)
cout<<'\t'<<*(*(a+i)=j);
cout<<endl;
}
trans(a);                  //调用求转置矩阵的函数
cout<<"转置矩阵为: \n";  
for(i=0;i<M;i++)             //输出转置后的矩阵        
{for(j=0;j<N;j++)
cout<<'\t'<<*(*(c+i)+j);
cout<<endl;
}
}
viod trans(int b[M][N])         //定义求转置矩阵的函数
{int i,j;
int(*p)[M];                      // 定义指向一维数组的指针P
p=c;                                     // 将数组c的首地址赋值给P
for(i=0;i<M;i++)                        //实现矩阵转置
for(j=0;j<N;j++)
*(*(p+i)+j)=*(*(b+j)+i);
}
4 回复
#2
白小乙2012-10-24 15:42
08写,和6.0不同,
#3
白小乙2012-10-24 16:13
问错地方了还是?没人理我
#4
rjsp2012-10-24 16:13
你写天书呀

程序代码:
#include <ctime>
#include <iostream>
using namespace std;

const size_t M = 4;
const size_t N = 5;
void trans( const int a[M][N], int b[N][M] );

int main()
{
    int a[M][N];
    srand( (unsigned)time(NULL) );
    for( size_t i=0; i<M; ++i )
        for( size_t j=0; j<N; ++j )
            a[i][j] = rand()%100;

    cout<<"自动生成矩阵为: \n";
    for( size_t i=0; i<M; ++i )
    {
        for( size_t j=0; j<N; ++j )
            cout << '\t' << a[i][j];
        cout << '\n';
    }

    int b[N][M];
    trans( a, b );

    cout<<"转置矩阵为: \n";

    for( size_t i=0; i<N; ++i )
    {
        for( size_t j=0; j<M; ++j )
            cout << '\t' << b[i][j];
        cout << '\n';
    }

    return 0;
}

void trans( const int a[M][N], int b[N][M] ) // 矩阵转置
{
    for( size_t i=0; i<M; ++i )
        for( size_t j=0; j<N; ++j )
            b[j][i] = a[i][j];
}

#5
白小乙2012-10-24 16:29
有点懂了,感谢版主,不好意思,新手第一次来论坛,不会,版主见谅。
1