c++编程题编写程序实现将一个n*n的矩阵的行列互换,在屏幕上输出互换后的矩阵。具体要求如下:

c++编程题编写程序实现将一个n*n的矩阵的行列互换,在屏幕上输出互换后的矩阵。具体要求如下:,第1张

#include<iostream>

#include <iomanip>

using namespace std;

int main() {

int n, tmp;

cin >> n;

int a = new int[n];

for (int i = 0; i < n; i++)

a[i] = new int[n];

for (int i = 0; i < n; i++)

for (int j = 0; j < n; j++)

cin >> a[i][j];

for (int i = 0; i < n; i++)

for (int j = i + 1; j < n; j++) {

tmp = a[i][j];

a[i][j] = a[j][i];

a[j][i] = tmp;

}

cout<<endl;

for (int i = 0; i < n; i++,cout<<endl)

for (int j = 0; j < n; j++)

cout <<setiosflags(ios::left)<< setw(6)<<a[i][j];

for (int i = 0; i < n; i++)

free(a[i]);

return 0;

}

你的错误是 在mat构造时没有为其分配内存空间,从而出错的, 现简单修改了一下,不过有个问题是,要把析构函数中的那句去掉, 在语句 mat=mat1mat2之后, 为什么对象就析构了呢,这个也不是很明白,因为main函数还没结束

#include<iostreamh>

#include<conioh>

#include<mathh>

#include<stringh>

#include<stdlibh>

template<class T>

class CMatrix

{

T Mat;

int nRow;

int nCol;

public:

/添加了一个赋值运算符 运算/

CMatrix<T> operator = (CMatrix<T> mat)

{

nRow = matnRow;

nCol = matnCol;

Mat = new T [nRow];

for(int i=0; i<nRow; i++)

{

Mat[i] = new T;

for(int j=0; j<nCol; j++)

{

Mat[i][j] = matMat[i][j];

}

}

return this;

}

CMatrix(int n,int b, int c, int d){}

CMatrix(T mat,int row,int col)

{

nRow=row;

nCol=col;

for(int i=0;i<row;i++)

for(int j=0;j<nCol;j++)

Mat[i][j]=mat[i][j];

}

CMatrix(int row=0,int col=0,int k=0)

{

nRow=row;

nCol=col;

cout<<"请输入数据:\n";

Mat=new T [row];

for(k;k<col;k++)

Mat[k]=new T;

for(int i=0;i<nRow;i++)

for(int j=0;j<nCol;j++)

{

cout<<"第["<<i+1<<"]["<<j+1<<"]个数据";

cin>>Mat[i][j];

}

}

void show()

{

int i,j;

for(i=0;i<nRow;i++)

for(j=0;j<nCol;j++)

{

cout<<Mat[i][j]<<'\t';

if(j==(nCol-1))cout<<endl;

}

}

friend CMatrix<T> operator (CMatrix<T> &mat1,CMatrix<T> &mat2)

{

CMatrix mat3(1,1,1,1);//添加了一个默认构造函数, 不起其他的作用

mat3 = mat1;

for(int i=0;i<mat1nRow;i++)

for(int j=0;j<mat2nCol;j++)

{

mat3Mat[i][j]=0;

for(int k=0;k<mat1nCol;k)

{

mat3Mat[i][j] += mat1Mat[i][k]mat2Mat[k][j];

k++;

}

}

return mat3;

}

~CMatrix(){/delete []Mat;/}

};

void main()

{

int m,n,i,j,k=0;

cout<<"请输入矩阵行列数:\n";

cout<<"m=";

cin>>m;

cout<<"n=";

cin>>n;

cout<<"i=";

cin>>i;

cout<<"j=";

cin>>j;

cout<<endl;

while(n!=i)

{

cout<<"输入的矩阵不满足条件\n"<<"请重新输入:\n";

cout<<"m=";

cin>>m;

cout<<"n=";

cin>>n;

cout<<"i=";

cin>>i;

cout<<"j=";

cin>>j;

cout<<endl;

}

CMatrix<int> mat1(m,n,k),mat2(i,j,k),mat;

mat1show();

cout << "======================================" << endl;

mat=mat1mat2;

matshow();

cin>>j;

exit(0);

}

可以调用 matrix2() 或 matrix() 做矩阵相乘。

下面主函数 以 调用 matrix() 为例,介绍过程:

输入参数,动态分配数组,输入矩阵数据,调用,输出,释放内存。

#include<iostream>

using namespace std;

#include<stdioh>

/---------------------

c[j]][i] = a[j][k] b[k][i] = c[j][i]

a[ny][nk]

b[nk][nx]

c[ny][nx]

---------------------/

void matrix2(int a,int b, int c, int nx, int ny, int nk)

{

int i,j,k;

for(j=0;j<ny;j++){

for(i=0;i<nx;i++){

c[jnx+i]=0;

for(k=0;k<nk;k++)c[jnx+i]+= a[jnk+k] b[knx+i];

};

};

}

/---------------------

b[j][k] c[k][i] = a[j][i]

---------------------/

void matrix(int b,int c, int a, int nx, int ny, int nk)

{

int i,j,k;

for (j=0;j<ny;j++)for(i=0;i<nx;i++)a[j][i]= 0;

for(j=0;j<ny;j++){

for(i=0;i<nx;i++){

for(k=0;k<nk;k++)a[j][i]+= b[j][k]c[k][i];

};

};

}

int main()

{

int i,j,k,tmp;

int b_row,b_col;

int c_row,c_col;

int a_row,a_col;

int b,c,a;

printf("please enter b_row b_col of matrix B\n");

scanf("%d %d",&b_row,&b_col);

c_row = b_col;

printf("please enter c_col of matrix C\n");

scanf("%d",&c_col);

a_row = b_row;

a_col = c_col;

a = (int ) malloc(sizeof(int ) a_row);

for (j=0;j<a_row;j++){

a[j] = (int ) malloc(sizeof(int) a_col);

}

b = (int ) malloc(sizeof(int ) b_row);

for (j=0;j<b_row;j++){

b[j] = (int ) malloc(sizeof(int) b_col);

}

c = (int ) malloc(sizeof(int ) c_row);

for (j=0;j<c_row;j++){

c[j] = (int ) malloc(sizeof(int) c_col);

}

if (!c[c_row-1]) {

printf("no enought memory\n");exit(0);

}

printf("Please input int matrix b[%d][%d]\n",b_row,b_col);

for (j=0;j<b_row;j++)

for (i=0;i<b_col;i++){

scanf("%d",&tmp);

b[j][i] = tmp;

}

printf("Please input int matrix c[%d][%d]\n",c_row,c_col);

for (j=0;j<c_row;j++)

for (i=0;i<c_col;i++){

scanf("%d",&tmp);

c[j][i] = tmp;

}

matrix( b ,c,a, a_col, a_row, b_col);

for(j=0;j<a_row;j++)

{

for (i=0;i<a_col;i++) printf("%d ",a[j][i]);

printf("\n");

};

for (i=0;i<a_row;i++) free((void )a[i]); free((void )a);

for (i=0;i<b_row;i++) free((void )b[i]); free((void )b);

for (i=0;i<c_row;i++) free((void )c[i]); free((void )c);

return 0;

}

数据和结果例子:

please enter b_row b_col of matrix B

3 2

please enter c_col of matrix C

3

Please input int matrix b[3][2]

1 2 3 4 5 6

Please input int matrix c[2][3]

1 2 3 4 5 6

9 12 15

19 26 33

29 40 51

以上就是关于c++编程题编写程序实现将一个n*n的矩阵的行列互换,在屏幕上输出互换后的矩阵。具体要求如下:全部的内容,包括:c++编程题编写程序实现将一个n*n的矩阵的行列互换,在屏幕上输出互换后的矩阵。具体要求如下:、c++程序设计问题,关于矩阵的~ 拜托高手了~谢谢、编写求矩阵相乘的程序,C++等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/9813986.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-02
下一篇 2023-05-02

发表评论

登录后才能评论

评论列表(0条)

保存