{"id":615,"date":"2022-01-10T17:18:49","date_gmt":"2022-01-10T09:18:49","guid":{"rendered":"https:\/\/blog.liuyingjie.com.cn\/?p=615"},"modified":"2023-11-07T08:32:15","modified_gmt":"2023-11-07T08:32:15","slug":"kr2-solutions-chapter-3-control-flow","status":"publish","type":"post","link":"https:\/\/blog.liuyingjie.com.cn\/?p=615","title":{"rendered":"K&#038;R2 solutions: Chapter 3 &#8211; Control Flow"},"content":{"rendered":"\n<p>Exercise 3.1 Our binary search makes two tests inside the loop, when one would suffice (at the price of more tests outside). Write a version with only one test inside the loop and measure the difference in run-time.<\/p>\n\n\n\n<p>Original for loop code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"c\" class=\"language-c\">\/* binsearch: find x in v[0] &lt;= v[1] &lt;= ... &lt;= v[n-1] *\/\nint binsearch(int x, int v[], int n)\n{\n    int low, high, mid;\n    low = 0;\n    high = n - 1;\n    while (low &lt;= high) {\n        mid = (low + high) \/ 2;\n        if (x &lt; v[mid])\n            high = mid + 1;\n        else if (x &gt; v[mid])\n            low = mid + 1;\n        else \/* found match *\/\n            return mid;\n    }\n    return -1; \/* no match *\/\n}<\/code><\/pre>\n\n\n\n<p>Modified code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"c\" class=\"language-c\">#include&lt;stdio.h&gt;\n\nint binsearch(int x, int v[], int n);\n\nmain() {\n    int arr[] = { 1,3,5,7,8,10,22,24,26,28,41,43,45,47,50,51 };\n    printf(\"%d\", binsearch(7,arr,10));\n}\n\nint binsearch(int x, int v[], int n) {\n    int low, high, mid;\n\n    low = 0;\n    high = n - 1;\n\n    mid = (low + high) \/ 2;\n\n    while (low &lt; high &amp;&amp; x != v[mid]) {\n        if (x &gt; v[mid]){\n            low = mid + 1;\n        } else {\n            high = mid - 1;\n        }\n        mid = (low + high) \/ 2;\n    }\n    if (x == v[mid]){\n        return mid;\n    } else {\n        return -1;\n    }\n}<\/code><\/pre>\n\n\n\n<p>Exercise 3.2 Write a function escape(s,t) that converts characters like newline and tab into visible escape sequences like \\n and \\t as it copies the string t to s . Use a switch . Write a function for the other direction as well, converting escape sequences into the real characters.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\"><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Exercise 3.1 Our binary search makes two tests inside t &hellip; <a href=\"https:\/\/blog.liuyingjie.com.cn\/?p=615\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">K&#038;R2 solutions: Chapter 3 &#8211; Control Flow<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[8],"tags":[],"class_list":["post-615","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/blog.liuyingjie.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/615","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.liuyingjie.com.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.liuyingjie.com.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.liuyingjie.com.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.liuyingjie.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=615"}],"version-history":[{"count":7,"href":"https:\/\/blog.liuyingjie.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/615\/revisions"}],"predecessor-version":[{"id":1027,"href":"https:\/\/blog.liuyingjie.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/615\/revisions\/1027"}],"wp:attachment":[{"href":"https:\/\/blog.liuyingjie.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.liuyingjie.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.liuyingjie.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}