{"id":5764,"date":"2023-03-04T00:01:23","date_gmt":"2023-03-04T08:01:23","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5764"},"modified":"2023-03-11T08:20:15","modified_gmt":"2023-03-11T16:20:15","slug":"totaling-a-grids-rows-and-columns","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5764","title":{"rendered":"Totaling a Grid&#8217;s Rows and Columns"},"content":{"rendered":"<p>Data in a matrix is entertaining and all that, but the point is usually to manipulate that data. The most basic form of manipulation I can conjure is to tally the rows and columns. Sounds like fun, but it took me a few attempts to get the code correct.<br \/>\n<!--more--><br \/>\nI&#8217;m certain an elegant way exists to obtain the sum of rows and columns in a grid. Rather than attempt such a solution, I chose brute force. In fact, I combined the output statements for the grid with the expressions to total the rows. The columns I handle in a separate nested loop.<\/p>\n<p>To keep things from getting too obnoxious, I use two dimensional array notation as covered in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5758\">last week&#8217;s Lesson<\/a>. Nested loops help with the processing. A new <em>int<\/em> variable <code>total<\/code> is used and reused to obtain the totals.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_03_04-Lesson.c\" rel=\"noopener\" target=\"_blank\">2023_03_04-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\n#define SIZE 3\r\n\r\nint main()\r\n{\r\n    int x,y,total;\r\n    int grid[SIZE][SIZE];\r\n\r\n    <span class=\"comments\">\/* seed the randomizer *\/<\/span>\r\n    srand( (unsigned)time(NULL) );\r\n\r\n    <span class=\"comments\">\/* populate the grid *\/<\/span>\r\n    for( x=0; x&lt;SIZE; x++ )\r\n        for( y=0; y&lt;SIZE; y++ )\r\n            grid[x][y] = rand()%10 + 1;\r\n\r\n    <span class=\"comments\">\/* output the grid and row totals *\/<\/span>\r\n    for( x=0; x&lt;SIZE; x++ )\r\n    {\r\n        total = 0;\r\n        for( y=0; y&lt;SIZE; y++ )\r\n        {\r\n            total += grid[x][y];\r\n            printf(\" %2d \",grid[x][y]);\r\n        }\r\n        printf(\"%d\\n\",total);\r\n    }\r\n    <span class=\"comments\">\/* output column totals *\/<\/span>\r\n    for( x=0; x&lt;SIZE; x++ )\r\n    {\r\n        total = 0;\r\n        for( y=0; y&lt;SIZE; y++ )\r\n            total += grid[y][x];\r\n        printf(\" %2d \",total);\r\n    }\r\n    putchar('\\n');\r\n\r\n    return(0);\r\n}\r\n<\/pre>\n<p>Stolen from last week&#8217;s code, array <code>grid[][]<\/code> is initialized and populated. Starting at Line 20, the grid&#8217;s values are output and the row totals calculated.<\/p>\n<p>Variable <code>total<\/code> is initialized at Line 23. It accumulates the row cell values as the matrix is output left-to-right: <code>total += grid[x][y];<\/code> The sum is output after each row.<\/p>\n<p>Calculating column totals requires another nested loop of a similar construction, starting at Line 32 . One mistake a lot of programmers may make is to use absolute offsets. But remember that the grid&#8217;s dimensions are set with constant <code>SIZE<\/code>. If you use absolute values: <code>grid[x][0]<\/code>, <code>grid[x][1]<\/code>, <code>grid[x][2]<\/code>, the code breaks when the value of <code>SIZE<\/code> is adjusted.<\/p>\n<p>Also note in the second nested loop how the values manipulated are <code>grid[y][x]<\/code>, with variables <code>x<\/code> and <code>y<\/code> swapped. Sure it could be written <code>grid[x][y]<\/code>, but the point is that the inner loop sets the row values, not columns.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>&nbsp;&nbsp;6&nbsp;&nbsp;&nbsp;8&nbsp;&nbsp;&nbsp;6&nbsp;20<br \/>\n&nbsp;&nbsp;8&nbsp;&nbsp;&nbsp;5&nbsp;&nbsp;10&nbsp;23<br \/>\n&nbsp;&nbsp;9&nbsp;&nbsp;&nbsp;3&nbsp;&nbsp;&nbsp;2&nbsp;14<br \/>\n&nbsp;23&nbsp;&nbsp;16&nbsp;&nbsp;18<\/code><\/p>\n<p>I did attempt to create a pointer version of this code. It&#8217;s similar to what&#8217;s shown here, but it&#8217;s boring. So,I didn&#8217;t include it. One issue I did encounter (and reminded you of above) is to use the <code>SIZE<\/code> constant to set the grid offsets when tallying the rows and columns. By doing so, you can adjust the matrix size and not have the code get stupid on you.<\/p>\n<p>For example, here is the same code presented in this Lesson, but with the <code>SIZE<\/code> constant set to five:<\/p>\n<p><code>&nbsp;10&nbsp;&nbsp;&nbsp;5&nbsp;&nbsp;&nbsp;9&nbsp;&nbsp;&nbsp;6&nbsp;&nbsp;&nbsp;2&nbsp;32<br \/>\n&nbsp;&nbsp;2&nbsp;&nbsp;10&nbsp;&nbsp;&nbsp;6&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;8&nbsp;27<br \/>\n&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;7&nbsp;&nbsp;&nbsp;6&nbsp;&nbsp;&nbsp;7&nbsp;&nbsp;&nbsp;7&nbsp;29<br \/>\n&nbsp;10&nbsp;&nbsp;10&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;7&nbsp;&nbsp;10&nbsp;38<br \/>\n&nbsp;&nbsp;3&nbsp;&nbsp;&nbsp;5&nbsp;&nbsp;10&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;8&nbsp;27<br \/>\n&nbsp;27&nbsp;&nbsp;37&nbsp;&nbsp;32&nbsp;&nbsp;22&nbsp;&nbsp;35&nbsp;<\/code><\/p>\n<p><a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5788\">Next week<\/a>, I frustrate myself further by presenting code that adds two matrixes to generate a third.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tallying the rows and columns in a grid is something I&#8217;m more than happy to have a computer do for me. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5764\">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":[2],"tags":[],"class_list":["post-5764","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5764","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=5764"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5764\/revisions"}],"predecessor-version":[{"id":5803,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5764\/revisions\/5803"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}