Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed advice on improving my code: Search Algorithm
    primarykey
    data
    text
    <p>I'm pretty new at C++ and would need some advice on this. Here I have a code that I wrote to measure the number of times an arbitrary integer x occurs in an array and to output the comparisons made.</p> <p>However I've read that by using multi-way branching("Divide and conqurer!") techniques, I could make the algorithm run faster.</p> <p>Could anyone point me in the right direction how should I go about doing it?</p> <p>Here is my working code for the other method I did:</p> <pre><code>#include &lt;iostream&gt; #include &lt;cstdlib&gt; #include &lt;vector&gt; using namespace std; vector &lt;int&gt; integers; int function(int vectorsize, int count); int x; double input; int main() { cout&lt;&lt;"Enter 20 integers"&lt;&lt;endl; cout&lt;&lt;"Type 0.5 to end"&lt;&lt;endl; while(true) { cin&gt;&gt;input; if (input == 0.5) break; integers.push_back(input); } cout&lt;&lt;"Enter the integer x"&lt;&lt;endl; cin&gt;&gt;x; function((integers.size()-1),0); system("pause"); } int function(int vectorsize, int count) { if(vectorsize&lt;0) //termination condition { cout&lt;&lt;"The number of times"&lt;&lt; x &lt;&lt;"appears is "&lt;&lt;count&lt;&lt;endl; return 0; } if (integers[vectorsize] &gt; x) { cout&lt;&lt; integers[vectorsize] &lt;&lt; " &gt; " &lt;&lt; x &lt;&lt;endl; } if (integers[vectorsize] &lt; x) { cout&lt;&lt; integers[vectorsize] &lt;&lt; " &lt; " &lt;&lt; x &lt;&lt;endl; } if (integers[vectorsize] == x) { cout&lt;&lt; integers[vectorsize] &lt;&lt; " = " &lt;&lt; x &lt;&lt;endl; count = count+1; } return (function(vectorsize-1,count)); } </code></pre> <p>Thanks!</p>
    singulars
    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.
 

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