我試圖創建一個二維數組3x3的方形。在二維數組中找到重複的值3x3魔方
計算和檢查完成。 現在,我試圖防止在二維數組中發生重複的數字。
我試圖用一個for循環蠻力而是cygwin的編譯給了我錯誤
下面是我的代碼。 我該如何去檢查2D數組?
#include <stdio.h> /* printf */
/*Preprocessor*/
/*set N constant 3*/
#define N 3
typedef enum {false,true}
bool;
int main()
{
/*initialize size to 3*/
int size =3;
int sum, sum1, sum2,array[N][N];
int row, col = 0;
int i, j, duplicate =0;
/* variable to
indicate process reached*/
int checkf =0;
/*input into the array of an array*/
for(row =0; row <size; row++)
{
for(col =0; col < size; col++)
{
scanf("%d", &array[row][col]);
}
}
**/*check for repeated values*/
for(row =0; row<9; row++)
{
for(col=0; col <9; col++)
{
if(array==array[row][col])
}
}**
/*Diagonal*/
/*sum of diagonal from the left
equals to sum of diagonal
from the right */
/*initialize sum2 to 0*/
sum2 =0;
/*check if row less than 3*/
for(row=0; row< size; row++)
{
/*check if col less than 3*/
for(col=0;col<size; col++)
{
/*number of row
equals number of col*/
if(row == col)
{
/*addition*/
sum2 = sum2 + array[row][col];
}
}
}
/*row*/
/*check if row less than 3*/
for(row=0; row < size; row++)
{
/*initialize sum */
sum = 0;
/*check if col less than 3*/
for(col=0; col <size; col++)
{
/*addition of numbers*/
sum = sum + array[row][col];
}
/*check if all additions
adds up to same sum*/
if(sum2 == sum)
{
/*a flag or check to print*/
checkf =1;
}
else
{
/*a flag or check to print*/
checkf =0;
break;
}
}
/*Columns*/
/*check if row less than 3*/
for(row = 0; row < size; row++)
{
/*initialize sum */
sum1 =0;
/*check if col less than 3*/
for(col = 0; col < size; col++)
{
/*addition*/
sum1 = sum1 + array[col][row];
}
/*sum of diagonal equals
sum of columns*/
if(sum == sum1)
{
/*a flag or check to print*/
checkf =1;
}
else
{
/*a flag or check to print*/
checkf =0;
break;
}
}
/*if statement is true
prints and display*/
if(checkf ==1)
{
printf("This is a magic square.\n");
}
else
{
/*print and display*/
printf("This is not a magic square.\n");
}
return 0;
}
「_but cygwin compiles give me error_」 - 什麼是錯誤?該錯誤消息對我們非常有用。 –
'if(array == array [row] [col])'看起來不正確。你需要兩個更多的循環來檢查。看到[這個答案](http://stackoverflow.com/a/14879569/3049655) –
嗨酷傢伙!我嘗試過應用之前的邏輯。即時通訊困惑我如何工作。 –