functions - matrix

Hello everyone!
I have some problem with this question .
The question is to program funciton which gets matrix which is not sorted and all i need to do is to sort it with the bubble sort.
I got this error :
Error C2440 'initializing':
cannot convert from 'initializer list' to 'int **'.
To this 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
using namespace std;

const int SIZEQ4 = 4;
const int ROWS = 4;
void changingTheNumbersOrderFromTheSmallestToTheBiggestQ4(int** matrixQ4,int rows, int size);

void main()
{

	int** matrixQ4 = {3,4,1,2,7,5,6,9, 8,10,13,12,11,14,15,16};
	int i, j;
	changingTheNumbersOrderFromTheSmallestToTheBiggestQ4(matrixQ4, ROWS, SIZEQ4);


	for (int i = 0; i < SIZEQ4; i++)
	{
		for (j = 0; j < SIZEQ4; j++)
		{
			cout << matrixQ4[i][j] << " ";

		}
		cout << endl;
	}

	system("pause");
}

void changingTheNumbersOrderFromTheSmallestToTheBiggestQ4(int** matrixQ4, int rows, int size)
{
	int i, j,k;
	int temp;
	for (k = size*rows - 1; k > 0; k--)
	{
		for (i = 0; i < size*rows - 1; i++)
		{
			for (j = 0; j < i; j++)
			{
				if (matrixQ4[i][j] > matrixQ4[i][j + 1])
				{
					temp = matrixQ4[i][j];
					matrixQ4[i][j] = matrixQ4[i][j + 1];
					matrixQ4[i][j + 1] = temp;

				}
			}
		}
	}
}



Thank you everyone !
int** matrixQ4 = {3,4,1,2,7,5,6,9, 8,10,13,12,11,14,15,16};

On the right, an initialiser list of int values.

On the left, a pointer to a pointer to an int.

Trying to make a pointer to a pointer to an int equal all those numbers makes no sense. What are you trying to do? What do you want matrixQ4 to be? Is it meant to be a 2D array?

If so, how is the compiler supposed to know that? How could it take those numbers are arrange them into a 2D array of the right size for you?

Last edited on
thank you very much for the quick answer .

the thing im trying to do is to sort those arrays (3,4,1,2,7,5,6,9, 8,10,13,12,11,14,15,16) with bubble sort so eventually i will have a sorted matrix .

i thougt use pointers, so the function will get the values and treat them like arrays ..

Here is how to assemble a 2D array of the nature I think you want:

1
2
3
4
5
6
7
int matrixQ4 [4][4] =
{
   { 3,4,1,2 }, 
   { 7,5,6,9 }, 
   { 8,10,13,12 },
   { 11,14,15,16 }
};
thank you very much again ! got it !!

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
using namespace std;

const int SIZEQ4 = 4;
const int ROWS = 4;
void changingTheNumbersOrderFromTheSmallestToTheBiggestQ4(int matrixQ4[][SIZEQ4],int rows, int size);

void main()
{

	int matrixQ4[SIZEQ4][SIZEQ4] = { { 3,4,1,2 },{ 7, 5, 6, 9 },{ 8, 10, 13, 12, },{ 11, 14, 15, 16 } };

	int i, j;
	changingTheNumbersOrderFromTheSmallestToTheBiggestQ4(matrixQ4, ROWS, SIZEQ4);


	for (int i = 0; i < SIZEQ4; i++)
	{
		for (j = 0; j < SIZEQ4; j++)
		{
			cout << matrixQ4[i][j] << " ";

		}
		cout << endl;
	}

	system("pause");
}

void changingTheNumbersOrderFromTheSmallestToTheBiggestQ4(int matrixQ4[][SIZEQ4], int rows, int size)
{
	int i, j,k;
	int temp;
	
		for (i = 0; i < size*rows -1 ; i++)
		{
			for (k = 0; k < size - 1; k++)
			{
				for (j = 0; j <size -1; j++)
				{
					if (matrixQ4[k][j] > matrixQ4[k][j + 1])
					{
						temp = matrixQ4[k][j];
						matrixQ4[k][j] = matrixQ4[k][j + 1];
						matrixQ4[k][j + 1] = temp;

					}
				}
			}
		}
	}


Last edited on
but still i dont think i did exactly what i was asked for
i need to sort it with the bubble sort

so .. i guess i dont have the right code.

help ?
Given the input 2D array:

1
2
3
4
5
6
7
int matrixQ4 [4][4] =
{
   { 3,4,1,2 }, 
   { 7,5,6,9 }, 
   { 8,10,13,12 },
   { 11,14,15,16 }
};


what's it meant to look like at the end?
i think i should have had the optimiztion of j<i
Topic archived. No new replies allowed.