當前位置:首頁 » 編程語言 » nn階矩陣c語言
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

nn階矩陣c語言

發布時間: 2022-05-24 01:39:13

Ⅰ n階矩陣(所有數據為整數),求對角線元素的和及四周元素的和。(n由鍵盤輸入) 用c語言

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

int main()

{int i,j,e,N,a[20][20],s1=0,s2=0; // 存儲四周數據之和

srand(time(NULL));

printf("請輸入矩陣階數:");

scanf("%d",&N);

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

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

a[i][j]=rand()%9+1;

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

{s1+=a[i][i]+a[i][N-1-i];

if(i==N-1)break;

s2+=a[0][i]+a[i][N-1]+a[N-1][N-1-i]+a[N-1-i][0];

}

// 輸出數據

printf(" 矩陣數據為: ");

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

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

printf("%4d",a[i][j]);

printf(" ");

}

if(N%2)s1-=a[N/2][N/2];

printf(" 對角線之和為: %d", s1);

printf(" 四邊之和為: %d", s2);

return 0;

}

Ⅱ 用C語言解決:求任意階(n階)矩陣的行列式

很遺憾,上面匿名的程序不正確。
比如n=2 輸入:
3---7
2---1
得出錯誤結果。
而當輸入n=3
0---1---3
3---0---2
5---2---0
時也會得出錯誤結果。
錯誤的原因有2:
1 是數據類型不對,匿名的程序是設定輸入都是整數int,顯然按照行列式的定義結果肯定是整數,但是他程序中使用了整型數的除法,結果是取整整數,雖然他使用了類型強制轉換,但結果顯然不同,有誤差而且有時候這個誤差很大形成錯誤。
2 是演算法有點問題。小可很欣賞匿名的演算法思路,簡潔明快。不過有相當缺陷,這在程序中註明。
下面的程序是在匿名的程序思路上改寫的。考慮到數據類型和精確度問題,程序中行列式數據使用double型。由於tc和win-tc是16位編輯器,對float型和double型數據支持不好,所以程序是在32位編輯器Dev-c++下調試並通過的。
本題的一個完整的c程序如下,程序在Dev-c++下都調試通過,結果正確。
/* 用C語言解決:求任意階(n階)矩陣的行列式值 */
#include <stdio.h>
#include <math.h>

void getarray(int n);
void showarray(int n);
double getresult(int n);
double array[10][10];/*設矩陣不超過10階,可更改*/

int main()
{
int n;
double result;
printf("\nPlease input the Array size n:");
scanf("%d",&n);
getarray(n);
showarray(n);
result=getresult(n);
printf("\nResult=%f\n",result);
system("pause");
return 0;
}

void getarray(int n)
{
int row,col;
for(row=0;row<n;row++)
{
printf("\nPlease input line %d:",row+1);
for(col=0;col<n;col++)
scanf("%lf",&array[row][col]);
}
}

void showarray(int n)
{
int row,col;
printf("\nA=");
for(row=0;row<n;row++)
{
for(col=0;col<n;col++)
printf("\t%f",array[row][col]);
printf("\n");
}
}

double getresult(int n)
{
double temp,result=1.0;
int switchtime=0,flag=0;
int row,nextrow,col,stemp;
for(row=0;row<n-1;row++)
{
nextrow=row+1;
if(array[row][row]==0)/* 開始處理第一列,如果行列式第一行第一個數為零,要交換行 */
{ while(array[nextrow][row]==0)
{
nextrow++; /* 如果行列式第二行第一個數為零,行增加繼續尋找非零數值的行 */
if(nextrow==n)/* 如果遍歷完行列式行列式第一列元素都為零,退出while循環 */
{ flag=1;
break;
}
}
if(flag==1) /* 退出while循環後回到for(row=0;row<n-1;row++)行加1?*/
continue; /* 從array[row][row]==0知列也相應加1,開始處理第二列 */
switchtime++; /* 每交換一次行,行列式符號變化1次,統計變化次數 */
for(col=0;col<n;col++) /* 交換非零行到行列式頂部 */
{
stemp=array[row][col];
array[row][col]=array[nextrow][col];
array[nextrow][col]=stemp;
}
}
for(nextrow=row+1;nextrow<n;nextrow++)
{ /* 類似高斯消去法,消第一行下各行第一列數值到零*/
temp=array[nextrow][row]/array[row][row];
for(col=0;col<n;col++)
array[nextrow][col]+=-temp*array[row][col];/* 化行列式為上三角行列式形式 */
}
}
showarray(n);
for(row=0;row<n;row++)
result*=array[row][row];
if(switchtime%2)
return -result;
else
return result;
}

Ⅲ 用c語言設計一n階方陣!急!!

#include <stdio.h>
#include<malloc.h>
void matrix(int x,int y) //這里都是向一個方向發展的。
{
int **a;
a = (int **)malloc(x*sizeof(int)); //分配空間。
int num = x+y-1;
int k;
for(k=0;k<x;k++)
{
a[k] = (int *)malloc(y*sizeof(int));
}
int i;
int j;
int start=1; //從1開始的。元素的起始值。
for(k=0;k<num;k++)
{
for(i=0;i<y;i++) //這是列坐標,因為列是在行變後才變的。
{
for(j=0;j<x;j++) //這是行坐標。
{
if(i+j == k)
{
a[j][i] = start;
//printf("%d\n",a[j][i]);
start++;
}
}
}
//printf("k=%d\n",k);
}
for(i=0;i<x;i++) //輸出矩陣。
{
for(j=0;j<y;j++)
{
printf("%3d",a[i][j]);
}
printf("\n");
}
for(k=0;k<x;k++) //釋放空間。
{
free(a[k]);
}
free(a);
}
int main()
{
int n;
printf("please input (1-9):\n");
scanf("%d",&n);
matrix(n,n);
printf("\n");
matrix(3,5);
getchar();
getchar();
return 0;
}

Ⅳ 用c語言編寫一個n階矩陣

#include<stdio.h>
#include<stdlib.h>

int main()
{
int n,**p,i,j;
scanf("%d",&n);
p=new int*[n];
for(i=0;i<n;i++)
{
p[i]=new int[n];
for(j=0;j<n;j++) scanf("%d",&p[i][j]);
}

for(i=0;i<n;i++)
{
printf("%d",p[i][0]);
j=1;
while(j<n)
{
printf(" %d",p[i][j]);
j++;
}
printf("\n");
}
return 0;
}

Ⅳ 輸入N*N階矩陣,用函數編程計算並輸出其兩條對角線上的各元素之和(用c語言編程),,求哪位大神幫忙

#include <stdio.h>
#define M 100
int main() {
int a[M][M],i,j,msum = 0,ssum = 0;
int N;
printf("input N:");
scanf("%d",&N);
printf("請輸入%d*%d的矩陣:\n",N,N);
for(i = 0; i < N; ++i) {
for(j = 0; j < N; ++j)
{

scanf("%d",&a[i][j]);
}
}

for(i = 0; i < N; ++i) {
msum += a[i][i];
ssum += a[i][N-1- i];
}
printf("主對角線的和是:%d\n斜對角線的和是:%d\n\n",msum,ssum);
return 0;
}

Ⅵ c語言給出一個N階數字矩陣,求其邊上數字的和。

這個樣子你試一下:

#include "stdio.h"

int number[1000][1000];

int main()

{ int n;

scanf("%d",&n);

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

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

{ scanf("%d",&number[i][j]);

}

}

int sum=0;

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

{ if(n==1)

{ sum=sum+number[0][0];

}

else

{ sum=sum+number[0][k]+number[n-1][k];

}

}

for(int f=1; f<n-1; ++f)

{ sum=sum+number[f][0]+number[f][n-1];

}

printf("%d ",sum);

return 0;

}

Ⅶ 用C語言怎樣輸出一個N階蛇形矩陣

先說思想:N=4時候和N=5的時候前面4條斜線上三角是相同的!所以這個可以用遞歸做。還有個規律很重要就是當2個數的橫縱坐標和都是N+1的時候這2個值的和都是N*N+1!問題就很容易解決了!
寫個大概:
int **array=NULL;
void main()
{
int n;
scanf("%d",&n);
array=(int**)malloc(n*sizeof(int*));
for(int i=0;i<n;i++)
{
array[i]=(int *)malloc(n*sizeof(int));
}
Testingval(n,n);
for(int i=0;i<n;i++)
{for(int j=0;j<n;j++)
printf("%d ",array[i][j]);
printf("\n");
}
free(array);
}
int Testingval(int a,int n)//a 表示n行中的第幾個。。n表示是N界矩陣!
{ if(n==1)
{ array[0][0]==1;
array[n][n]=n*n;
}
else
Testingval(a-1,n);
if(a%2)
{ int i=1;
array[0][a-1]=array[0][a-2]+1;
array[n-1][n-a]=n*n+1-array[0][a];
while(i<a)
{

array[i][a-1-i]=array[i-1][a-i+1]+1;
array[n-1-i][n-a+i]=n*n+1-array[i][a-i];
i++;
}
}
else
{int i=1;
array[a-1][0]=array[a-2][0]+1;
array[n-a][n-1]=n*n+1-array[a-1][0];
while(i<a)
{

array[a-1-i][i]=array[a-i+1][i-1]+1;
array[n-a+i][n-1-i]=n*n+1-array[a-1-i][i];
i++;
}
}

}
}

Ⅷ 如何用c語言中的函數遞歸調用演算法實現n階矩陣的n次冪的求解

/*用c語言中的函數遞歸調用演算法實現n階矩陣的n次冪*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
//創建矩陣,矩陣用一維數組存儲
double *matCreate(unsigned int m, unsigned int n)
{
double *p = (double *)malloc(sizeof(double) * m * n);
if (p == NULL) printf("創建矩陣失敗!\n");
return p;
}
//輸入矩陣元素
void matInput(double *a, unsigned int m, unsigned int n)
{
for (int i = 0; i < m; ++i)
{
for (int j = 0; j < n; ++j)
{
scanf("%f ", &a[i * n + j]);
}
}
return;
}
//隨機產生矩陣元素,均勻分布於[from to]
void matInitRand(double *a, unsigned int m, unsigned int n, double from, double to)
{
if (a == NULL || m <= 0 || n <= 0) return;
double x;
srand(time(NULL));
for (int i = 0; i < m; ++i)
{
for (int j = 0; j < n; ++j)
{
x = (1.0 * rand() / RAND_MAX) * (to - from) + from;
a[i * n + j] = x;
}
}
return;
}
//轉置
void matTranspose(double *a, double *b, unsigned int m, unsigned int n)
{
for (int i = 0; i < m; ++i)
{
for (int j = 0; j < n; ++j)
{
b[j*n +i]=a[i * n + j] ;
}
}
}
//輸出矩陣
void matPrint(double *a, unsigned int m, unsigned int n)
{
for (int i = 0; i < m; ++i)
{
for (int j = 0; j < n; ++j)
{
printf("%8.4f ", a[i * n + j]);
}
putchar('\n');
}
return;
}
//矩陣乘法c=a*b
void matMul(double *a, double *b, double *c, unsigned int m, unsigned int n, unsigned int k)
{
if (a == NULL || b == NULL || c == NULL || m <= 0 || n <= 0 || k <= 0) return;
double x = 0.0f;
for (int i = 0; i < m; ++i)
{
for (int u = 0; u < k; ++u)
{
x = 0.0f;
for (int j = 0; j < n; ++j)
{
x += a[i * n + j] * b[j * k + u];
}
c[i * k + u] = x;
}
}
return;
}
//b=a^n, a:m*m階矩陣
void matFac(double *a, double *b, unsigned int n, unsigned int m)
{
double *c = (double *)malloc(sizeof(double) * m * m); //保存臨時結果
if (n > 1)
{
matFac(a, c, n - 1, m);
matMul(a, c, b, m, m, m);
}
else
memcpy(b, a, sizeof(double)*m * m);
// printf("%d:\n",n);
// matPrint(b, m,m);
free(c); //回收內存
return ;
}
#define M 3
#define N 4
#define K N
int main(int argc, char const *argv[])
{
double *A, *B, *B1,*BT, *C;
A = matCreate(M, N);
B = matCreate(N, K);
B1 = matCreate(N, K);
BT = matCreate(K,N);
C = matCreate(M, K);
if (!A || !B || !B1 || !BT || !C) return -1;
matInitRand(A, M, N, 0.0f, 1.0f);
printf("A=\n");
matPrint(A, M, N);
matInitRand(B, N, K, 0.0f, 1.0f);
printf("B=\n");
matPrint(B, N, K);

matTranspose(B,BT,N,K);
printf("B'=\n");
matPrint(BT, K,N);
matMul(A, B, C, M, N, K);
printf("C=A*B\n");
matPrint(C, M, N);
matFac(B, B1, 4, N);
printf("B^4\n");
matPrint(B1, N, K);
return 0;
}

Ⅸ C++中,怎麼輸出一個n階矩陣呢

C++中,輸出一個n階矩陣步驟如下:

1、首先,定義8個整型變數,實現n階矩陣的計算。

Ⅹ 關於C語言的問題:如何用C語言實現n階行列式和矩陣的值

輸入:
3---7
2---1
得出錯誤結果。
而當輸入n=3
0---1---3
3---0---2
5---2---0
時也會得出錯誤結果。
錯誤的原因有2:
1
是數據類型不對,匿名的程序是設定輸入都是整數int,顯然按照行列式的定義結果肯定是整數,但是他程序中使用了整型數的除法,結果是取整整數,雖然他使用了類型強制轉換,但結果顯然不同,有誤差而且有時候這個誤差很大形成錯誤。
2
是演算法有點問題。小可很欣賞匿名的演算法思路,簡潔明快。不過有相當缺陷,這在程序中註明。
下面的程序是在匿名的程序思路上改寫的。考慮到數據類型和精確度問題,程序中行列式數據使用double型。由於tc和win-tc是16位編輯器,對float型和double型數據支持不好,所以程序是在32位編輯器Dev-c++下調試並通過的。
本題的一個完整的c程序如下,程序在Dev-c++下都調試通過,結果正確。
/*
用C語言解決:求任意階(n階)矩陣的行列式值
*/
#include
<stdio.h>
#include
<math.h>
void
getarray(int
n);
void
showarray(int
n);
double
getresult(int
n);
double
array[10][10];/*設矩陣不超過10階,可更改*/
int
main()
{
int
n;
double
result;
printf("\nPlease
input
the
Array
size
n:");
scanf("%d",&n);
getarray(n);
showarray(n);
result=getresult(n);
printf("\nResult=%f\n",result);
system("pause");
return
0;
}
void
getarray(int
n)
{
int
row,col;
for(row=0;row<n;row++)
{
printf("\nPlease
input
line
%d:",row+1);
for(col=0;col<n;col++)
scanf("%lf",&array[row][col]);
}
}
void
showarray(int
n)
{
int
row,col;
printf("\nA=");
for(row=0;row<n;row++)
{
for(col=0;col<n;col++)
printf("\t%f",array[row][col]);
printf("\n");
}
}
double
getresult(int
n)
{
double
temp,result=1.0;
int
switchtime=0,flag=0;
int
row,nextrow,col,stemp;
for(row=0;row<n-1;row++)
{
nextrow=row+1;
if(array[row][row]==0)/*
開始處理第一列,如果行列式第一行第一個數為零,要交換行
*/
{
while(array[nextrow][row]==0)
{
nextrow++;
/*
如果行列式第二行第一個數為零,行增加繼續尋找非零數值的行
*/
if(nextrow==n)/*
如果遍歷完行列式行列式第一列元素都為零,退出while循環
*/
{
flag=1;
break;
}
}
if(flag==1)
/*
退出while循環後回到for(row=0;row<n-1;row++)行加1?*/
continue;
/*
從array[row][row]==0知列也相應加1,開始處理第二列
*/
switchtime++;
/*
每交換一次行,行列式符號變化1次,統計變化次數
*/
for(col=0;col<n;col++)
/*
交換非零行到行列式頂部
*/
{
stemp=array[row][col];
array[row][col]=array[nextrow][col];
array[nextrow][col]=stemp;
}
}
for(nextrow=row+1;nextrow<n;nextrow++)
{
/*
類似高斯消去法,消第一行下各行第一列數值到零*/
temp=array[nextrow][row]/array[row][row];
for(col=0;col<n;col++)
array[nextrow][col]+=-temp*array[row][col];/*
化行列式為上三角行列式形式
*/
}
}
showarray(n);
for(row=0;row<n;row++)
result*=array[row][row];
if(switchtime%2)
return
-result;
else
return
result;
}