#include`<stdio.h>`
#include`<stdlib.h>`
int main()
{
int k, i, j,tot=0, htot=0, vtot=0, dtot=0, m, n;
int a[8][8] = {
{0,0,0,0,0,0,1,0},
{0,0,0,1,0,0,0,0},
{0,1,0,0,0,0,0,0},
{1,0,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0},
{0,0,0,0,0,1,0,0},
{0,0,0,0,1,0,0,0},
{0,0,0,0,0,0,0,1},
};
for(i=0;i<8;i++)
{
htot=0;
printf("\n");
for(j=0;j<8;j++)
{
htot += a[i][j];
printf("%d\t", a[i][j]);
}
tot += htot ;
}
if(tot == 8)
printf("Moving on to Vertical checking");
else
printf("Horizontal criterion not fulfilled %d ", tot);
tot=0;
for(j=0;j<8;j++)
{
vtot=0;
printf("\n");
for(i=0;i<8;i++)
{
vtot += a[i][j];
printf("%d\t", a[i][j]);
}
tot +=vtot;
}
if(tot == 8)
printf("Moving on to Diagonal checking");
else
printf("Vertical criterion not fulfilled %d ", tot);
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
if(a[i][j])
{
m=i;
n=j;
while(n!=0)// running the loop leftwards
{
m++;
n--;
dtot +=a[m][n];
}
printf("diagonal left total = %d", dtot);
if(dtot == 1)
{
m=i;
n=j;
while(n!=0)// running the loop rightwards
{
m++;
n++;
dtot +=a[m][n];
}
printf("diagonal right total = %d", dtot);
}
}
}
}
return 0;
}
答案 0 :(得分:1)
对角线跑道没有正确限制。你只是检查n索引,并且你在向右跨度上检查错误的方向。在某些时候,n超出范围并进行非法数组访问。
答案 1 :(得分:1)
while(n!=0)// running the loop rightwards
{
m++;
n++;
dtot +=a[m][n];
}
这对我来说似乎是一个非常明显的崩溃。增加n
并检查n!=0
。
此前的循环可能也会崩溃,因为m
用完了数组索引。
在这些循环中为printf
和m
添加一些n
个。更好的是:在编写代码之前考虑数组索引范围。