Hello-
I need to do a selection sort on two different 2d arrays, sorting by the last column of one of them (thirdarray). The first column of thirdarray is the effective column size for the first (morearray) array. I am so confused. I cant find any examples to help me. Here is what I have so far:

int selectionsort(int morearray[][5], int thirdarray[][3],int total){
       int temp[5], index_of_largest;
       for ( ;total>0;total--){
           index_of_largest=0;
          for (int index=1;index<=total;index++){
              if (thirdarray[index][2]>thirdarray[index+1][2]){
                 index_of_largest=index;
              }
              if (total!=index_of_largest){
                 for (int f=0;f<thirdarray[index][0];f++){
                 temp[index]=morearray[index][total];
                 morearray[index][total]=morearray[index][index_of_largest];
                 morearray[index][index_of_largest]=temp[index];
                 }
                 }
          }
      }
       return(twoarray[total][4]);
       return(morearray[total][5]);

Let's start with the easy thing - why does this function return anything, and more importantly, your two return statements at the end will not do what you may think you're doing. Only the first return statement will execute, because once it does, you leave the function and the second one does nothing. Ever.

Also, first return references an array that does not exist in this function. Second return uses column index [5], which is past the end of the rows. [4] is the largest legal column index for morearray.

Now to the good stuff.
The loop controlling the comparisons should stop at the last element, which is (normally) total-1. Thus, your for loop should be:

for (int index=1;index<total;index++)   // less than, not less than or equal

Your comparison is written like that of Bubble Sort (compares adjacent items). It should compare the current element to the element at index_of_largest. Like so:

if (thirdarray[index][2]>thirdarray[index_of_largest][2])
			{
				index_of_largest=index;
			}

When you do the swap, you must also swap the rows of thirdarray, since each row there correesponds to a row in morearray.

If total is the count of elements (rows) to be sorted, you cannot use total as the index, you need to subtract one (total-1) in several places.
A better strategy might be to declare a variable to be the last valid index (row) of arrays, set it to total-1 and use it as the loop counter. int last = total-1; Val

Thanks for the help. The return stuff was from a previous program. I 'm sorry, I left that in accidentally. Here's what i've got now:

case Quit:
               selectionsort(morearray, thirdarray, mttIndex-1);
                    cout << "\nOption 2: \n"; 
                    for (int h=0;h<mttIndex;h++){
                        for (int b=0;b<1;b++){
                            cout << "\nYour response was: ";             //displays review of option 2 answers
                            cout << thirdarray[h][1];
                            cout << "   The equation was: ";
                            int a=0;
                            for (int q=0;q<thirdarray[h][0];q++){
                                cout << morearray[h][q];
                                ++a;
                                if (a!= thirdarray[h][0]){                    //loop for numbers generated
                                   cout << " + ";
                                }  
                            }    
                                cout << " = ";
                                cout << thirdarray[h][2];
                                cout << "\n\n";
                        }
                    }
               cout << endl;
          }
}      





int selectionsort(int morearray[][5], int thirdarray[][3], int total){
       int tempone[5], temptwo[3],index_of_largest;
         cout << "MttIndex :"<< total;
       for ( ;total>0;total--){
           index_of_largest=0;
          for(int index=0;index<total;index++){
              if (thirdarray[index][2]<thirdarray[index_of_largest][2]){
                 index_of_largest=index;
              }
              if (total!=index_of_largest){
                 for (int f=0;f<thirdarray[index][0];f++){
                     tempone[f]=morearray[index][f];
                     morearray[index][f]=morearray[index+1][f];
                     morearray[index+1][f]=tempone[f];
                     
                     temptwo[f]=thirdarray[index][f];
                     thirdarray[index][f]=thirdarray[index+1][f];
                     thirdarray[index+1][f]=temptwo[f];
                 }
              }
          }
          }
}

When I sent 'total', it was the total minus 1. I hope that is what you meant. I made two temps instead of one and attempted to swap all of them. Now when I run it, the numbers of the first equation don't even show up.

Well, you've changed so many things, it's now a new batch of problems.
First, you've changed the direction of your comparison, so we must be clear what order you want to sort. If you want an ascending order (the typical case) then

if (thirdarray[index][2]<thirdarray[index_of_largest][2]){
				index_of_largest=index;

should use the greater than ( > ) operator. Otherwise, you are finding lower values to put into the "index_of_largest" which really should be an "index_of_smallest" in that case.

When you do the swapping of rows int the morearray, you should exchange all 5 columns, otherwise you're corrupting the data. Likewise, thirdarray should be controlled by a loop that moves 3 elements per row. There's no simple way to get around copying the two arrays in separate loops, since they are not of the same size.

Again, you're getting Bubble Sort code into your swapping now. "index" and "index+1" have no place in Selection sort. Swap should look like:

tempone[f]=morearray[total][f];
	morearray[total][f]=morearray[iindex_of_largest][f];
	morearray[iindex_of_largest][f]=tempone[f];

You had the indexing of this correct in your first sample.

I do want it sorted in descending order. My problem now is that I don't understand how to swap all 5 or all 3 at a time.

You need separate loops for each array to be swapped, since they are of different column dimensions. A loop of 5 steps for the first one, a loop of three steps for the other. Like so:

int f;
for ( f = 0; f < 5; f++ )
{
	tempone[f] = morearray[total][f];
	morearray[total][f] = morearray[index_of_largest][f];
	morearray[index_of_largest][f] = tempone[f];
}

for (  f = 0; f < 3; f++ )
{
	temptwo[f] =thirdarray[total][f];
	thirdarray[total][f] = thirdarray[index_of_largest][f];
	thirdarray[index_of_largest][f] = temptwo[f];
}

Also, put a little whitespace in your code - separate operators from their operands. Makes for more readable code.

Actually, tempone and temptwo don't have to be arrays. You only hold one value at a time when swapping, and don't care about it after each swap iteration. Just make them plain int's.

Val

Thank you so much for your help. I just don't understand sorting. The examples in the book the teacher gave us (that he wrote) don't even run and make no sense. Here is my new code:

void selectionsort(int morearray[][5], int thirdarray[][3],int total){
       int f, tempone[5], temptwo[3], index_of_largest;
       for ( ; total > 0; total --){
           index_of_largest=0;
          for (int index = 0; index < total; index++){
              if (thirdarray[index][2] < thirdarray[index+1][2]){
                 index_of_largest=index;
                 if (total != index_of_largest){
                    for ( f = 0; f < 5; f++ ){
	                    tempone[f] = morearray[total][f];
	                    morearray[total][f] = morearray[index_of_largest][f];
	                    morearray[index_of_largest][f] = tempone[f];
                     }
                     for (  f = 0; f < 3; f++ ){
	                     temptwo[f] =thirdarray[total][f];
                         thirdarray[total][f] = thirdarray[index_of_largest][f];
	                     thirdarray[index_of_largest][f] = temptwo[f];
                      }
                 }
              }
          }
      }
}

I tried using ints rather than arrays for the temps, but the compiler gave me an "invalid converison" error. With this code, the data does not get sorted at all. It just goes out the way it came in! Are there still problems?

You need separate loops for each array to be swapped, since they are of different column dimensions. A loop of 5 steps for the first one, a loop of three steps for the other. Like so:

int f;
for ( f = 0; f < 5; f++ )
{
	tempone[f] = morearray[total][f];
	morearray[total][f] = morearray[index_of_largest][f];
	morearray[index_of_largest][f] = tempone[f];
}

for (  f = 0; f < 3; f++ )
{
	temptwo[f] =thirdarray[total][f];
	thirdarray[total][f] = thirdarray[index_of_largest][f];
	thirdarray[index_of_largest][f] = temptwo[f];
}

Also, put a little whitespace in your code - separate operators from their operands. Makes for more readable code.

Actually, tempone and temptwo don't have to be arrays. You only hold one value at a time when swapping, and don't care about it after each swap iteration. Just make them plain int's.

Val

Look at line 6 of what you just posted. You put the [index+1] back in! that should be [index_of_largest]

And please work on your indenting - you just keep going further and further to the right, which makes for misleading code. That leads to getting the closing curly braces out of sync with where they should be. So, your loops probably are not doing quite what they should.

When you made tempone and temptwo plain int's, did you still have array indexes with them in the swap?

Here's what your function ought to look like:

void selectionsort(int morearray[][5], int thirdarray[][3],int total)
{
	int f, tempone, temptwo, index_of_largest;
	for ( ; total > 0; total --)
	{
		index_of_largest=0;
		for (int index = 0; index < total; index++)
		{
			if (thirdarray[index][2] < thirdarray[index_of_largest][2])
				index_of_largest=index;
		}
		if (total != index_of_largest)
		{
			for ( f = 0; f < 5; f++ )
			{
				tempone = morearray[total][f];
				morearray[total][f] = morearray[index_of_largest][f];
				morearray[index_of_largest][f] = tempone;
			}
			for (  f = 0; f < 3; f++ )
			{
				temptwo =thirdarray[total][f];
				thirdarray[total][f] = thirdarray[index_of_largest][f];
				thirdarray[index_of_largest][f] = temptwo;
			}
		}
	}
}

I can't believe the stupid mistakes I made. I think I just needed to take a break. I am done now, and it is working. Thanks so much for all your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.