lab 6 anlayamadık

4 views
Skip to first unread message

Chaka

unread,
Mar 20, 2008, 5:33:32 PM3/20/08
to Çankaya Bilgisayar Mühendisliği
Write a function to determine whether a particular square two-
dimensional array is a magic square (all rows, columns, and diagonals
add to the same value). Use a constant to define the size of the
square, and make the function work no matter what the constant is set
to. For example, if the size is 3, the following is a magic square. 4
9 2
3 5 7
8 1 6

ya arkadaslar ben anlamadım bısey buldumda cok hantal bı yontem
anlayanlar bulanlar hbr verırse sevınırım.... ılgınıze tesskkurler....

çağdaş üsfekes

unread,
Mar 27, 2008, 6:14:08 PM3/27/08
to Çankaya Bilgisayar Mühendisliği
#include<stdio.h>
#define size 3
void square(int arr[size][size]);
int main()
{
int arr[size][size],i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("enter [%d][%d]. element:",i,j);
scanf("%d",&arr[i][j]);
}
}
square(arr);
return(0);
}
void square(int arr[size][size])
{
int sum1=0,sum2=0;
int sum3[3]={0},sum4[3]={0},i,j,x=0;
sum1=arr[0][0]+arr[1][1]+arr[2][2];
sum2=arr[0][2]+arr[1][1]+arr[2][0];
if(sum1==sum2)
{
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
sum3[i]=sum3[i]+arr[i][j];
}
}
for(j=0;j<size;j++)
{
for(i=0;i<size;i++)
{
sum4[i]=sum4[i]+arr[j][i];
}
}
for(i=0;i<3;i++)
{
if(sum3[i]==sum4[i])
{
x++;
}
}
if(x==3)
{
printf("it is a magic square\n");
}
else
{
printf("it is not a magic square\n");
}
}
}

--------------------------------------------------------
3x3 boyutlu için
Reply all
Reply to author
Forward
0 new messages