debugging - Pattern Printing-Where am I going wrong in this C++ code? -
i wrote program print n x n square pattern alternate 0's , 1's. eg. 5 x 5 square looks this:
i used following code-
#include<iostream.h> int main() { int i, n; cin >> n; //number of rows (and columns) in n x n matrix for(i = 1; <= n*n; i++) { cout << " " << i%2; if(i%n == 0) cout << "\n"; } fflush(stdin); getchar(); return 0; } this code works fine odd numbers numbers prints same thing in each new line , not alternate pattern.for 4 prints this-
where going wrong?
in opinion best way iterate on matrix using loop in loop.
i think code helpful you:
for(i = 0; < n; i++) { (j = 1; j <= n; j++) { cout<<" "<< (j + i) % 2; } cout<<"\n"; } where n number of rows, i , j ints.
try understand why , how works.


Comments
Post a Comment