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

熱點內容
it天空解壓密碼 發布:2024-07-27 09:50:39 瀏覽:548
軟體腳本買賣 發布:2024-07-27 09:50:38 瀏覽:916
android對象轉json 發布:2024-07-27 09:50:15 瀏覽:182
安卓平板有什麼可以畫對稱的 發布:2024-07-27 09:36:03 瀏覽:132
羊創意腳本 發布:2024-07-27 09:29:30 瀏覽:894
榮耀v20升級存儲 發布:2024-07-27 09:20:19 瀏覽:485
安卓用什麼和電腦傳圖片 發布:2024-07-27 09:02:07 瀏覽:288
存儲過程就是 發布:2024-07-27 08:56:51 瀏覽:131
c語言高級試題 發布:2024-07-27 08:48:30 瀏覽:282
ip伺服器世界上有幾台 發布:2024-07-27 08:46:18 瀏覽:394