我最近開始學習 C 編碼。功能是從 2D 陣列中提取最小值和最大值。我不明白為什么版本 #1 有效,但版本 #2 無效(區別是使用“else if”或“if”)
我使用的具體測驗用例是:
1 2 -3 4 5
2 3 4 5 6
4 5 6 7 8
5 4 23 1 27
1 2 3 4 5
版本 #1
#include <stdio.h>
#define SIZE 5
void findMinMax2D(int ar[SIZE][SIZE], int *min, int *max);
int main()
{
int A[5][5];
int i,j,min,max;
printf("Enter the matrix data (%dx%d): \n",SIZE,SIZE);
for (i=0;i<5;i )
for (j=0;j<5;j )
scanf("%d",&A[i][j]);
findMinMax2D(A,&min,&max);
printf("min = %d\nmax=%d",min,max);
return 0;
}
void findMinMax2D(int ar[SIZE][SIZE], int *min, int *max)
{
int row,col;
(*min)=ar[0][0];
(*max)=ar[0][0];
for (row=0;row<SIZE;row )
{
for (col=0;col<SIZE;col )
if (ar[row][col]>(*max))
{
(*max)=ar[row][col];
}
else if (ar[row][col]<(*min)) //else if works here
{
(*min)=ar[row][col];
}
}
}
這給了我 min = -3 和 max = 27
版本 #2
#include <stdio.h>
#define SIZE 5
void findMinMax2D(int ar[SIZE][SIZE], int *min, int *max);
int main()
{
int A[5][5];
int i,j,min,max;
printf("Enter the matrix data (%dx%d): \n",SIZE,SIZE);
for (i=0;i<5;i )
for (j=0;j<5;j )
scanf("%d",&A[i][j]);
findMinMax2D(A,&min,&max);
printf("min = %d\nmax=%d",min,max);
return 0;
}
void findMinMax2D(int ar[SIZE][SIZE], int *min, int *max)
{
int row,col;
(*min)=ar[0][0];
(*max)=ar[0][0];
for (row=0;row<SIZE;row )
{
for (col=0;col<SIZE;col )
if (ar[row][col]>(*max))
{
(*max)=ar[row][col];
}
if (ar[row][col]<(*min)) //if does not work here
{
(*min)=ar[row][col];
}
}
}
這給了我 min = 1 和 max = 27
我無法在版本 2 中提取 -3。據我所知,在這種情況下使用“else if”或“if”是無關緊要的。任何建議都非常感謝,謝謝!
uj5u.com熱心網友回復:
我修復了代碼中的編譯器錯誤,并用硬編碼的示例資料替換了輸入:
#include <stdio.h>
#define SIZE 5
void findMinMax2D(int ar[SIZE][SIZE], int *min, int *max);
int main()
{
int A[5][5] = {
{ 1, 2, -3, 4, 5 },
{ 2, 3, 4, 5, 6 },
{ 4, 5, 6, 7, 8 },
{ 5, 4, 23, 1, 27 },
{ 1, 2, 3, 4, 5 }
};
int min,max;
findMinMax2D(A,&min,&max);
printf("min = %d\nmax=%d",min,max);
return 0;
}
void findMinMax2D(int ar[SIZE][SIZE], int *min, int *max)
{
int row,col;
(*min)=ar[0][0];
(*max)=ar[0][0];
for (row=0;row<SIZE;row )
{
for (col=0;col<SIZE;col )
if (ar[row][col]>(*max))
{
(*max)=ar[row][col];
}
if (ar[row][col]<(*min)) //else if works here
{
(*min)=ar[row][col];
}
}
}
當我用 GCC 編譯它時,我收到以下警告:
main.c: In function ‘findMinMax2D’:
main.c:29:8: warning: this ‘for’ clause does not guard... [-Wmisleading-indentation]
29 | for (col=0;col<SIZE;col )
| ^~~
main.c:34:12: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘for’
34 | if (ar[row][col]<(*min)) //else if works here
| ^~
當您在除錯器中逐步運行代碼時,您會看到第二個代碼if沒有在回圈內執行。
問題是由于缺少大括號引起的for。
if...else是不需要大括號的單個陳述句。else(分支包含另一個if陳述句是無關緊要的。)
沒有else你有兩個單獨if的陳述句,盡管有誤導性的縮進,只有第一個在for回圈體中。第二個if將在回圈結束后for執行,導致對陣列的越界訪問。
修復是
for (col=0;col<SIZE;col )
{ /* <-------------------------------- *** important ***/
if (ar[row][col]>(*max))
{
(*max)=ar[row][col];
}
if (ar[row][col]<(*min))
{
(*min)=ar[row][col];
}
} /* <-------------------------------- *** important ***/
這就是為什么安全關鍵型軟體的編碼規則經常需要使用大括號,即使是在 C 語言不需要大括號的簡單單個陳述句中也是如此。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/529463.html
上一篇:為if陳述句選擇條件
