當前位置:首頁 » 編程語言 » 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;
}

熱點內容
lol頭像在那個文件夾 發布:2025-08-28 20:02:14 瀏覽:383
安卓手機如何轉換聲音模式 發布:2025-08-28 19:55:05 瀏覽:484
樂視手機開機密碼忘了怎麼解鎖 發布:2025-08-28 19:54:18 瀏覽:242
網路爬蟲java源碼 發布:2025-08-28 19:49:06 瀏覽:10
文章的資料庫 發布:2025-08-28 19:20:16 瀏覽:424
shell腳本mysql資料庫 發布:2025-08-28 19:19:20 瀏覽:454
c語言kill 發布:2025-08-28 17:57:24 瀏覽:743
java中i 發布:2025-08-28 17:50:58 瀏覽:873
python強制類型轉換int 發布:2025-08-28 17:50:55 瀏覽:67
ftp未授權訪問修復 發布:2025-08-28 17:29:44 瀏覽:649