Trouble Sorting a 2-D Array

So I am having trouble implementing a bubble sort for my 2-D matrix. I'll post the whole code below but before I do Ill tell you the general purpose of it. The assignment is supposed to have an options menu from which multiple things can be chosen in relation to a predetermined matrix.

My problem, literally my only problem, is the ascending and descending sorting of my array. Long story short, my teacher is quite happy with assigning problems that require no explanation so I have been scouring google. I feel as if I am missing certain things and I will show you what I have so far.

This is the sort function I am trying to create
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void bubbleSort(int givenArray[5][4])
{			
			int temp, n, j;
			bool swapped = true;
			while(swapped) {
				swapped = false;
				j++;

					for(int i = 0; i < n - j; i++) {
						if (givenArray[i] > givenArray[i + 1]) {
						temp = givenArray[i];
						givenArray[i] = givenArray[i+1];
						givenArray[i+1] = temp;
						swapped = true;
						}
					}
			}
}

and here is the matrix I am having so much trouble sorting
1
2
3
4
5
6
7
8

#define HEIGHT 5
#define WIDTH 4
int givenArray[5][4] = { { 34,  56, 79, 12 }, 
			     { 25, 37, 41, 18  },
				 { 59, 29,  38, 47 },
				 { 55,  11, 88, 34 },
				 { 45, 19,  34, 66 } };


Any help you guys could give me is great. I really thought I was doing the right thing but the above matrix continues to give me errors. The two I get are this,
 
 cannot convert from 'int [4]' to 'int' //in relation to the givenArray[i] = givenArray[i+1] 

and
 
left operand must be l-value //in relation to the same thing as the other error 


Thanks in advance! I hope I was clear with my problems and what not.
Your "givenArray" is a 2D array, but in your loop, you're treating it as a 1D array. You are trying to compare:

{ 34, 56, 79, 12 } to {25, 37, 41, 18}

and the computer can't do that.

Do you have to have it as a 2D array? Your loop would work if you turned it into a 1D array.

EDIT: Oops, did not look at it closely enough.

You also need to give some initial values to n and j. Right now, they're random numbers. I don't know why you're using nested loops when you're not really using the j at all.
Last edited on
It is not entirely clear what you need to sort. Do you need to sort each row, only the rows as a whole, or both? Have you been given an example of what a sorted "matrix" should look like?

Regarding your specific syntax error, the issue is that the comparison operators require a "simple" numerical data type, but you are trying to compare an entire array.

As defined, givenArray is an array of arrays, so the expression givenArray[i]
evaluates to the ith row in the matrix, which is itself an array. For example,
givenArray[0] evaluates to the address of the row {34, 56, 79, 12}. This can not be used with the > operator.

You should also look closer at the expression i < n - j in your for statement.
You have not initialized the value of n or j, so at best you can expect them to be equal to 0 and 1 when you reach the for loop, in which case that expression always evaluates to true, and your loop will never execute. At worst, their values will be garbage, in which case you can't predict what will take place.

I would provide broader advice, but again, I am not certain what your goal is here.
It makes a lot more sense why I'm running into such errors.

I have to keep this array as a 2-D array and the goal is to sort the entire thing from top-left to bottom-right in both ascending and descending order. If that's not possible then row by row will do as well.

Really the only reason my sort loop looks the way it does is because I tried to make it look similar to the other ones I have seen. The problem is, since most are written with the example being just a 1-D array it is difficult to see where the changes would be for a 2-D array.
1
2
3
j++;

for(int i = 0; i < n - j; i++)


It's a little unclear as to what you want to do here. n isn't initialized, so it's probably set to 0 by default, and with that, I don't see the body of the loop ever getting executed.

for (int i = 0; 0 < 0 - 1; i++) // 0 is not less than -1, so don't execute the loop

Perhaps you could clear things up a bit for the readers?
Have you tried using the debugger to watch your variables?

That is the best way to find problems.

If it doesn't compile, post the compiler output.

Cheers

TheIdeasMan
closed account (D80DSL3A)
It seems that your main problem lies with the comparison of a pointer to an integer. This: givenArray[i] is a pointer to an array of 4 ints.
You need 2 indexes to get to an integer value.

I see 2 methods for you. The 1st was suggested by Moschops in your other thread: http://www.cplusplus.com/forum/beginner/73885/#msg395075
Use an int* to access the elements as if you were dealing with a 1d array.

Partial code:
1
2
3
4
5
6
7
8
9
10
11
12
13
int* pInt = &givenArray[0][0];// use a regular pointer to point to the 1st element
    do
    {
       ...
        for( int i=0; i<high; ++i )
        {
            if( pInt[i] > pInt[i+1] )
            {
               // swap the elements
            }
        }
        --high;
    }while( swapped );


A 2nd method is to use the / and % operators to extract the row and column indexes from a single index running from 0 to 19:
1
2
3
4
5
6
7
8
9
10
11
12
do
    {
       ...
        for( int i=0; i<high; ++i )
        {
            if( givenArray[i/4][i%4] > givenArray[(i+1)/4][(i+1)%4] )
            {
               // swap the elements
            }
        }
        --high;
    }while( swapped );

Using either method I get this for the ascending sort result:

11 12 18 19
25 29 34 34
34 37 38 41
45 47 55 56
59 66 79 88

Hopefully, that is what you are seeking.

EDIT: Changed < to > in 1st code example.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
do
    {
       ...
        for( int i=0; i<high; ++i )
        {
            if( givenArray[i/4][i%4] > givenArray[(i+1)/4][(i+1)%4] )
            {
               // swap the elements
            }
        }
        --high;
    }while( swapped );


This is an extremely clever alternative ( [i / 4][i % 4] ) to using nested for loops, thanks for that. :)
Topic archived. No new replies allowed.