Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy my insertion sort function does not change the array that I passed from int main?
    primarykey
    data
    text
    <p><strong>SHORT VERSION (Using hard-wired input)</strong></p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; #include &lt;vector&gt; using namespace std; void insertionSort(string theArray[], int n) { //unsorted = first index of the unsorted region, //loc = index of insertion in the sorted region, //nextItem = next item in the unsorted region, //Initially, sorted region is theArray[0] // unsorted region is theArray [1..n-1]. //In general, sorted region is theArray[0..unsorted-1], // unsorted region theArray[unsorted.. n-1] for(int unsorted = 1; unsorted&lt; n; unsorted++) { //At this point, theArray[0..unsorted-1] is sorted. //Find the right position (loc) in theArray[0..unsorted] //for theArray[unsorted], which is the first entry in the //unsorted region; shift, if necessary, to make room string nextItem = theArray[unsorted]; int loc = unsorted; while( (loc &gt; 0) &amp;&amp; (theArray[loc-1] &gt; nextItem) ) { //shift theArray [loc -1] to the right theArray[loc] = theArray[loc-1]; } // At this point, theArray[loc] is where nexItem belongs theArray[loc] = nextItem; // Insert nextItem into sorted region loc--; }// end for } int main() { vector&lt;string&gt; token; int countToken; string input= "I,love,doing,nothing,at,all"; int count =0; for(int i=0; i&lt; input.length(); i++) { if(input[i] == ',') count++; } countToken = count +1; for(int i=0; i&lt; countToken; i++) { int x= input.find(','); token.push_back(input.substr(0,x)); input = input.substr(x+1); } cout &lt;&lt; endl &lt;&lt; "Current String: "; for(int i =0; i&lt; countToken; i++) { cout &lt;&lt; token[i] &lt;&lt;" " ; } cout &lt;&lt; endl; string theArray[countToken]; for(int i =0; i&lt; countToken; i++) { theArray[i] = token[i]; } insertionSort(theArray, countToken); cout &lt;&lt; "SORTED: " ; for(int i =0; i&lt; countToken; i++) { cout &lt;&lt; theArray[i] &lt;&lt; " "; } return 0; }// main </code></pre> <p><strong>FULL VERSION</strong></p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; #include &lt;vector&gt; using namespace std; void insertionSort(string theArray[], int n) { //unsorted = first index of the unsorted region, //loc = index of insertion in the sorted region, //nextItem = next item in the unsorted region, //Initially, sorted region is theArray[0] // unsorted region is theArray [1..n-1]. //In general, sorted region is theArray[0..unsorted-1], // unsorted region theArray[unsorted.. n-1] for(int unsorted = 1; unsorted&lt; n; unsorted++) { //At this point, theArray[0..unsorted-1] is sorted. //Find the right position (loc) in theArray[0..unsorted] //for theArray[unsorted], which is the first entry in the //unsorted region; shift, if necessary, to make room string nextItem = theArray[unsorted]; int loc = unsorted; while( (loc &gt; 0) &amp;&amp; (theArray[loc-1] &gt; nextItem) ) { //shift theArray [loc -1] to the right theArray[loc] = theArray[loc-1]; } // At this point, theArray[loc] is where nexItem belongs theArray[loc] = nextItem; // Insert nextItem into sorted region loc--; }// end for } int main() { vector&lt;string&gt; token; int countToken; while(1) { int answer; cout &lt;&lt; "MENU: " &lt;&lt; endl; cout &lt;&lt; "1. Input String " &lt;&lt; endl; cout &lt;&lt; "2. Sort " &lt;&lt; endl; cout &lt;&lt; "3. Quit " &lt;&lt; endl; cout &lt;&lt; endl; cout &lt;&lt; "Response: "; cin &gt;&gt; answer; cin.ignore(1000, 10); if( answer == 1) // case 1. Input String { string input; cout &lt;&lt; endl &lt;&lt; "Default delimiter is ','"&lt;&lt; endl &lt;&lt; "Enter String: " ; getline(cin, input); // find delimiter int count =0; for(int i=0; i&lt; input.length(); i++) { if(input[i] == ',') count++; } //cout &lt;&lt; "Count: "&lt;&lt; count &lt;&lt;endl; //clear previous vector token.clear(); countToken = count +1; for(int i=0; i&lt; countToken; i++) { int x= input.find(','); token.push_back(input.substr(0,x)); input = input.substr(x+1); } //cout &lt;&lt; "countToken: " &lt;&lt; countToken &lt;&lt; endl; cout &lt;&lt; endl &lt;&lt; "Current String: "; for(int i =0; i&lt; countToken; i++) { cout &lt;&lt; token[i] &lt;&lt;" " ; } cout &lt;&lt; endl; } else if(answer == 2) // case 2. Sort { string theArray[countToken]; for(int i =0; i&lt; countToken; i++) { theArray[i] = token[i]; } //cout &lt;&lt; "COUNTTOKEN: "&lt;&lt; countToken; insertionSort(theArray, countToken); cout &lt;&lt; "SORTED: " ; for(int i =0; i&lt; countToken; i++) { cout &lt;&lt; theArray[i] &lt;&lt; " "; } } else if(answer == 3) { break; } else { cout &lt;&lt; endl &lt;&lt; "Invalid input !" &lt;&lt; endl &lt;&lt; endl; } cout &lt;&lt; endl; }// while return 0; }// main </code></pre> <p>Hi, a brief explanation of my program. I asked user to input a set of string with ',' as delimiter to cut the string off every time ',' is found, and push it to a vector, then pass it to an array, then pass the array to an insertion sort function. (nb: program will not cut correctly if there is any spaces involved in the string)</p> <p>However, my insertion sort function does not seem to change the array that I passed correctly. My program even crashes when I called the function. I read an article, and it said that <strong>arrays are by default passed by reference</strong> Thus, I do not think an ampersand '&amp;' in my function's parameter is necessary to change my array in main. </p> <p>I hope somebody can figure out what's missing in my program Any comments will be much appreciated</p> <p>Thanks</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.
 

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