I am having trouble with this array. I need to sort the second column in descending order. Please have a look and tell me where I went wrong. Much appreciated.

const int sizex=20;
const int sizey=2;

void descend(int arrayx[][sizey],int sizex,int sizey);
void printarray(int arrayx[][sizey],int sizex,int sizey);



int main()
{	
	int score[sizex][sizey];
const int sizex=20;
const int sizey=2;
	int id, test;
	for (int i=0; i<20; i++)
	{id=rand()%90000+10000;
	test=rand()%71+30;
	score[i][0]=id;
	score[i][1]=test;
cout<<"1 st: "<<score[i][0]<<" \t 2nd: "<<score[i][1]<<"\n";
	}
	
	descend(score, sizex, sizey);
    
	return 0;
}

void printarray(int arrayx[sizex][sizey], int sizex, int sizey)
{
	for(int i=0; i<=sizex-1; i++)
	{for (int j=0; j<sizey-1; j++)
	cout<<"Sorted 2nd: "<<arrayx[i][j]<<"\t";
	cout<<"\n";
	}
}

void descend(int arrayx[sizex][sizey], int sizex, int sizey)
{int temp[20][2];
for (int i=0; i<sizex; i++)
	{for (int j=i+1; j<sizex; j++)
		{if (arrayx[0][j]>arrayx[0][i])
		{for(int k=0; k<sizex; k++)
		{
			{temp[i][k]=arrayx[j][k];
			arrayx[j][k]=arrayx[i][k];
			arrayx[i][k]=temp[i][k];
			}
		}		cout<<"\n";
			printarray(arrayx,sizex,sizey);
}
	}
}
}

Recommended Answers

All 19 Replies

Just looking at it with a glance, you have your const int defined at wrong location. Is it compiling? You need to define them before you declare your array.

Will take a look at the sorting.

Also there is nothing right about the descend() function.

In your if statement the subscript 0 should be used in the second subscript not first.

temp should not be a 2d array, it should either be just an integer or at most it should be just an array with two elements.

You don't need the third loop k definitely not running through sizex. You should just swap the values using the temp int.

Search and study bubble sort since looks like that's what you are trying to implement.

Does anyone know how to fix this? I tried the posters suggestions and it did not work.

Please post the revised code.

What seems to be wrong? It really helps to explain the problem.

And if you made changes, you need to post the new code. You might also want to post just the part that has the problem.

Here is the revised code of the function that he said was bad:

void descend(int arrayx[sizex][sizey], int sizex, int sizey)
{int temp;
for (int i=0; i<sizex; i++)
	{for (int j=0; j<sizex; j++)
		{if (arrayx[0][j]>arrayx[0][i])
		
		{
			{temp=arrayx[0][j];
			arrayx[0][j]=arrayx[0][i];
			arrayx[0][i]=temp;
			}
		}		cout<<"\n";
			printarray(arrayx,sizex,sizey);

	}
}
}

The way you have defined the array:

const int sizex=20;
const int sizey=2;
int score[sizex][sizey];

it is obvious that the first dimension is the main dimension, the second dimension is just storing the two values in each element. So the first dimension is varying from 0 to sizex-1.

Now in your if statement, you are fixing the first subscript to 0 and you are using the variable in the second subscript running from 0 to sizex-1. So first thing to fix is to swap your subscripts.

Secondly in the body of the if statement you are swapping the 0th element. You need to write the similar 3 statements to swap the 1st element as well. (Same three lines but instead of using 0 in the SECOND subscript use 1).

Hopefully this should take care of the problem.

so like this?

void descend(int arrayx[sizex][sizey], int sizex, int sizey)
{int temp;
for (int i=0; i<sizex; i++)
	{for (int j=0; j<sizex-1; j++)
		{if (arrayx[0][j]>arrayx[0][i])
		
		{
			{temp=arrayx[j][1];
			arrayx[j][1]=arrayx[i][1];
			arrayx[i][1]=temp;
			}
		}		cout<<"\n";
			printarray(arrayx,sizex,sizey);

	}
}
}

Well not quite. More like this:

if (arrayx[j][0]>arrayx[i][0])  // The subscript 0 goes in the second dimension
		{
                        // Swap both of the elements
			temp=arrayx[j][0];
			arrayx[j][0]=arrayx[i][0];
			arrayx[i][0]=temp;

			temp=arrayx[j][1];
			arrayx[j][1]=arrayx[i][1];
			arrayx[i][1]=temp;
		}

It definately closer! Thank for that but its still not sorting descending. It may be something wrong with the print array. Here is the revised code so far and the output.

void printarray(int arrayx[sizex][sizey], int sizex, int sizey)
{
	for(int i=0; i<=sizex-1; i++)
	{for (int j=0; j<sizey-1; j++)
	cout<<"Sorted 2nd: "<<arrayx[i][1]<<"\t";
	cout<<"\n";
	}
}

void descend(int arrayx[sizex][sizey], int sizex, int sizey)
{int temp;
for (int i=0; i<sizex-1; i++)
	{for (int j=0; j<sizex; j++)
		{if (arrayx[j][0]>arrayx[i][0])
		
		{
			{temp=arrayx[j][0];
			arrayx[j][0]=arrayx[i][0];
			arrayx[i][0]=temp;

			temp=arrayx[j][1];
			arrayx[j][1]=arrayx[i][1];
			arrayx[i][1]=temp;

			}
		}		cout<<"\n";
			printarray(arrayx,sizex,sizey);

	}
}
}

Output:

Sorted 2nd: 37
Sorted 2nd: 58
Sorted 2nd: 62
Sorted 2nd: 44
Sorted 2nd: 41
Sorted 2nd: 70
Sorted 2nd: 30
Sorted 2nd: 59
Sorted 2nd: 47
Sorted 2nd: 95
Sorted 2nd: 65
Sorted 2nd: 66
Sorted 2nd: 73
Sorted 2nd: 63
Sorted 2nd: 45
Sorted 2nd: 30
Sorted 2nd: 59
Sorted 2nd: 70
Sorted 2nd: 79
Sorted 2nd: 55
Press any key to continue . . .

Output:

Sorted 2nd: 37
Sorted 2nd: 58
Sorted 2nd: 62
Sorted 2nd: 44
Sorted 2nd: 41
Sorted 2nd: 70
Sorted 2nd: 30
Sorted 2nd: 59
Sorted 2nd: 47
Sorted 2nd: 95
Sorted 2nd: 65
Sorted 2nd: 66
Sorted 2nd: 73
Sorted 2nd: 63
Sorted 2nd: 45
Sorted 2nd: 30
Sorted 2nd: 59
Sorted 2nd: 70
Sorted 2nd: 79
Sorted 2nd: 55
Press any key to continue . . .

I am surprised. Something is weird about this output. From your printarray function you are printing 40 times, but your output is 20 times. Also you are printing each element twice.

But ignoring that, you are printing the element corresponding to the subscript 1, whereas you have sorted the elements corresponding to the subscript 0. So change your cout to print arrayx[0] and remove your inner loop j.

I only posted the output of the last section so thats why it was weird. But I hope you understand that I am trying to sort the items in the second column of the array which would be column 1. print array[0] prints the wrong column.

The array is not defined in the function definition correctly.
There is no way the compiler knows how big the array is using void descend(int arrayx[sizex][sizey], int sizex, int sizey) sizey in the array definition needs to be an explicit number, not a variable.

I only posted the output of the last section so thats why it was weird. But I hope you understand that I am trying to sort the items in the second column of the array which would be column 1. print array[0] prints the wrong column.

Oops! Ok then you need to change your if condition in the descend() function. Use the subscript 1 instead of 0 in there: if (arrayx[j][1]>arrayx[i][1]) Everything else in descend() remains the same.

commented: thanks +1

Ok, It is working now using the following code:

code removed. Thanks guys!

void descend(int arrayx[sizex][sizey], int sizex, int sizey)
{int temp;
for (int i=0; i<sizex-1; i++)
	{for (int j=0; j<sizex; j++)
		{if (arrayx[j][1]<arrayx[i][1])
		
		{
			{temp=arrayx[j][0];
			arrayx[j][0]=arrayx[i][0];
			arrayx[i][0]=temp;

			temp=arrayx[j][1];
			arrayx[j][1]=arrayx[i][1];
			arrayx[i][1]=temp;

			}
		}		cout<<"\n";
			printarray(arrayx,sizex,sizey);

	}
}
}

Because your condition is i<sizex-1 (see in red above) it used to be and is supposed to be i<sizex. Why did you change that? :)

Because your condition is i<sizex-1 (see in red above) it used to be and is supposed to be i<sizex. Why did you change that? :)

Thanks for all the help. Solved!

Could a mod remove the code in the first post if possible. I want my code to be exclusive to me. If not, completely understand.

This program shows how to do a simple selection sort (almost the same as a bubble sort), keying on one column of a 2D array.

/* shows how to a 2D array, using one column (the last column) as the key.
*/

#include <stdio.h>

void printArray(const int a[3][4]);

int main()

{
  int pass;
  int i;
  int j, k;
  int hold;
  int array1[3][4]= {5,34,78,112,1,56,86,142,3,45,29,74};

  printf("Employee number   1st Quarter Sales   2nd Quarters Sales   Total Sales\n");

  printArray(array1);
  
  for (i = 0; i < 3; i++) {
    for(j = i+1; j < 3 ; j++) {
      if(array1[i][3] < array1[j][3]) {  //swap all cols in the row
        hold= array1[i][3];                  //this is our key column
        array1[i][3]=array1[j][3];
        array1[j][3] = hold;

        hold = array1[i][2];
        array1[i][2] = array1[j][2];
        array1[j][2] = hold;

        hold = array1[i][1];
        array1[i][1] = array1[j][1];
        array1[j][1] = hold;

        hold = array1[i][0];
        array1[i][0] = array1[j][0];
        array1[j][0] = hold;

      }
    }
  }
  printArray(array1);

     // system("PAUSE");

  printf("\n\n\t\t\t     press enter when ready");
  getchar();
  return 0;
}

void printArray(const int a[3][4])

{
  int i;
  int j;
  printf("\n\n");
  for (i =0; i < 3; i++) {
    for (j=0; j < 4; j++) {
      printf("%9d  ", a[i][j]);
    }
    printf("\n");
  }
}

This is not the best way to do this, however. (Imagine doing this with a 2D array with 100 columns <eek!>.) The best way is to NOT move any of the data, and swap values in an index (or pointer) auxiliary array, instead. That allows the data to be printed out in sorted order, but no data will have to be moved.

If you don't need to keep the columns in the same row originally, together, then just remove the swaps on the non-key columns.

commented: Thank you for your time. +1

Thanks for all the help. Solved!

You are welcome.

Here is a tip. To make it more effecient you don't really need to run the inner loop j in descend() function all the way through sizex-1 (that is j < sizex ) because you have already pushed the smallest element all the way to the end. So you could change that condition to j < (sizex - i) to make your code more efficient, not that it matters in a 20 element array :)

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.