{"id":7111,"date":"2025-08-16T00:01:11","date_gmt":"2025-08-16T07:01:11","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7111"},"modified":"2025-08-23T08:48:17","modified_gmt":"2025-08-23T15:48:17","slug":"things-that-fall-more-exp-function-stuff","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7111","title":{"rendered":"Things That Fall (More <em>exp()<\/em> Function Stuff)"},"content":{"rendered":"<p>Text mode graphics were a Big Deal with computers for the longest time. C&#8217;s stream I\/O didn&#8217;t stop various computer games from being developed throughout the 1970s and 1980s. And this limitation isn&#8217;t going to stop me from animating a falling ball, er, asterisk.<br \/>\n<!--more--><br \/>\nIn <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7097\">last week&#8217;s Lesson<\/a>, I explored the <em>exp()<\/em> function, which is the mathematician&#8217;s equivalent of an ice cream sundae. While most of what it does or how it&#8217;s used is beyond me, the output is interesting enough that when applied graphically, it can be used to simulate how gravity affects a falling object.<\/p>\n<p>The exponential function outputs results for a sequential series of numbers as small values that rapidly increase in size. This data can be used as the row value when positioning an object on the terminal (text) screen, which is how it makes the movement look natural. But before plotting, a few useful functions must be gathered to boost the C language&#8217;s lack of &#8220;fun&#8221; capabilities: <em>clear()<\/em>, <em>putat()<\/em>, and <em>delay()<\/em>. These functions are covered in previous Lessons, with the lot presented <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6607\">here<\/a>.<\/p>\n<p>The <em>clear()<\/em> function (macro) uses ANSI codes to clear the screen.<\/p>\n<p>The <em>putat()<\/em> function uses ANSI codes to position the cursor and output a character.<\/p>\n<p>The <em>delay()<\/em> function pauses program execution for a given number of milliseconds.<\/p>\n<p>The <em>setvbuf()<\/em> function is also required. Its purpose is to remove line buffering for the standard output device. This modification allows the asterisks to be output without waiting for a newline or for the output buffer to fill.<\/p>\n<p>Here is my code, which uses the <em>exp()<\/em> function&#8217;s output animate a falling asterisk:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_08_16-Lesson.c\" rel=\"noopener\" target=\"_blank\">2025_08_16-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;math.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\n#define clear() printf(\"\\e[H\\e[2J\")\r\n\r\n<span class=\"comments\">\/* move the cursor to position x (columns)\r\n   and y (rows) and set character c *\/<\/span>\r\nvoid putat(x,y,c)\r\n{\r\n    printf(\"\\e[%d;%dH%c\",y,x,c);\r\n}\r\n\r\n<span class=\"comments\">\/* pause for m milliseconds *\/<\/span>\r\nvoid delay(int m)\r\n{\r\n    long pause;\r\n    clock_t now,then;\r\n\r\n    pause = m*(CLOCKS_PER_SEC\/1000);\r\n    now = then = clock();\r\n    while( (now-then) &lt; pause )\r\n        now = clock();\r\n}\r\n\r\nint main()\r\n{\r\n    int x;\r\n    double b;\r\n    char buffer[BUFSIZ];\r\n\r\n    <span class=\"comments\">\/* initialization *\/<\/span>\r\n    setvbuf(stdout,buffer,_IONBF,BUFSIZ);\r\n    clear();\r\n\r\n    for( x=5; x&lt;64; x++ )\r\n    {\r\n        b=exp( (double)x\/20 );\r\n        putat( 3, (int)b, '*' );\r\n        delay(50);\r\n        putat( 3, (int)b, ' ' );\r\n    }\r\n    putchar('\\n');\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Remember when building this code at the Linux command prompt to add the <code>-lm<\/code> (little L, little M) switch to link in the math library.<\/p>\n<p>In the <em>main()<\/em> function, <em>int<\/em> variable <code>x<\/code> is the looping variable, ranging from 5 to 64. These values were used in last week&#8217;s Lesson as arguments for the <em>exp()<\/em> function &mdash; and they&#8217;re used here again. The results of <code>exp( (double)x\/20 )<\/code> range from 1.000000 through 23.336065. Each value generated is saved in <em>double<\/em> variable <code>b<\/code>.<\/p>\n<p>Immediately, <em>double<\/em> variable <code>b<\/code> is typecast to an integer for use in the <em>putat()<\/em> function. The values, now ranging from 1 to 23, plot the asterisk&#8217;s location on the screen:<\/p>\n<p><code>putat( 3, (int)b, '*' )<\/code><\/p>\n<p>The program pauses for 50 milliseconds. Then the asterisk is &#8220;erased.&#8221; The loop repeats, plotting the asterisk&#8217;s position down the screen. Video of the output appears below:<\/p>\n<div id=\"attachment_7112\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-7112\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2025\/08\/0816-figure1.gif\" alt=\"\" width=\"640\" height=\"396\" class=\"size-full wp-image-7112\" \/><p id=\"caption-attachment-7112\" class=\"wp-caption-text\">Figure 1. Program output: the falling asterisk.<br \/>(Image repeats\/cycles, but the program&#8217;s output does not.)<\/p><\/div>\n<p>Remember that the <em>for<\/em> loop repeats 59 times. A lot of the initial action is the program overwriting the asterisk&#8217;s position with itself. Because the output from the <em>exp()<\/em> function changes little (especially when converted into an integer), the asterisk appears to hang for a while before dropping. This effect is what builds the animation.<\/p>\n<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7119\">next week&#8217;s Lesson<\/a>, I add code to bounce the asterisk across the terminal window.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When applied graphically, the output from the <em>exp()<\/em> function does interesting things. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7111\">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-7111","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\/7111","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=7111"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7111\/revisions"}],"predecessor-version":[{"id":7139,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7111\/revisions\/7139"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}