{"id":8516,"date":"2022-09-14T01:36:14","date_gmt":"2022-09-13T20:06:14","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-stack-overflow-in-recursive-function-language-c\/"},"modified":"2022-09-14T01:36:14","modified_gmt":"2022-09-13T20:06:14","slug":"solved-stack-overflow-in-recursive-function-language-c","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-stack-overflow-in-recursive-function-language-c\/","title":{"rendered":"[Solved] Stack overflow in recursive function (language C)"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-27206653\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"27206653\" data-parentid=\"27206453\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>You must protect against dividing by zero. Specifically, any time the value of <code>q<\/code> may be zero. At a minimum you need to test <code>q<\/code> each time you enter your function. With the additional code you posted, if you insure <code>q<\/code> isn&#8217;t <code>0<\/code> before calling <code>geoprogress<\/code>, that is sufficient as well. Regardless, a test in <code>geoprogress<\/code> is a solid failsafe:<\/p>\n<pre><code>int geoprogress (int a, int q, int n)\n{\n    int result;\n    if (n==0)\n    {\n        result = a;\n    }\n    if (q == 0)\n    {\n        return (value of choice);   \/* or just return *\/\n    }\n    if (n == -1)\n    {\n        result = geoprogress (a, q, n + 1)\/q;\n    }\n    else\n    {\n        result = geoprogress (a, q, n - 1)\/q;\n    }\n    return result;\n}\n<\/code><\/pre>\n<p>You will also need to make sure your function ultimately terminates with <code>n = 0<\/code> or in some other way. This makes <code>if (n == -1)<\/code> look suspicious. What if <code>n = -2<\/code>? The value of <code>n<\/code> becomes progressively more negative and the function may never terminate. Without seeing the remainder of the code it is hard to tell, but it looks like <code>if (n == -1)<\/code> would work better as <code>if (n &lt; 0)<\/code>.<\/p>\n<p>There is also a logic error in the function concerning when\/how it terminates. If <code>n &gt; 0<\/code>, your function enters an <strong>endless loop<\/strong> toggling the value of <code>n<\/code> between <code>0<\/code> and <code>-1<\/code>:<\/p>\n<pre><code>result: 0  geoprogress (2, 3, 4)\nresult: 0  geoprogress (2, 3, 3)\nresult: 0  geoprogress (2, 3, 2)\nresult: 0  geoprogress (2, 3, 1)\nresult: 0  geoprogress (2, 3, 0)  \/* non-terminating loop entered *\/\nresult: 0  geoprogress (2, 3, -1)\nresult: 0  geoprogress (2, 3, 0)\nresult: 0  geoprogress (2, 3, -1)\nresult: 0  geoprogress (2, 3, 0)\nresult: 0  geoprogress (2, 3, -1)\n(snip)\n<\/code><\/pre>\n<p>Your test <code>if (n == 0)<\/code> simply sets <code>result = a<\/code> and has no control over function return. Your new value of <code>result = a<\/code> is then immediately overwritten with your next call to <code>result = geoprogress()<\/code>. It looks like you intended:<\/p>\n<pre><code>if (n==0)\n{\n    return a;\n}\n<\/code><\/pre>\n<p>Otherwise, your recursion never terminates because <code>n<\/code> always bounces around between <code>0<\/code> and <code>-1<\/code>, neither of which will terminate the recursion. <strong>Why?<\/strong> Think about it, look at the following code:<\/p>\n<pre><code>if (n == -1)\n{\n    result = geoprogress (a, q, n + 1)\/q;\n}\nelse\n{\n    result = geoprogress (a, q, n - 1)\/q;\n}\n<\/code><\/pre>\n<p>Pick any value for <code>n<\/code>. Now ask yourself <strong>when will this return?<\/strong> Answer: It won&#8217;t. You either call <code>geoprogress<\/code> again with <code>n + 1<\/code> or <code>geoprogress<\/code> again with <code>n - 1<\/code>. You never reach <code>return result<\/code> in your function. That is what causes me to believe the logic is probably intended to be <code>if (n == 0) return a;<\/code>. That gives the function a way to return. Either that or one of the other <code>result =<\/code> needs to be a <code>return<\/code>.<\/p>\n<p>A recursive function needs at a minimum two things <strong>(1)<\/strong> appropriate setup of the values before entering recursion, and <strong>(2)<\/strong> a way to return from, or terminate, the recursion. There is virtually no setup in your recursive logic. The only thing you are doing is testing <code>n<\/code> and increasing or decreasing <code>n<\/code> by <code>1<\/code>. With that being the only <strong>setup<\/strong> you provide no way for the recursion to ever terminate as the function is currently written.<\/p>\n<p>Also, you are performing <code>integer division<\/code>. This makes <code>result = 0<\/code> any time <code>geoprogress (...) &lt; q<\/code>. If you are interested in fractional values, you will need to make result a <code>float<\/code> or <code>double<\/code> as well as the type for <code>geoprogress<\/code>. With the values you provide, the answer is always <code>3<\/code> if you terminate with <code>return a;<\/code>E.g.:<\/p>\n<pre><code>i: 0  result: 3\ni: 1  result: 3\ni: 2  result: 3\ni: 3  result: 3\n<\/code><\/pre>\n<p>What are you modeling and what should the values be? I can continue to look at the function in the abstract, but if I knew what you were modeling, that would really help. Is this representative of some equation or some numerical expansion? Recursion is tricky enough when you have a clear model, recursively modeling the unknown is much more difficult.<\/p>\n<hr>\n<p>After working through the logic of your code once more, what it looks like is missing is an <code>else<\/code> clause. The recursion works properly as follows (leaving you to prevent <code>q=0<\/code>, and with the integer div issue)<\/p>\n<pre><code>int geoprogress (int a, int q, int n)\n{\n    int result = 0;\n\n    if (n==0)\n    {\n        result = a;\n    }\n    else\n    {\n        if (n == -1)\n        {\n            result = geoprogress (a, q, n + 1)\/q;\n        }\n        else\n        {\n            result = geoprogress (a, q, n - 1)\/q;\n        }\n    }\n\n    return result;\n}\n<\/code><\/pre>\n<p>That is your original code with an additional <code>else<\/code> which was all that was needed to make recursion terminate on <code>(n == 0)<\/code>. Let me know if you have any more questions.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">6<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Stack overflow in recursive function (language C) <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] You must protect against dividing by zero. Specifically, any time the value of q may be zero. At a minimum you need to test q each time you enter your function. With the additional code you posted, if you insure q isn&#8217;t 0 before calling geoprogress, that is sufficient as well. Regardless, a test &#8230; <a title=\"[Solved] Stack overflow in recursive function (language C)\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-stack-overflow-in-recursive-function-language-c\/\" aria-label=\"More on [Solved] Stack overflow in recursive function (language C)\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324,413,494,2422,992],"class_list":["post-8516","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-function","tag-recursion","tag-stack-overflow","tag-visual-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Stack overflow in recursive function (language C) - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-stack-overflow-in-recursive-function-language-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Stack overflow in recursive function (language C) - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] You must protect against dividing by zero. Specifically, any time the value of q may be zero. At a minimum you need to test q each time you enter your function. With the additional code you posted, if you insure q isn&#8217;t 0 before calling geoprogress, that is sufficient as well. Regardless, a test ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-stack-overflow-in-recursive-function-language-c\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-13T20:06:14+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-stack-overflow-in-recursive-function-language-c\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-stack-overflow-in-recursive-function-language-c\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Stack overflow in recursive function (language C)\",\"datePublished\":\"2022-09-13T20:06:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-stack-overflow-in-recursive-function-language-c\/\"},\"wordCount\":513,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\",\"function\",\"recursion\",\"stack-overflow\",\"visual-c++\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-stack-overflow-in-recursive-function-language-c\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-stack-overflow-in-recursive-function-language-c\/\",\"name\":\"[Solved] Stack overflow in recursive function (language C) - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-09-13T20:06:14+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-stack-overflow-in-recursive-function-language-c\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-stack-overflow-in-recursive-function-language-c\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-stack-overflow-in-recursive-function-language-c\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Stack overflow in recursive function (language C)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Stack overflow in recursive function (language C) - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-stack-overflow-in-recursive-function-language-c\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Stack overflow in recursive function (language C) - JassWeb","og_description":"[ad_1] You must protect against dividing by zero. Specifically, any time the value of q may be zero. At a minimum you need to test q each time you enter your function. With the additional code you posted, if you insure q isn&#8217;t 0 before calling geoprogress, that is sufficient as well. Regardless, a test ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-stack-overflow-in-recursive-function-language-c\/","og_site_name":"JassWeb","article_published_time":"2022-09-13T20:06:14+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-stack-overflow-in-recursive-function-language-c\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-stack-overflow-in-recursive-function-language-c\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Stack overflow in recursive function (language C)","datePublished":"2022-09-13T20:06:14+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-stack-overflow-in-recursive-function-language-c\/"},"wordCount":513,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","function","recursion","stack-overflow","visual-c++"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-stack-overflow-in-recursive-function-language-c\/","url":"https:\/\/jassweb.com\/solved\/solved-stack-overflow-in-recursive-function-language-c\/","name":"[Solved] Stack overflow in recursive function (language C) - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-13T20:06:14+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-stack-overflow-in-recursive-function-language-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-stack-overflow-in-recursive-function-language-c\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-stack-overflow-in-recursive-function-language-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Stack overflow in recursive function (language C)"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/8516","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=8516"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/8516\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=8516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=8516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=8516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}