Sunday 15 January 2012

loops - How can I make a checkerboard with variable size out of text in C++? -



loops - How can I make a checkerboard with variable size out of text in C++? -

how can create checkerboard variable size out of text? can create checkerboard consisting of single characters using code:

#include <iostream> using namespace std; int main() { char c; int length; int width; int count = 0; int n; int row = 0; int col = 0; cout << "input c" << endl; cin >> c; cout << "input length" << endl; cin >> length; cout << "input width" << endl; cin >> width; cout << "input n" << endl; cin >> n; (row = 0; row < n; ++row) { if (row % 2 == 1) { (count = 0; count < length; count++) { { cout << "-"; } cout << ""; } } (col = 0; col < n; ++col) { (count = 0; count < length; count++) { { cout << c; } cout << ""; } if (col != n) { (count = 0; count < length; count++) { { cout << "-"; } cout << ""; } } } cout << endl; } printf("\n"); }

but gives me output of single characters, how added varying "block" sizes equation?

for examaple, given character (c), length (l), width (w), , size (n), write c++ programme draw board of size (2n x 2n) consisting of cells filled character c, length of l, , width of w. board cells have painted chess board

assume c = ‘&’, l = 4, w = 6, , n = 3

the output (without border lines)

honestly, tried through code , find mistakes, ended rewriting problem scratch. it's nested loops, , need quite of them fine-grained control.

the code did trick me (i hope comments helpful):

#include <iostream> int main(){ // our variables char c; int l, w, n; // take parameters standard input std::cin >> c >> l >> w >> n; // loop on coarse rows for(int row = 0; row < 2*n; row++){ // loop on fine rows for(int smallrow = 0; smallrow < l; smallrow++){ // loop on coarse columns for(int col = 0; col < 2*n; col++){ // loop on fine columns for(int smallcol = 0; smallcol < w; smallcol++){ // if col , row both or uneven, color if(row % 2 == col % 2){ std::cout << c; }else{ std::cout << " "; } } } // end of little row, need new line std::cout << std::endl; } } homecoming 0; }

compiling g++ -o main main.cpp , executing parameters, illustration echo "x 3 5 2" | ./main gives me nice result:

xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx

c++ loops text

No comments:

Post a Comment