我有一个 C 代码来生成对角单位矩阵。代码如下:
#include<stdio.h>
void main()
{
int n,m,a,b,c,d,i,j;
int ar1[100][100];
printf("Enter value of n to create a square matrix of order n (odd no.): ");
scanf("%d", &n);
printf(" matrix in diagonal direction is - \n");
for (i=0; i<n; i++) {
for (j=0; j<n; j++) {
if (ar1[j]==ar1[i]) {
ar1[j][i] = 2;
printf("%d, ", ar1[j][i]);
} else {
ar1[j][i] = -1;
printf("%d, ", ar1[j][i]);
}
}
printf("\n");
}
}
这给了我这样的结果:
2, -1, -1, -1, -1,
-1, 2, -1, -1, -1,
-1, -1, 2, -1, -1,
-1, -1, -1, 2, -1,
-1, -1, -1, -1, 2
但我想像这样以相反的方向打印对角矩阵:
-1, -1, -1, -1, 2,
-1, -1, -1, 2, -1,
-1, -1, 2, -1, -1,
-1, 2, -1, -1, -1,
2, -1, -1, -1, -1
有人可以帮助我,关于如何以相反的方向打印对角矩阵吗?我想改变实际矩阵不仅仅是输出
我已经像这样更新了我的代码(如下所示),但仍然出现错误(如下所示)
#include<stdio.h>
void main()
{
int n,m,a,b,c,d,i,j,s;
int ar1[100][100];
printf("Enter value of n to create a square matrix of order n (odd no.): ");
scanf("%d",&n);
printf("Enter m: (odd no.) ");
scanf("%d",&m );
if(n>m)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
for(a=1;a<=(m-1)/2;a++){
if(ar1[i-a]==ar1[j]){
ar1[i][j] = 2;
printf("%d, ",ar1[i][j]);
}
}
for(a=(m+1)/2;a<=(n-1);a++){
if(ar1[i-a]==ar1[j]){
ar1[i][j] = -1;
printf("%d, ",ar1[i][j]);
}
}
if(ar1[i]==ar1[j]){
ar1[i][j] = 2;
printf("%d, ",ar1[i][j]);
}
for(a=1;a<=(m-1)/2;a++){
if(ar1[i+a]==ar1[j]){
ar1[i][j] = 2;
printf("%d, ",ar1[i][j]);
}
}
for(a=(m+1)/2;a<=(n-1);a++){
if(ar1[i+a]==ar1[j]){
ar1[i][j] = -1;
printf("%d, ",ar1[i][j]);
}
}
s = flip (ar1[100][100], n);
}
printf("\n");
}
void flip(int a[100][100], int n) {
int t;
for (i=0; i<n; i++) {
for (j=0; j<n/2; j++) {
t=a[i][j];
a[i][j] = a[i][n-1-j];
a[i][n-1-j] = t;
}
}
}
}
}
并且收到这样的错误
/tmp/cc72v9hd.o: In function `main':
faltu2.c:(.text+0x629): undefined reference to `flip'
collect2: ld returned 1 exit status