Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding a sorting algorithm (bubble sort) to a program
    text
    copied!<p>Here is a question i got from a past paper in our university:</p> <ol> <li>The user can input the array size</li> <li>User can enter some values to the array;(until it fit the array size given by the user</li> <li>The array input by user .should be sorted using bubble sort algorithm </li> </ol> <p>For example </p> <ul> <li>I input the size 3</li> <li>I can enter three numbers let's assume <code>12, 2, 5</code></li> </ul> <p>It should be sorted like this 2.5.12</p> <p>Using two functions, an input function and the bubble sort algorithm function, I wrote the input function like this:</p> <pre><code>void input_score(int array[], int score) { for(int i=0; i&lt;score; i++) { cout&lt;&lt;"enter the machine values\n"; cin&gt;&gt;array[i]; } for(int i=0; i&lt;score; i++) { cout&lt;&lt;array[i]; } } </code></pre> <p>Then after that I want to call the bubble sort algorithm function to sort it and print:</p> <pre><code>void sort_array(int array[],int score){ int temp; for(int i=0;i&lt;score;i++){ for(int j=i+1;j&lt;score;j++){ if(array[i]&gt;array[j]){ temp=array[i]; array[i]=array[j]; array[j]=temp; } } } cout&lt;&lt;"Sorted elements\n"; for(i=0;i&lt;score;i++) cout&lt;&lt;array[i]&lt;&lt;"\n; } </code></pre> <p>So i wrote the statement in the input function like this</p> <pre><code>for(i=0;i&lt;=size;i++){ sort_array(array[i],size); } </code></pre> <p>Am i correct? By the way here is my main function</p> <pre><code>int main() { cout&lt;&lt;" enter number of machines\n"; int a; cin&gt;&gt;a; int array[a]; input_score(array,a); } </code></pre>
 

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