pattern

8 by 8 board where all positions are 0, except the diagonal

#include <iostream>
using namespace std;
int main ()
{


for(int r = 0 ;r < 8;r++){
for (int j = 0;j<8;j++)
if(r <1)
cout << "1";
else
cout << "0";
cout << endl;
}
return 0;
}
closed account (o3hC5Di1)
Hi there,

If you have any questions, please state them clearly. Are you having problems with how to solve the problem, is the code not compiling, does the program crash, etc. etc.

Also, please put your code in between [code][/code]-tags, it makes it more readable.

Please clarify your question and I'm sure somebody will be happy to help you.

All the best,
NwN
Are you trying to do this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//diagonal.cpp
//print diagonal

#include <iostream>
using std::cout;
using std::endl;

int main(){

int diagonal=7;
for(int i=0;i<8;i++){
        for(int j=0;j<8;j++){
                if(j==diagonal) cout<<'1';
                else cout<<'0';
        }//end inner for
cout<<endl; //new line
diagonal--;
}//end outer for

return 0; //indicates success
}//end main 



Eyenrique-MacBook-Pro:Help Eyenrique$ ./diagonal 
00000001
00000010
00000100
00001000
00010000
00100000
01000000
10000000
@NwN is right!
You have to use tags and clarify your questions.
The typical diagonal happens when the row index equals the column index:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

int main()
{
    for (int i = 0; i < 8; ++i)
    {
        for (int j = 0; j < 8; ++j)
        {
            if (i == j)
            {
                std::cout << "1 ";
            }
            else
            {
                std::cout << "0 ";
            }
        }

        std::cout << std::endl;
    }

    return 0;
}
Thank you guys
@ahmarajeel
can you help to me?
allow private messages from account settings! pls ;D
I think you understand my question , but I am looking for both the diagonals.
not one diagonal
That's simple:

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
//diagonal.cpp
//print diagonal

#include <iostream>
using std::cout;
using std::endl;

int main(){

int diagonal=7;
for(int i=0;i<8;i++){
        for(int j=0;j<8;j++){
                if(j==diagonal) cout<<'1';
                else cout<<'0';
        }//end inner for
cout<<endl; //new line
diagonal--;
}//end outer for

cout<<'\n'<<endl;

for(int i=0;i<8;i++){
        for(int j=0;j<8;j++){
                if(i==j) cout<<'1';
                else cout<<'0';
        }//end inner for
cout<<endl; //new line
}//end outer for

return 0; //indicates success
}//end main 



Eyenrique-MacBook-Pro:Help Eyenrique$ ./diagonal 
00000001
00000010
00000100
00001000
00010000
00100000
01000000
10000000


10000000
01000000
00100000
00010000
00001000
00000100
00000010
00000001
I am sorry I should have mentioned that both diagonal at a time
small version is
1001
0110
0110
1001
I don't think you're making an effort to understand our contributions.

http://ideone.com/FU9ffO
Help to me with something,
allow private messages from
your account settings.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//diagonal.cpp
//print diagonal

#include <iostream>
using std::cout;
using std::endl;

int main(){

int diagonal=7;
for(int i=0;i<8;i++){
        for(int j=0;j<8;j++){
                if(j==diagonal||i==j) cout<<'1';
                else cout<<'0';
        }//end inner for
cout<<endl; //new line
diagonal--;
}//end outer for


return 0; //indicates success
}//end main 



Eyenrique-MacBook-Pro:Help Eyenrique$ ./diagonal 
10000001
01000010
00100100
00011000
00011000
00100100
01000010
10000001
thank you very much
Topic archived. No new replies allowed.