Note that there are some explanatory texts on larger screens.

plurals
  1. PODetermining asymptotic complexity of program
    primarykey
    data
    text
    <p>Hey guys I'm fairly new to c++ and am trying to determine the asymptotic complexity of my program which takes in an input and determines if it's a polynomial or not.</p> <p>"If the length of the input expression is m chars, what is the big-Oh complexity of your program with respect to m?"</p> <p>My guess is O(m*log m) in which the first m is the for loop that iterates m times and log m is the while loop that counts exponents greater than 1 digit.</p> <p>Also, I'm trying to save a "largest" value which holds the largest exponent in order to calculate the polynomials runtime complexity. However, I am having confusion with storing the exponent correctly. Can anyone recommend an easier way? </p> <p>example input: "n^23 + 4n^10 - 3" should have 23 as the largest exponent</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; using namespace std; int main() { string input; int pcount = 1; //count for # of variables( min 3 to be poly) int largest = 0; // used for complexity int temp=0; cout &lt;&lt; "Enter equation below. " &lt;&lt; endl; getline(cin,input); //to input polynomial cout &lt;&lt; endl &lt;&lt; input &lt;&lt; endl; if (input[0] == '-' || input[0] == '+') //to check for '-' as first char pcount--; bool pcheck = true; for (unsigned i = 0; i &lt; input.size(); i++) { temp = 0; cout &lt;&lt; input[i] &lt;&lt; endl; if ( input[i] == '+' || input[i] == '-' ) pcount++; if ( input[i] == 'n') //checks for integer { if ( input[i+1] &amp;&amp; input[i+1] == '^' ) { if (input[i+2] == 46 || input[i+2] == 43 || input[i+2] == 45) { cout &lt;&lt; "fail" &lt;&lt; endl; pcheck = false; } temp = input[i+2]-48; // to check for largest exp while ( input[i+2] != 43 &amp;&amp; input[i+2] != 45) { if ( i+3 == input.size()) { cout &lt;&lt; "break"; break; } if( input[i+2] &lt; 48 || input[i+2] &gt;57) // 48-57 is ascii { cout &lt;&lt; "fail" &lt;&lt; endl; pcheck = false; } i++; temp = temp * 10; temp = temp + (input[i+2]-48); if (temp &gt; largest) largest = temp; } } } } if ( pcheck == true &amp;&amp; pcount &gt;= 3) //valid ints and at least 3 variables { cout &lt;&lt; "polynomial detected!" &lt;&lt; endl &lt;&lt; "The big-Oh complexity is O(n^" &lt;&lt; largest &lt;&lt; ")." &lt;&lt; endl; } else cout &lt;&lt; "not a poly" &lt;&lt; endl; return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload