當前位置:首頁 » 編程語言 » c語言矩陣轉置34

c語言矩陣轉置34

發布時間: 2024-05-30 08:34:34

① 用C語言編寫一個矩陣轉置的函數,矩陣的行數和列數在程序中由用戶輸入,請問怎麼寫,非常感謝

我的代碼邏輯是:

矩陣行指針初值指向每行首地址,迭代依次取所有行指針指向值組成新行,所有行指針自增。最終組合新的矩陣。

#include<stdio.h>
#include<malloc.h>
int**getList(introw,intclo);//獲取矩陣地址空間
voidsetNum(int**nList,intn);//填寫數值
voidprtList(int**nList,introw,intclo);//列印矩陣
int**zz(int**nList,introw,intclo);//轉置函數

intmain()
{
introw,clo,**nList=NULL,**nListSave=NULL;

printf("輸入矩陣行列數:");
scanf("%d%d",&row,&clo);
nList=getList(row,clo);
setNum(nList,row*clo);

printf("輸入的矩陣為: ");
prtList(nList,row,clo);

printf("轉置後的矩陣為: ");
nListSave=zz(nList,row,clo);
free(nList);
nList=nListSave;
prtList(nList,clo,row);
return0;
}

int**zz(int**nList,introw,intclo)
{
int*nSave=NULL,**listSave=NULL,**listp=nList,*p=NULL,i,j;
nSave=(int*)malloc(sizeof(int)*row*clo);
listSave=(int**)malloc(sizeof(int*)*clo);//倒置後的矩陣
p=nSave;

for(j=0;j<clo;j++)
{
for(i=0;i<row;i++)
{
*p++=*listp[i];
listp[i]=listp[i]+1;
}
}
for(i=0;i<clo;i++)
listSave[i]=&nSave[i*row];

for(i=0;i<row;i++)
free(nList[i]);//釋放原矩陣行空間
returnlistSave;
}
voidprtList(int**nList,introw,intclo)
{
inti,j;
for(i=0;i<row;i++)
{
for(j=0;j<clo;j++)
printf("%d",nList[i][j]);
printf(" ");
}
}
voidsetNum(int**nList,intn)
{
int*p=nList[0];
printf("填寫矩陣中%d個數值: ",n);
while(n-->0)
scanf("%d",p++);
}
int**getList(introw,intclo)
{
int*nums,**nList,i;
nums=(int*)malloc(sizeof(int)*row*clo);
nList=(int**)malloc(sizeof(int*)*row);
for(i=0;i<row;i++)
nList[i]=&nums[i*clo];
returnnList;
}

② C語言的矩陣轉置

#include "StdAfx.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define length 3//指定長度
/*裝置矩陣*/
void transposition(int Array[length][length])
{
int ArrayTemp[length][length];
for (int i = 0;i < length;i++)
{
for (int j = 0;j < length;j++)
{
ArrayTemp[j][i] = Array[i][j];
}
}
for (i = 0;i < length;i++)
{
for (int o =0;o < length;o++)
{
Array[i][o] = ArrayTemp[i][o];
}
}
return;
}
main()
{
int i,j;
/*printf("please input length\n");
scanf("%d",&length)*/
int Array[length][length];
printf("please input transposition\n");
for (i = 0;i < length;i++)
{
for (j = 0;j < length;j++)
{
scanf("%d",&Array[i][j]);
}
}
for (i = 0;i < length;i++)
{
for (j = 0;j < length;j++)
{
printf("%3d",Array[i][j]);
}
printf("\n");
}
printf("\n\n");
transposition(Array);
for (i = 0;i < length;i++)
{
for (j = 0;j < length;j++)
{
printf("%3d",Array[i][j]);
}
printf("\n");
}
system("pause");
return 0;
}

熱點內容
linux下ntp伺服器搭建 發布:2024-09-08 08:26:46 瀏覽:742
db2新建資料庫 發布:2024-09-08 08:10:19 瀏覽:171
頻率計源碼 發布:2024-09-08 07:40:26 瀏覽:778
奧迪a6哪個配置帶後排加熱 發布:2024-09-08 07:06:32 瀏覽:101
linux修改apache埠 發布:2024-09-08 07:05:49 瀏覽:209
有多少個不同的密碼子 發布:2024-09-08 07:00:46 瀏覽:566
linux搭建mysql伺服器配置 發布:2024-09-08 06:50:02 瀏覽:995
加上www不能訪問 發布:2024-09-08 06:39:52 瀏覽:811
銀行支付密碼器怎麼用 發布:2024-09-08 06:39:52 瀏覽:513
蘋果手機清理瀏覽器緩存怎麼清理緩存 發布:2024-09-08 06:31:32 瀏覽:554