{"id":1310,"date":"2015-04-08T00:01:47","date_gmt":"2015-04-08T07:01:47","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1310"},"modified":"2015-04-04T13:02:26","modified_gmt":"2015-04-04T20:02:26","slug":"board-of-chess-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1310","title":{"rendered":"Board of Chess &#8211; Solution"},"content":{"rendered":"<p>A chessboard is merely a two-dimensional array &mdash; a matrix &mdash; one where the rows and columns are the same size. As long as you can write code to output an 8-by-8 board, you can modify the code to reset the board&#8217;s size to any value.<br \/>\n<!--more--><br \/>\nWhen I wrote my solution, I started with code that output a standard 8-by-8 board. I realized that the board was simply the same row repeated 8 times. Here&#8217;s the basic row:<\/p>\n<pre><code> --- --- --- --- --- --- --- ---\r\n|   |   |   |   |   |   |   |   |<\/code><\/pre>\n<p>A final line is drawn under the last row, which makes the board look better for my chosen approach.<\/p>\n<p>Then I recognized that each cell in the row is basically the same thing over and over:<\/p>\n<pre><code> ---\r\n|   <\/code><\/pre>\n<p>The last cell in the row has a right side to it. So the code could just repeat the above cell chunk 8 times, cap the row with another <code>|<\/code> character. Then repeat that row 8 times, followed by output of the final line:<\/p>\n<pre><code> --- --- --- --- --- --- --- ---\r\n|   |   |   |   |   |   |   |   |\r\n --- --- --- --- --- --- --- ---\r\n|   |   |   |   |   |   |   |   |\r\n --- --- --- --- --- --- --- ---\r\n|   |   |   |   |   |   |   |   |\r\n --- --- --- --- --- --- --- ---\r\n|   |   |   |   |   |   |   |   |\r\n --- --- --- --- --- --- --- ---\r\n|   |   |   |   |   |   |   |   |\r\n --- --- --- --- --- --- --- ---\r\n|   |   |   |   |   |   |   |   |\r\n --- --- --- --- --- --- --- ---\r\n|   |   |   |   |   |   |   |   |\r\n --- --- --- --- --- --- --- ---\r\n|   |   |   |   |   |   |   |   |\r\n --- --- --- --- --- --- --- ---<\/code><\/pre>\n<p>To make the size adjustable, I replaced the value 8 with the variable <code>size<\/code>. Then I added a simple check to determine whether a command line argument was present:<\/p>\n<pre><code>    if(argc &lt;= 1)<\/code><\/pre>\n<p>When no argument is present, <code>argc<\/code> equals one, so the value of variable <code>size<\/code> is set to 8. Otherwise, <code>size<\/code> is assigned the value of <code>argv[1]<\/code>, the first argument. The <em>atoi()<\/em> function handles the conversion.<\/p>\n<p>The rest of the code consists of the loops required to output the board. Oh! And I added initial output that announces the board&#8217;s dimensions.<\/p>\n<p>Here is my solution:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nint main(int argc, char *argv[])\r\n{\r\n    int size,row,column;\r\n\r\n    if(argc &lt;= 1)\r\n        size = 8;\r\n    else\r\n        size = atoi(argv[1]);\r\n\r\n    printf(\"%d x %d Chessboard:\\n\",size,size);\r\n    for(row=0;row&lt;size;row++)\r\n    {\r\n        <span class=\"comments\">\/* Draw the top line *\/<\/span>\r\n        for(column=0;column&lt;size;column++)\r\n            printf(\" ---\");\r\n        putchar('\\n');\r\n        <span class=\"comments\">\/* Draw the squares *\/<\/span>\r\n        for(column=0;column&lt;size;column++)\r\n            printf(\"|   \");\r\n        printf(\"|\\n\");\r\n    }\r\n    <span class=\"comments\">\/* Draw the bottom line *\/<\/span>\r\n    for(column=0;column&lt;size;column++)\r\n        printf(\" ---\");\r\n    putchar('\\n');\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Your solution doesn&#8217;t need to look exactly like this. The output must resemble a chess board in some way, and the code must react properly to the command line option. I tested my code for a 1 square chess board as well as a 40 square board and various values in between. It worked! If your code works as well, then you&#8217;re good.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A chessboard is merely a two-dimensional array &mdash; a matrix &mdash; one where the rows and columns are the same size. As long as you can write code to output an 8-by-8 board, you can modify the code to reset &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1310\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1310","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1310","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1310"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1310\/revisions"}],"predecessor-version":[{"id":1320,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1310\/revisions\/1320"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}