|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目:将一个5×5的矩阵中最大的元素放在中心,4个角分别放4个最小的元素(顺序为从左到右,从上到下依次从小到大存放),写一函数实现之。用main函数调用。
程序:
#include<stdio.h>
int main()
{
void operation(float p[5][5]);
float a[5][5];
float copy[5][5];
int i, j;
for(i = 0; i <= 4; i++)
{
for(j = 0; j <= 4; j++)
{
scanf("%f", &a[i][j]);
}
}
for(i = 0; i <= 4; i++)
{
for(j = 0; j <= 4; j++)
{
copy[i][j] = a[i][j];
}
}
operation(a);
printf("原矩阵:\n");
for(i = 0; i <= 4; i++)
{
for(j = 0; j <= 4; j++)
{
printf("%8.2f", copy[i][j]);
}
printf("\n");
}
printf("操作后矩阵:\n");
for(i = 0; i <= 4; i++)
{
for(j = 0; j <= 4; j++)
{
printf("%8.2f", a[i][j]);
}
printf("\n");
}
return 0;
}
void operation(float p[5][5])
{
int i, j;
float temp;
float *min_1 = &p[0][0], *min_2 = &p[0][1], *min_3 = &p[0][2], *min_4 = &p[0][3], *max = &p[0][0];
printf("刚定义完时:\n");
printf("max:%f\n", *max);
printf("min_1:%f\n", *min_1);
printf("min_2:%f\n", *min_2);
printf("min_3:%f\n", *min_3);
printf("min_4:%f\n", *min_4);
for(i = 0; i <= 4; i++)
{
for(j = 0; j <= 4; j++)
{
if(p[i][j] < *min_1)
{
min_4 = min_3;
min_3 = min_2;
min_2 = min_1;
min_1 = &p[i][j];
}
else if(p[i][j] < *min_2)
{
min_4 = min_3;
min_3 = min_2;
min_2 = &p[i][j];
}
else if(p[i][j] < *min_3)
{
min_4 = min_3;
min_3 = &p[i][j];
}
else if(p[i][j] < *min_4)
{
min_4 = &p[i][j];
}
}
}
for(i = 0; i <= 4; i++)
{
for(j = 0; j <= 4; j++)
{
if(p[i][j] > *max)
{
max = &p[i][j];
}
}
}
printf("操作过后:\n");//这里就出问题了,找不出来原因
printf("max:%f\n", *max);
printf("min_1:%f\n", *min_1);
printf("min_2:%f\n", *min_2);
printf("min_3:%f\n", *min_3);
printf("min_4:%f\n", *min_4);
temp = p[0][0]; p[0][0] = *min_1; *min_1 = temp;
temp = p[0][4]; p[0][4] = *min_2; *min_2 = temp;
temp = p[4][0]; p[4][0] = *min_3; *min_3 = temp;
temp = p[4][4]; p[4][4] = *min_4; *min_4 = temp;
temp = p[2][2]; p[2][2] = *max; *max = temp;
} |
|