Viphone Straik
Viphone Straik

Reputation: 1

c++ program doesn't print and breaks down

my program doesn't print anything . What is wrong ? It's breaking down after compiling I tried without void functions and it was Ok but i have to use a function.

    #define N 8
#include <iostream>
#include <stdlib.h>
#include <cstdlib>

using namespace std;

void put(int table[N][N],int a,int b)
{
int x;
  x=a;
   for(int y=b;y<8;y++)
   {
       table[x][y]=1;
       x++;
    }
x=a;

    for(int y=b;y>=0;y--)
    {
       table[x][y]=1;
        x++;
     }
x=a;
   for(int y=b;y>=0;y--)
    {
     table[x][y]=1;
    x--;
     }

 x=a;

     for(int y=b;y<8;y++)
   {
       table[x][y]=1;
       x--;
    }

table[a][b]=2;
}

int main()
{
    int table[N][N] = { { 0, 0, 0, 0, 0, 0, 0, 0 },
                        { 0, 0, 0, 0, 0, 0, 0, 0 },
                        { 0, 0, 0, 0, 0, 0, 0, 0 },
                        { 0, 0, 0, 0, 0, 0, 0, 0 },
                        { 0, 0, 0, 0, 0, 0, 0, 0 },
                        { 0, 0, 0, 0, 0, 0, 0, 0 },
                        { 0, 0, 0, 0, 0, 0, 0, 0 },
                        { 0, 0, 0, 0, 0, 0, 0, 0 }, };

put(table,2,2);

for(int i=0;i<8;i++)
    {
        for(int j=0;j<8;j++)
        {
            cout<<table[i][j]<<" ";

        }
       cout<<endl;
      }
    return 0;
}

It has to place bishop on chess board and occupy cells. So I wrote 'put' function but something doesnt work here

Upvotes: 0

Views: 41

Answers (1)

cigien
cigien

Reputation: 60228

You have undefined behaviour in your last for loop, as x becomes -1.

You could simplify the put function considerably like this:

void put(int table[N][N],int a,int b)
{
   for(int y = 0; y < 8; ++y)
   {
       table[a][y] = 1;
       table[y][b] = 1;
   }

   table[a][b]=2;
}

Upvotes: 1

Related Questions