Note that there are some explanatory texts on larger screens.

plurals
  1. POMerge sort on structs not working
    primarykey
    data
    text
    <p>I am performing a merge sort on a struct. The sort is performed on the destination cities which is an array When I tried it with plain arrays it worked.It doesnt work for structures though :(</p> <pre><code>#include &lt;fstream&gt; // for std::ifstream #include &lt;sstream&gt; // for std::istringstream #include &lt;cstring&gt; // for std::string and std::getline #include &lt;iostream&gt; #include &lt;ctype.h&gt; #include &lt;stdio.h&gt; #include&lt;algorithm&gt; #include &lt;string.h&gt; using namespace std; #define MAX 30 #define WORD 3 typedef struct node{ int nodeId; char destCity[MAX]; char arrCity[MAX]; int time; }NODE; typedef struct edge{ int adjoin; int distance; }EDGE; typedef struct graph{ NODE cityNode[MAX]; EDGE e[MAX][MAX]; }GRAPH; GRAPH graph,graphCpy,Temp; GRAPH currentArray; void MergeA(int low ,int mid , int high) { int i = low, j = mid+1 , k = low; while(i &lt;= mid &amp;&amp; j &lt;= high) { if(currentArray.cityNode[i].destCity &lt;= currentArray.cityNode[j].destCity) { strcpy(Temp.cityNode[k].destCity,currentArray.cityNode[i].destCity); i++; } else { strcpy(Temp.cityNode[k].destCity,currentArray.cityNode[j].destCity); Temp[k].assign(currentArray[j]); j++; } k++; } if(i &gt; mid ) { for(int h = j ;h &lt;= high ; h++ ) { strcpy(Temp.cityNode[k].destCity,currentArray.cityNode[h].destCity); k++; } } else for(int h = i; h&lt;= mid ; h++ ) { strcpy(Temp.cityNode[k].destCity,currentArray.cityNode[h].destCity); k++; } for(int i = low; i &lt;= high ; i++){ strcpy(currentArray.cityNode[i].destCity,Temp.cityNode[i].destCity); } } void MergeSortA(int low , int high) { int mid = 0; if(low &lt; high) { mid = low + (high-low)/2; cout&lt;&lt;"beforemerge"&lt;&lt;endl; MergeSortA(low , mid); MergeSortA(mid+1,high); MergeA(low,mid,high); } } int main(){ std::ifstream infile("theWords.txt"); std::string line; while (std::getline(infile,(line)) &amp;&amp; count&lt;30){ std::istringstream iss(line); if ((iss) &gt;&gt;graph.cityNode[count].destCity &gt;&gt; graph.cityNode[count].arrCity &gt;&gt; graph.cityNode[count].time){ graph.cityNode[count].nodeId = count ; count++; } graphCpy= graph; currentArray= graphCpy; MergeSortA(0,count); for(int i = 0; i &lt;= count ; i++){ cout &lt;&lt; currentArray.cityNode[i].destCity &lt;&lt;endl; } } </code></pre> <p>I input values into graph copy which is then given to currentArray!!</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