{"id":6553,"date":"2024-09-08T00:01:34","date_gmt":"2024-09-08T07:01:34","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6553"},"modified":"2024-10-01T10:23:06","modified_gmt":"2024-10-01T17:23:06","slug":"describing-complex-data-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6553","title":{"rendered":"Describing Complex Data &#8211; Solution"},"content":{"rendered":"<p>In the C language, a structure is used to express complex data types. The structure contains members that describe different parts of this complex data, such as a matrix required in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6542\">this month&#8217;s Exercise<\/a>.<br \/>\n<!--more--><br \/>\nFor my solution, I created the following structure.<\/p>\n<p><code>struct matrix {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;int *grid;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;int columns;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;int rows;<br \/>\n};<\/code><\/p>\n<p>The structure contains three members: <code>grid<\/code> is a pointer, the base of an array that contains the matrix&#8217;s data (integers); <code>columns<\/code>, the number of columns; and <code>rows<\/code>, the number of rows. Together, <code>rows<\/code> &times; <code>columns<\/code> defines the size of the matrix.<\/p>\n<p>It&#8217;s possible to assign a single-dimension array to a pointer, which makes it easy to create this structure using the two matrixes presented in the challenge post. Here is my solution, including the <em>output()<\/em> function:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_09-Exercise.c\" rel=\"noopener\" target=\"_blank\">2024_09-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nstruct matrix {\r\n    int *grid;\r\n    int columns;\r\n    int rows;\r\n};\r\n\r\nvoid output(char *title,struct matrix m)\r\n{\r\n    int x,y;\r\n\r\n    puts(title);\r\n    for( y=0; y&lt;m.rows; y++ )\r\n    {\r\n        for( x=0; x&lt;m.columns; x++ )\r\n        {\r\n            printf(\" %3d \",*(m.grid+x+y*m.columns) );\r\n        }\r\n        putchar('\\n');\r\n    }\r\n}\r\n\r\nint main()\r\n{\r\n    int matrix_a[12] = {\r\n        10, 20, 30, 40,\r\n        11, 21, 31, 41,\r\n        12, 22, 32, 42\r\n    };\r\n    int matrix_b[4] = {\r\n        1, 2,\r\n        3, 4\r\n    };\r\n    struct matrix a = { matrix_a, 4, 3 };\r\n    struct matrix b = { matrix_b, 2, 2 };\r\n\r\n    <span class=\"comments\">\/* output each matrix *\/<\/span>\r\n    output(\"Matrix A\",a);\r\n    output(\"Matrix B\",b);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Each matrix is defined as an integer array in the <em>main()<\/em> function. The next two statements assign values to the matrix structures <code>a<\/code> and <code>b<\/code>:<\/p>\n<p><code>struct matrix a = { matrix_a, 4, 3 };<br \/>\nstruct matrix b = { matrix_b, 2, 2 };<\/code><\/p>\n<p>My version of the <em>output()<\/em> function has two arguments: The first is a title string, the second the structure defining the matrix. The function outputs the title first, then uses a nested loop to output the rows and column values.<\/p>\n<p>To find offsets in the array, which is now a pointer, the following expression is used:<\/p>\n<p><code>*(m.grid+x+y*m.columns)<\/code><\/p>\n<p>The row values are held in <code>m.rows<\/code>, which defines the outer loop (variable <code>y<\/code>). Each row consists of column values stored in <code>m.columns<\/code>. The values are offset from the array&#8217;s base, <code>m.grid<\/code>. Add the row offset plus the row count times the number of columns to obtain each element in the matrix.<\/p>\n<p>Here&#8217;s the output:<\/p>\n<p><code>Matrix&nbsp;A<br \/>\n&nbsp;&nbsp;10&nbsp;&nbsp;&nbsp;20&nbsp;&nbsp;&nbsp;30&nbsp;&nbsp;&nbsp;40&nbsp;<br \/>\n&nbsp;&nbsp;11&nbsp;&nbsp;&nbsp;21&nbsp;&nbsp;&nbsp;31&nbsp;&nbsp;&nbsp;41&nbsp;<br \/>\n&nbsp;&nbsp;12&nbsp;&nbsp;&nbsp;22&nbsp;&nbsp;&nbsp;32&nbsp;&nbsp;&nbsp;42&nbsp;<br \/>\nMatrix&nbsp;B<br \/>\n&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;2&nbsp;<br \/>\n&nbsp;&nbsp;&nbsp;3&nbsp;&nbsp;&nbsp;&nbsp;4&nbsp;<\/code><\/p>\n<p>I hope your solution met with success and you&#8217;re able to use a structure to describe a matrix. By using this technique, you can manipulate the matrix in various ways without having to rely upon or assume that the grid is square.<\/p>\n<p>For <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6585\">next month&#8217;s Exercise<\/a>, I continue this exploration with some matrix manipulation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the C language, a structure is used to express complex data types. The structure contains members that describe different parts of this complex data, such as a matrix required in this month&#8217;s Exercise.<\/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-6553","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\/6553","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=6553"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6553\/revisions"}],"predecessor-version":[{"id":6617,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6553\/revisions\/6617"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}