{"id":137,"date":"2020-08-15T15:04:15","date_gmt":"2020-08-15T20:04:15","guid":{"rendered":"https:\/\/www.brezeale.com\/?p=137"},"modified":"2020-08-19T13:47:04","modified_gmt":"2020-08-19T18:47:04","slug":"python-programming-producing-a-multiplication-table","status":"publish","type":"post","link":"https:\/\/www.brezeale.com\/?p=137","title":{"rendered":"Python Programming: Producing a Multiplication Table"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">People learning to program often struggle with how to decompose a problem into the steps necessary to write a program to solve that problem.  This is one of a series of posts in which I take a problem and go through my decision-making process that leads to a program.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The problem: produce a multiplication table.  I assume that at this point you know about statements, conditionals, loops, and functions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can find a video with more details at <a href=\"https:\/\/youtu.be\/QJqfYn_4pmw\">https:\/\/youtu.be\/QJqfYn_4pmw<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Design<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Think of design as being similar to planning a trip.  Given a specific destination, how do I get there from where I am now?  I can better understand what I need to do by working a small example by hand.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An n x n multiplication table where n = 4 looks like this:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\n      1    2    3    4  \n   --------------------\n 1 |  1    2    3    4  \n 2 |  2    4    6    8  \n 3 |  3    6    9   12  \n 4 |  4    8   12   16 \n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">To produce this, I took each row number in the left-hand column and multiplied by the row of numbers across the top.  So the first row of products is <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\n    1 x (1, 2, 3, 4) = (1, 2, 3, 4)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">the second row is <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\n    2 x (1, 2, 3, 4) = (2, 4, 6, 8)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">and so forth.  This may seem obvious given how young you probably were when you were first exposed to multiplication tables, but by doing this I am trying to understand my decision-making process while producing the table.  I can see that I repeat the process of multiplying a number by the numbers 1 to n.  The number than I multiply by each row of numbers is the row number itself-row 1, row 2, and so forth.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I can see that I have a two processes that repeat and therefore I will probably need two loops.  One loop produces the integers from 1 to n with each being multiplied by the row number.  The other loop produces the row numbers, which themselves are the integers from 1 to n.  The loop that produces the row numbers will be the outer loop, with the other loop being inside of it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">At this point I have the function<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ndef multTable(n) :\n    for r in range(1, n+1) :\n        for c in range(1, n+1) :\n            print(&quot;%3d  &quot; % (r*c), end=&quot;&quot;)\n\n        print()\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">that produces<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\n  1    2    3    4  \n  2    4    6    8  \n  3    6    9   12  \n  4    8   12   16\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">This is my starting point.  Now I want to add the row and column numbers that make it look like a traditional multiplication table.f  What should I do first?  Since printing the column values will require indenting the output with spaces, I want to do that after I see how much space the row numbers require.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Before printing a row of output value, that is, the products, I need to print the row number followed by the vertical bar.  By default the print() function includes a newline, so I suppress it using end=&#8221;&#8221;.  Then the row of products will follow the line number.  At this point my function looks like this:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ndef multTable(n) :\n    for r in range(1, n+1) :\n        print(&quot;%2d |&quot; % r, end=&quot;&quot;)\n        for c in range(1, n+1) :\n            print(&quot;%3d  &quot; % (r*c), end=&quot;&quot;)\n\n        print()\n\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">and it produces<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\n 1 |  1    2    3    4  \n 2 |  2    4    6    8  \n 3 |  3    6    9   12  \n 4 |  4    8   12   16  \n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Now I am ready to add the column numbers and a row of dashes to form a line.  For this, I decided to create a separate function:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ndef printHeader(n) :\n    print(&quot;    &quot;, end=&quot;&quot;)\n\n    for c in range(1, n+1) :\n        print(&quot;%3d  &quot; % c, end=&quot;&quot;)\n\n    print()\n\n    print(&quot;   &quot;, end=&quot;&quot;)\n\n    for c in range(1, n+1) :\n        print(&quot;-----&quot;, end=&quot;&quot;)\n\n    print()\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Much of the logic in <code>printHeader()<\/code> is similar to what I wrote in <code>multTable()<\/code>.  Could I have put the logic of <code>printHeader()<\/code> inside <code>multTable()<\/code> instead of it&#8217;s own function?  Sure, but I think this makes the underlying logic easier to follow. \n It also has the benefit that I can &#8220;turn off&#8221; the header if I wish by simply commenting out the function call.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is the final program:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef printHeader(n) :\n    print(&quot;    &quot;, end=&quot;&quot;)\n\n    for c in range(1, n+1) :\n        print(&quot;%3d  &quot; % c, end=&quot;&quot;)\n\n    print()\n\n    print(&quot;   &quot;, end=&quot;&quot;)\n\n    for c in range(1, n+1) :\n        print(&quot;-----&quot;, end=&quot;&quot;)\n\n    print()\n\n\ndef multTable(n) :\n    printHeader(n)\n\n    for r in range(1, n+1) :\n        print(&quot;%2d |&quot; % r, end=&quot;&quot;)\n        for c in range(1, n+1) :\n            print(&quot;%3d  &quot; % (r*c), end=&quot;&quot;)\n\n        print()\n\n#######  main  #######\nn = int( input(&quot;enter size (15 rows max):  &quot;) )\n\nmultTable(n)\n\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>People learning to program often struggle with how to decompose a problem into the steps necessary to write a program to solve that problem. This is one of a series of posts in which I take a problem and go through my decision-making process that leads to a program. The problem: produce a multiplication table. I assume that at this point you know about statements, conditionals, loops, and functions. You can find a video with more details at https:\/\/youtu.be\/QJqfYn_4pmw. Design&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.brezeale.com\/?p=137\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[9,5],"tags":[],"class_list":["post-137","post","type-post","status-publish","format-standard","hentry","category-programming","category-python-programming"],"_links":{"self":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts\/137","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=137"}],"version-history":[{"count":11,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts\/137\/revisions"}],"predecessor-version":[{"id":296,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts\/137\/revisions\/296"}],"wp:attachment":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}