matrix problem in c++


hello everyone, firstly happy holidays

now to the problem:

I'm making a program in c++, to print a chessboard. The program was based on asking for an x number of columns and a y rows, with that the program prints a matrix with zeros representing black colors and 1 for whites, it works when I put lines different from columns, but when I put lines = columns it prints wrong.

If possible, could someone help me solve this?

by the way this is the code
obs (apologies for grammatical errors)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  #include <iostream>
#include <math.h>
#include <locale.h>
#set size 4
using namespace std;

int main()
{

    int row = 0;
    int column = 0;
    int aux = 1;

    cout << "\nEnter column \n"<< endl;
 cin >>column;
 cout << "Enter the number of lines on the board:\n";
   cin >>line;



  while( line <= 0 || line >= 1000 )
 {
     cout << "\nVish will give an error, because the line must be greater than 0 and less than 1000 :)\n"<< endl;


 cout << "\nenter the row number of the second array: ";
 cin >> line;

 }

  while( column <= 0 || column >= 1000 )
 {
     cout << "\nVish will give an error, because the column must be greater than 0 and less than 1000 :)\n"<< endl;


 cout << "\nenter the column number of the second matrix: ";
 cin >> column;

 }




    int board[row][column];

    cout << "NOTE: 1 is for WHITE colors and 0 is for Black"<< endl;

    for (int i = 0; i < line; i++) {
        for (int j = 0; j < column; j++) {
            board[i][j] = aux;
            aux = 1 - aux;
        }
    }

    for (int i = 0; i < line; i++) {
        for (int j = 0; j < column; j++) {
             cout << board[i][j];
        }
        cout <<"\n";
    }
    if(board[row-1][column-1] == 0){
    cout <<"\nBottom right box of color is = black ";
}


else if(board[row-1][column-1] == 1){

        cout <<"\nBottom right house of color is = white ";
}
}
Last edited on
L44 isn't standard C++. You can't define the array size at run-time - although some compilers allow an exception for this.

L17 - line is used but not defined.

A chessboard is square - so the number of rows equal number of cols so you only need to ask for one value instead of two.

Slightly reformatted for readability:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <math.h>
#include <locale.h>
#set size 4
using namespace std;

int main() {
	int row = 0;
	int column = 0;
	int aux = 1;

	cout << "\nEnter column \n" << endl;
	cin >> column;
	cout << "Enter the number of lines on the board:\n";
	cin >> line;

	while (line <= 0 || line >= 1000) {
		cout << "\nVish will give an error, because the line must be greater than 0 and less than 1000 :)\n" << endl;
		cout << "\nenter the row number of the second array: ";
		cin >> line;
	}

	while (column <= 0 || column >= 1000) {
		cout << "\nVish will give an error, because the column must be greater than 0 and less than 1000 :)\n" << endl;
		cout << "\nenter the column number of the second matrix: ";
		cin >> column;
	}

	int board[row][column];

	cout << "NOTE: 1 is for WHITE colors and 0 is for Black" << endl;

	for (int i = 0; i < line; i++) {
		for (int j = 0; j < column; j++) {
			board[i][j] = aux;
			aux = 1 - aux;
		}
	}

	for (int i = 0; i < line; i++) {
		for (int j = 0; j < column; j++) {
			cout << board[i][j];
		}
		cout << "\n";
	}

	if (board[row - 1][column - 1] == 0) {
		cout << "\nBottom right box of color is = black ";
	} else if (board[row - 1][column - 1] == 1) {
		cout << "\nBottom right house of color is = white ";
	}
}


Note that the if on L66 original is not needed as if the condition fails on L61 then the colour must be white.
140 / 5,000

Sorry, but could you explain better?
what do you mean line 17, is not defined?
and line 44, I didn't quite understand what you meant?
where is the variable 'line' created?

line 44:
array index must be a constant:
int x = 10;
double foo[x]; //error: x is not a compile time constant.
some compilers support the above. That is an extension to the c++ language, though.

I think I still don't understand what you're talking about, as I'm a beginner in c++, but I think I found a way to solve this, I was watching here and there is a constant when column = row it will always be white. I'm thinking about putting one
if (row=column)cout << "is white"...,
but how can i do to print the array?
L17,21: You do a cin to line, but where is line defined? Did you mean row?

L44: In standard C++, row and column must be constants.


finally got it. l17 was supposed to be a line
thank you
Topic archived. No new replies allowed.