能帮忙看看这程序哪里错了吗?运行结果不对,语言是C
题目:将一个5×5的矩阵中最大的元素放在中心,4个角分别放4个最小的元素(顺序为从左到右,从上到下依次从小到大存放),写一函数实现之。用main函数调用。程序:
#include<stdio.h>
int main()
{
void operation(float p);
float a;
float copy;
int i, j;
for(i = 0; i <= 4; i++)
{
for(j = 0; j <= 4; j++)
{
scanf("%f", &a);
}
}
for(i = 0; i <= 4; i++)
{
for(j = 0; j <= 4; j++)
{
copy = a;
}
}
operation(a);
printf("原矩阵:\n");
for(i = 0; i <= 4; i++)
{
for(j = 0; j <= 4; j++)
{
printf("%8.2f", copy);
}
printf("\n");
}
printf("操作后矩阵:\n");
for(i = 0; i <= 4; i++)
{
for(j = 0; j <= 4; j++)
{
printf("%8.2f", a);
}
printf("\n");
}
return 0;
}
void operation(float p)
{
int i, j;
float temp;
float *min_1 = &p, *min_2 = &p, *min_3 = &p, *min_4 = &p, *max = &p;
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 < *min_1)
{
min_4 = min_3;
min_3 = min_2;
min_2 = min_1;
min_1 = &p;
}
else if(p < *min_2)
{
min_4 = min_3;
min_3 = min_2;
min_2 = &p;
}
else if(p < *min_3)
{
min_4 = min_3;
min_3 = &p;
}
else if(p < *min_4)
{
min_4 = &p;
}
}
}
for(i = 0; i <= 4; i++)
{
for(j = 0; j <= 4; j++)
{
if(p > *max)
{
max = &p;
}
}
}
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; p = *min_1; *min_1 = temp;
temp = p; p = *min_2; *min_2 = temp;
temp = p; p = *min_3; *min_3 = temp;
temp = p; p = *min_4; *min_4 = temp;
temp = p; p = *max; *max = temp;
} 当然不对了。你改变的都是些什么玩意!max应该等于p。min1=p,min2=p,
min3=p,min4=p.
你看看你那个函数,一进去,min1234分别为,,,.连max都为。
你没有讲明白,假设 5 x 5 矩阵的 25 个数分别是 1 - 25,那么,经过处理后,输出的矩阵应该是什么样子的呢?
页:
[1]