{"id":5474,"date":"2017-02-28T04:00:16","date_gmt":"2017-02-28T04:00:16","guid":{"rendered":"https:\/\/assignment.essayshark.com\/blog\/?p=5474"},"modified":"2022-01-11T15:13:34","modified_gmt":"2022-01-11T15:13:34","slug":"web-assignment-sample-check-for-balanced-parentheses-in-an-expression","status":"publish","type":"post","link":"https:\/\/assignmentshark.com\/blog\/web-assignment-sample-check-for-balanced-parentheses-in-an-expression\/","title":{"rendered":"Web Assignment Sample: Check for Balanced Parentheses in an Expression"},"content":{"rendered":"<p>Hello! In this guide, I\u2019ll show you the program to check the balance of different types of parentheses in an expression using C++.<\/p>\n<p><strong>Idea for Implementation<\/strong><\/p>\n<p>Everything would be much simpler if we had to check only one type of parentheses. If it is required to check the balance of the parentheses of only one type, for example, \u201c(\u201c and \u201c)\u201d, we can read all the symbols one by one, ignoring all the symbols except the braces. If it is an opening bracket we increase the counter by 1, and if it\u2019s a closing bracket we reduce the counter by one. After the check, it\u2019s easy to define if the expression is balanced. If the counter is bigger than zero, we have more opening brackets. But if it goes lower than zero, we can\u2019t check further. So we will use the stack.<!--more--><\/p>\n<p>Algorithm<\/p>\n<p>1) Declare a stack.<br \/>\n2) Now iterate through the expression:<br \/>\na) If the character is the starting bracket \u2018(\u2018 or \u2018{\u2018 or \u2018[\u2018 then push it to the stack.<br \/>\nb) If the character is the closing bracket \u2018)\u2019 or \u2018}\u2019 or \u2018]\u2019 then pop it from the stack and match for relevant parenthesis. If it matches, pop it from the stack.<br \/>\n3) After iterations are complete, if there is a starting bracket left in the stack, then the parentheses are \u201cnot balanced\u201d.<\/p>\n<p>Screenshots of the working program:<\/p>\n<p><a href=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/Check-for-balanced-parentheses-in-an-expression-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-5478\" src=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/Check-for-balanced-parentheses-in-an-expression-1.png\" alt=\"check-for-balanced-parentheses-in-an-expression-1\" width=\"600\" height=\"304\" srcset=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/Check-for-balanced-parentheses-in-an-expression-1.png 677w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/Check-for-balanced-parentheses-in-an-expression-1-300x152.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/Check-for-balanced-parentheses-in-an-expression-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-5480\" src=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/Check-for-balanced-parentheses-in-an-expression-2.png\" alt=\"check-for-balanced-parentheses-in-an-expression-2\" width=\"600\" height=\"304\" srcset=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/Check-for-balanced-parentheses-in-an-expression-2.png 677w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/Check-for-balanced-parentheses-in-an-expression-2-300x152.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>Code listing with comments:<\/p>\n<p>[code language=&#8221;cpp&#8221;]<br \/>\n#include&lt;iostream&gt;<br \/>\n#include&lt;stdlib.h&gt;<br \/>\n#include&lt;stack&gt;\t\t\t\/\/standard C++ data structure<br \/>\nusing namespace std;<\/p>\n<p>\/*prororypes*\/<br \/>\nbool Check(char e[]);<br \/>\nbool Coincide(char c1, char c2);<\/p>\n<p>int main()<br \/>\n{<br \/>\n\tchar a[100];<\/p>\n<p>\tcout &lt;&lt; &quot;Input the expression you want to check: \\n&quot;; cin &gt;&gt; a;<\/p>\n<p>\tif (Check(a))<br \/>\n\t\tprintf(&quot;Balanced \\n&quot;);<br \/>\n\telse<br \/>\n\t\tprintf(&quot;Not balanced \\n&quot;);<\/p>\n<p>\tsystem(&quot;pause&quot;);<\/p>\n<p>\treturn 0;<br \/>\n}<\/p>\n<p>bool Check(char e[])<br \/>\n{<br \/>\n\t\/*stack variable*\/<br \/>\n\tstack&lt;char&gt; Stack;<br \/>\n\tint i = 0;<\/p>\n<p>\t\/* iterate through the expression*\/<br \/>\n\twhile (e[i])<br \/>\n\t{<\/p>\n<p>\t\t\/*If the current element is an opening bracket then pust it to the stack*\/<br \/>\n\t\tif (e[i] == &#8216;(&#8216; || e[i] == &#8216;{&#8216; || e[i] == &#8216;[&#8216;)<br \/>\n\t\t{<br \/>\n\t\t\tStack.push(e[i]);<br \/>\n\t\t}<\/p>\n<p>\t\t\/* If the current element is a closing bracket then check if the stack is<br \/>\n\t\tempty or it is its top element and pop it from the stack*\/<br \/>\n\t\tif (e[i] == &#8216;)&#8217; || e[i] == &#8216;}&#8217; || e[i] == &#8216;]&#8217;)<br \/>\n\t\t{<br \/>\n\t\t\tif (Stack.empty() || !Coincide(Stack.top(), e[i]))<br \/>\n\t\t\t{<br \/>\n\t\t\t\treturn false;<br \/>\n\t\t\t}<br \/>\n\t\t\telse<br \/>\n\t\t\t{<br \/>\n\t\t\t\tStack.pop();<br \/>\n\t\t\t}<br \/>\n\t\t}<br \/>\n\t\ti++;<br \/>\n\t}<\/p>\n<p>\t\/*If the stack is empty then the expression is balanced*\/<br \/>\n\treturn Stack.empty();<br \/>\n}<\/p>\n<p>\/* Coincide for relevent paranthesis *\/<br \/>\nbool Coincide(char c1, char c2)<br \/>\n{<br \/>\n\tif (c1 == &#8216;(&#8216; &amp;&amp; c2 == &#8216;)&#8217;)<br \/>\n\t\treturn true;<br \/>\n\telse if (c1 == &#8216;{&#8216; &amp;&amp; c2 == &#8216;}&#8217;)<br \/>\n\t\treturn true;<br \/>\n\telse if (c1 == &#8216;[&#8216; &amp;&amp; c2 == &#8216;]&#8217;)<br \/>\n\t\treturn true;<br \/>\n\telse<br \/>\n\t\treturn false;<br \/>\n}<br \/>\n[\/code]<\/p>\n<blockquote><p><em>Do you find our web assignment sample high quality? It was completed by an expert in the defined filed. If you are eager to complete the task of the same quality but you are having troubles with this apply for help to AssignmentShark. Our experts will offer <a href=\"https:\/\/assignmentshark.com\/\" target=\"_blank\" rel=\"noopener\">assignment help<\/a> you with any of your technical assignments. The only thing you need to do is to specify the requirements accurately. The rest of work you can rely on out experts. When using our service you have the ability to communicate with experts via live chat. If you need to complete your web assignment as soon as possible do not waste any minute! Make the order right away!<\/em><\/p>\n<p>You can also be interested in <a href=\"https:\/\/assignmentshark.com\/blog\/glsl-shader-examples-blue-color-screen\/\" target=\"_blank\" rel=\"noopener noreferrer\">GLSL shader examples<\/a> posted on our blog.<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Hello! In this guide, I\u2019ll show you the program to check the balance of different types of parentheses in an expression using C++. Idea for Implementation Everything would be much simpler if we had to check only one type of parentheses. If it is required to check the balance of the parentheses of only one [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53,35],"tags":[],"class_list":["post-5474","post","type-post","status-publish","format-standard","hentry","category-it","category-samples"],"_links":{"self":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/5474","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/comments?post=5474"}],"version-history":[{"count":8,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/5474\/revisions"}],"predecessor-version":[{"id":13003,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/5474\/revisions\/13003"}],"wp:attachment":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/media?parent=5474"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/categories?post=5474"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/tags?post=5474"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}