|
20鱼币
//程序功能是将输入的5阶行列式中最大的数字放在中间,按照从上到下,从左到右的顺序,四个角分别放最小,第二小,第三小,第四小的值;
#include <stdio.h>
void main()
{
void sort(int (*p)[5]);
int a[5][5], i, j;
printf("please input 5*5 array: \n");
for(i = 0; i < 5; i++) //一个一个输入行列式的每一个值;
{
for(j = 0; j < 5; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("\nThe 5*5 array is: \n"); //将行列式打印出来;
for(i = 0; i < 5; i++)
{
for(j = 0; j < 5; j++)
{
printf("%4d", a[i][j]);
}
printf("\n");
}
printf("\nNow they are: \n");
sort(a); //调用sort函数
printf("\n");
}
void sort(int (*p)[5])
{
int i, j, temp;
int *Max, *Min;
Max = p; //将行列式第一个地址赋给Max,赋给Min;
Min = p;
for(i = 0; i < 5; i++) //找出最大值地址;
{
for(j = 0; j < 5; j++)
{
if(*Max < *(*(p + i) + j))
{
Max = *(p + i) + j;
}
if(*Min > *(*(p + i) + j)) //找出最小值地址;
{
Min = *(p + i) + j;
}
}
}
temp = *(*(p + 2) + 2); //最大值与中间值交换;
*(*(p + 2) + 2) = *Max;
*Max = temp;
temp = *Min; //最小值与第一行第一列的值交换;
*Min = *p;
*p = temp;
Min = *p + 1;
for(i = 0; i < 5; i++)
{
for(j = 0; j < 5; j++)
{
if(*(*(p + i) + j) != *p) && (*Min > *(*(p + i) + j))) //判断条件为循环值不等于最小值,且大于*Min;
{
Min = *(p + i) + j; //找出第二小值地址;
}
}
}
temp = *Min; //将第二小值与第一行第五列值交换;
*Min = *(*p + 4);
*(*p + 4) = temp;
Min = *p + 1;
for(i = 0; i < 5; i++) //找出第三小值;
{
for(j = 0; j < 5; j++)
{
if(*(*(p + i) + j) != *p) && (*(*(p + i) + j) != *(*p + 4)) && (*Min > *(*(p + i) + j)))
{
Min = *(p + i) + j;
}
}
}
temp = *Min; //第三小值与第五行第一列值交换;
*Min = *(*(p + 4));
*(*(p + 4)) = temp;
Min = *p + 1; //找出第四小值地址;
for(i = 0; i < 5; i++)
{
for(j = 0; j < 5; j++)
{
if((*(*(p + i) + j) != *p) && (*(*(p + i) + j) != *(*p + 4)) && (*(*(p + i) + j) != *(*(p + 4))) && (*Min > *(*(p + i) + j)))
{
Min = *(p + i) + j;
}
}
}
temp = *Min; //第四小值与第五行第五列值交换;
*Min = *(*(p + 4) + 4);
*(*(p + 4) + 4) = temp;
for(i = 0; i < 5 ; i++) //打印交换过后的行列式;
{
for(j = 0; j < 5; j++)
{
printf("%4d", *(*(p + i) + j));
}
printf("\n");
}
}
编译过后出现了好多问题,检查了很久,可能是指针问题,求大神看看。
0.c
C:\Documents and Settings\桌面\10\10.c(46) : warning C4047: '=' : 'int *' differs in levels of indirection from 'int (*)[5]'
C:\Documents and Settings\\桌面\10\10.c(48) : warning C4047: '=' : 'int *' differs in levels of indirection from 'int (*)[5]'
C:\Documents and Settings\\桌面\10\10.c(71) : warning C4047: '=' : 'int ' differs in levels of indirection from 'int *'
C:\Documents and Settings\桌面\10\10.c(72) : warning C4047: '=' : 'int [5]' differs in levels of indirection from 'int '
C:\Documents and Settings\chgtx\桌面\10\10.c(72) : error C2106: '=' : left operand must be l-value
C:\Documents and Settings\chgtx\桌面\10\10.c(79) : warning C4047: '!=' : 'int ' differs in levels of indirection from 'int [5]'
C:\Documents and Settings\chgtx\桌面\10\10.c(79) : error C2143: syntax error : missing ';' before '&&'
C:\Documents and Settings\chgtx\桌面\10\10.c(86) : error C2065: 'Min' : undeclared identifier
C:\Documents and Settings\chgtx\桌面\10\10.c(86) : error C2100: illegal indirection
C:\Documents and Settings\chgtx\桌面\10\10.c(86) : error C2099: initializer is not a constant
C:\Documents and Settings\chgtx\桌面\10\10.c(87) : error C2040: 'Min' : 'int *' differs in levels of indirection from 'int '
C:\Documents and Settings\chgtx\桌面\10\10.c(87) : error C2065: 'p' : undeclared identifier
C:\Documents and Settings\chgtx\桌面\10\10.c(87) : error C2100: illegal indirection
C:\Documents and Settings\chgtx\桌面\10\10.c(87) : error C2100: illegal indirection
C:\Documents and Settings\chgtx\桌面\10\10.c(87) : error C2099: initializer is not a constant
C:\Documents and Settings\chgtx\桌面\10\10.c(88) : error C2143: syntax error : missing ')' before '+'
C:\Documents and Settings\chgtx\桌面\10\10.c(88) : error C2143: syntax error : missing '{' before '+'
C:\Documents and Settings\chgtx\桌面\10\10.c(88) : error C2059: syntax error : '+'
C:\Documents and Settings\chgtx\桌面\10\10.c(88) : error C2059: syntax error : ')'
C:\Documents and Settings\chgtx\桌面\10\10.c(90) : error C2100: illegal indirection
C:\Documents and Settings\chgtx\桌面\10\10.c(91) : error C2059: syntax error : 'for'
C:\Documents and Settings\chgtx\桌面\10\10.c(91) : error C2143: syntax error : missing '{' before '<'
C:\Documents and Settings\chgtx\桌面\10\10.c(91) : error C2059: syntax error : '<'
C:\Documents and Settings\chgtx\桌面\10\10.c(91) : error C2143: syntax error : missing '{' before '++'
C:\Documents and Settings\chgtx\桌面\10\10.c(91) : error C2059: syntax error : '++'
C:\Documents and Settings\chgtx\桌面\10\10.c(91) : error C2059: syntax error : ')'
C:\Documents and Settings\chgtx\桌面\10\10.c(102) : error C2374: 'temp' : redefinition; multiple initialization
C:\Documents and Settings\chgtx\桌面\10\10.c(86) : see declaration of 'temp'
C:\Documents and Settings\chgtx\桌面\10\10.c(102) : error C2100: illegal indirection
C:\Documents and Settings\chgtx\桌面\10\10.c(102) : error C2099: initializer is not a constant
C:\Documents and Settings\chgtx\桌面\10\10.c(103) : error C2040: 'Min' : 'int *' differs in levels of indirection from 'int '
C:\Documents and Settings\chgtx\桌面\10\10.c(103) : error C2100: illegal indirection
C:\Documents and Settings\chgtx\桌面\10\10.c(103) : error C2100: illegal indirection
C:\Documents and Settings\chgtx\桌面\10\10.c(103) : error C2099: initializer is not a constant
C:\Documents and Settings\chgtx\桌面\10\10.c(104) : error C2143: syntax error : missing ')' before '+'
C:\Documents and Settings\chgtx\桌面\10\10.c(106) : error C2374: 'Min' : redefinition; multiple initialization
C:\Documents and Settings\chgtx\桌面\10\10.c(106) : error C2100: illegal indirection
C:\Documents and Settings\chgtx\桌面\10\10.c(106) : error C2099: initializer is not a constant
C:\Documents and Settings\chgtx\桌面\10\10.c(107) : error C2059: syntax error : 'for'
C:\Documents and Settings\chgtx\桌面\10\10.c(107) : error C2143: syntax error : missing '{' before '<'
C:\Documents and Settings\chgtx\桌面\10\10.c(107) : error C2059: syntax error : '<'
C:\Documents and Settings\chgtx\桌面\10\10.c(107) : error C2143: syntax error : missing '{' before '++'
C:\Documents and Settings\chgtx\桌面\10\10.c(107) : error C2059: syntax error : '++'
C:\Documents and Settings\chgtx\桌面\10\10.c(107) : error C2059: syntax error : ')'
C:\Documents and Settings\chgtx\桌面\10\10.c(118) : error C2374: 'temp' : redefinition; multiple initialization
C:\Documents and Settings\chgtx\桌面\10\10.c(86) : see declaration of 'temp'
C:\Documents and Settings\chgtx\桌面\10\10.c(118) : error C2100: illegal indirection
C:\Documents and Settings\chgtx\桌面\10\10.c(118) : error C2099: initializer is not a constant
C:\Documents and Settings\chgtx\桌面\10\10.c(119) : error C2040: 'Min' : 'int *' differs in levels of indirection from 'int '
C:\Documents and Settings\chgtx\桌面\10\10.c(119) : error C2100: illegal indirection
C:\Documents and Settings\chgtx\桌面\10\10.c(119) : error C2100: illegal indirection
C:\Documents and Settings\chgtx\桌面\10\10.c(119) : error C2099: initializer is not a constant
C:\Documents and Settings\chgtx\桌面\10\10.c(120) : error C2143: syntax error : missing ')' before '+'
C:\Documents and Settings\chgtx\桌面\10\10.c(122) : error C2059: syntax error : 'for'
C:\Documents and Settings\chgtx\桌面\10\10.c(122) : error C2143: syntax error : missing '{' before '<'
C:\Documents and Settings\chgtx\桌面\10\10.c(122) : error C2059: syntax error : '<'
C:\Documents and Settings\chgtx\桌面\10\10.c(122) : error C2143: syntax error : missing '{' before '++'
C:\Documents and Settings\chgtx\桌面\10\10.c(122) : error C2059: syntax error : '++'
C:\Documents and Settings\chgtx\桌面\10\10.c(122) : error C2059: syntax error : ')'
C:\Documents and Settings\chgtx\桌面\10\10.c(133) : error C2059: syntax error : '}'
Error executing cl.exe. |
最佳答案
查看完整内容
int *Max;这是MAX的定义
int (*p)[5];这是p的定义
它们类型不一样。
Max = p; //将行列式第一个地址赋给Max,赋给Min;
这行是你写的,所以给警告
|