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

熱點內容
90歲老年人助聽器如何配置 發布:2025-07-02 00:59:16 瀏覽:815
配置ip代理伺服器加速軟體 發布:2025-07-02 00:58:32 瀏覽:693
linux鏈接庫 發布:2025-07-02 00:53:06 瀏覽:676
資料庫的劃分的 發布:2025-07-02 00:43:19 瀏覽:655
補碼源碼和 發布:2025-07-02 00:37:25 瀏覽:979
centos7mysql遠程訪問 發布:2025-07-02 00:35:58 瀏覽:712
有線認證伺服器地址錯誤 發布:2025-07-02 00:33:22 瀏覽:278
本田思域2021款買哪個配置 發布:2025-07-02 00:31:43 瀏覽:326
安卓十二系統什麼時候更新 發布:2025-07-02 00:12:28 瀏覽:346
shell腳本需要編譯鏈接 發布:2025-07-02 00:04:20 瀏覽:476