Can someone explain why my code isnt working?

So im trying to write some code for one of my classes and I cant figure out whats wrong with it, its suppose to sort a single row of a matrix of type Char. Its suppose to use pointers in order to do this.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void sortRow(char(*array)[arraySize], int row) {

	char *temp = array[row];

	for (int i = 1; i < arraySize; i++) {
		int j = i;
		while (j > 0 && *(array[row] +(j-1)) > *(array[row] + j)) {

			*(temp) = *(array[row]+j);
			*(array[row] + (j-1)) = *(array[row] + j);
				*(array[row] +(j-1)) = *(temp);



			j--;

		}



	}

};



this is the matrix that its failing to sort,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
TEST_CASE("Testing sortRow() - 2"){
    const unsigned int rows = 5;
    const unsigned int columns = 5;
    
    char first[rows][columns] = {
        {'a', 'q', 'a', 'z', 'p'},
        {'b', 'a', 'b', 'a', 'c'},
        {'a', 'b', 'c', 'c', 'd'},
        {'a', 'a', 'a', 'a', 'a'},
        {'a', 'b', 'a', 'b', 'z'}
    };
    
    char firstSorted[rows][columns] = {
        {'a', 'a', 'p', 'q', 'z'},
        {'a', 'a', 'b', 'b', 'c'},
        {'a', 'b', 'c', 'c', 'd'},
        {'a', 'a', 'a', 'a', 'a'},
        {'a', 'a', 'b', 'b', 'z'}
    };
    
    for (int i = 0; i < 5; i++){
        sortRow(first, i);
    }
    
    for (int i = 0; i < rows; i++){
        for (int j = 0; j < columns; j++){
            REQUIRE(first[i][j] == firstSorted[i][j]);
        }
    }
}

Last edited on
I would say that your function sortRow() should look more like void sortRow(char *array, int rowSize)

You only need to pass the row into the function not the complete array.

Its suppose to use pointers in order to do this.

What exactly does this mean? Does it mean you can't use array notation in the function (using pointer addition instead) or does just passing by pointer fill the requirements?

yes I cant use array notation, must use pointers. The function argument was pre-made and I dont think they want me to adjust it. Its a pointer homework so its supposed to be about learning pointers.
Last edited on
In your sort function shouldn't temp be a simple char, not a pointer?


I managed to figure it out, I was trying to use double pointers instead of assigning one part of the matrix to another array and then using a single pointer on that array.
this is the correct code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void sortRow(char(*array)[arraySize], int row) {

	char* temp = array[row];
	char temp2 = '0';

	for (int i = 0; i < arraySize; i++) {
		int j = i;
		

		while (j > 0 && *(temp + (j -1)) > *(temp +j)) {

			temp2 = *(temp + (j-1));
			*(temp + (j - 1)) = *(temp + j);
			*(temp + j) = temp2;
			
			


			j--;

		}

		array[row][i] = *(temp + i);

	}
	
	
};
this is the correct code

Does it work as expected?

I thought you couldn't use array notation?

Yeah it works and its not using array notion its using pointer notion, its the pointer of the array not the array itself that im manipulating.
This: array[row][i] is array notation.

Registered users can post here. Sign in or register to post.