Note that there are some explanatory texts on larger screens.

plurals
  1. POC++: Number Arranging Program Not Working Properly
    primarykey
    data
    text
    <p>I have tried making a program to sort numbers of an array.</p> <p>I have done my best but there is this problem: Although I do a loop to swap the numbers and arrange them, when I output the array, nothing changes and the array remains the same.</p> <p>The code will make everything clearer</p> <p>This is the main function:</p> <pre><code>int main(){ int arr[10]; //For loop to get from user numbers to be put into the array for ( int i = 0; i&lt;10; i++){ cout &lt;&lt; "Enter the number to be recorded: "; cin &gt;&gt; arr[i]; cout &lt;&lt; endl; } // Set counter n to 0 ( counts numbes of number swaps) int n = 0; do { //re sets counter to 0 n=0; //Check the entire loop if arr[i] bigger than arr[i+1] and swaps their values if true then adds 1 to n for ( int i = 0; i&gt;9; i++){ if(arr[i]&gt;arr[i+1]){ swap(&amp;arr[i], &amp;arr[i+1]);//swaps by sending the addresses of the two array elements the pointers in the swap function n++; } } }while(n&gt;0); // if counter = 0 then end (therefore the numbers are arranged correctly since no swapping happened) cout &lt;&lt; "The numbers ordered are:\n\n"; // Loop to output the arranged array for (int i =0; i&lt;10; i++){ cout &lt;&lt; arr[i] &lt;&lt; ", "; } cout&lt;&lt;endl; system("PAUSE"); return 0;} </code></pre> <p>This is the swap function:</p> <pre><code>void swap ( int *p, int *t){ int temp; temp = *p; *p = *t; *t = temp;} </code></pre> <p>I hope you guys can help me with my problem here and tell me what's wrong with this code</p> <p>Thank you all</p>
    singulars
    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