Rotate matrix without []
up vote
-2
down vote
favorite
I need to rotate a matrix and call the funcion without using ,
I can't even think on a solution.
void _90DegClockwise(int *pS, int row, int col) {
for (int i = 0; i < row; ++i)
{
for (int j = i + 1; j < col; j++) {
int temp;
temp = (int)(pS);
*((int*)((pS + i) + j)) = (int*)((pS + j) + i);
(int*)((pS + j) + i) = temp;
}
}
}
I don't know how to insert a value to matrix or how to swap
c arrays pointers
New contributor
add a comment |
up vote
-2
down vote
favorite
I need to rotate a matrix and call the funcion without using ,
I can't even think on a solution.
void _90DegClockwise(int *pS, int row, int col) {
for (int i = 0; i < row; ++i)
{
for (int j = i + 1; j < col; j++) {
int temp;
temp = (int)(pS);
*((int*)((pS + i) + j)) = (int*)((pS + j) + i);
(int*)((pS + j) + i) = temp;
}
}
}
I don't know how to insert a value to matrix or how to swap
c arrays pointers
New contributor
how is matrix rotation related to?
– phuclv
yesterday
I can't swap elements by using: a[i][j] = temp ,etc.
– Itay Zemah
yesterday
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I need to rotate a matrix and call the funcion without using ,
I can't even think on a solution.
void _90DegClockwise(int *pS, int row, int col) {
for (int i = 0; i < row; ++i)
{
for (int j = i + 1; j < col; j++) {
int temp;
temp = (int)(pS);
*((int*)((pS + i) + j)) = (int*)((pS + j) + i);
(int*)((pS + j) + i) = temp;
}
}
}
I don't know how to insert a value to matrix or how to swap
c arrays pointers
New contributor
I need to rotate a matrix and call the funcion without using ,
I can't even think on a solution.
void _90DegClockwise(int *pS, int row, int col) {
for (int i = 0; i < row; ++i)
{
for (int j = i + 1; j < col; j++) {
int temp;
temp = (int)(pS);
*((int*)((pS + i) + j)) = (int*)((pS + j) + i);
(int*)((pS + j) + i) = temp;
}
}
}
I don't know how to insert a value to matrix or how to swap
c arrays pointers
c arrays pointers
New contributor
New contributor
edited yesterday
phuclv
14.1k846203
14.1k846203
New contributor
asked yesterday
Itay Zemah
31
31
New contributor
New contributor
how is matrix rotation related to?
– phuclv
yesterday
I can't swap elements by using: a[i][j] = temp ,etc.
– Itay Zemah
yesterday
add a comment |
how is matrix rotation related to?
– phuclv
yesterday
I can't swap elements by using: a[i][j] = temp ,etc.
– Itay Zemah
yesterday
how is matrix rotation related to
?– phuclv
yesterday
how is matrix rotation related to
?– phuclv
yesterday
I can't swap elements by using: a[i][j] = temp ,etc.
– Itay Zemah
yesterday
I can't swap elements by using: a[i][j] = temp ,etc.
– Itay Zemah
yesterday
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
You're mixing up the pointer and the dereferenced value in the 3 lines
In the first line temp = (int)(pS);
you're assigning the pointer value to temp instead of the value that the pointer points to. Similarly you're also assigning the pointer value to the memory location like this *((int*)((pS + i) + j)) = (int*)((pS + j) + i);
which makes no sense.
Then in the last line (int*)((pS + j) + i) = temp;
doesn't work because you're a value to an address instead of a memory location
Remember to use *
to dereference a pointer to get the variable, i.e. the memory location the pointer points to. In C a[b]
is equivalent to *(a + b)
so just replace all those occurrences. I don't know why you do it "correctly" with *((int*)((pS + i) + j))
but didn't apply that to others
Even then you're calculating the index incorrectly. *((int*)((pS + i) + j))
is just pS[i + j] which is not the correct item you want. If you pass the 2D array as a 1D array you need to calculate the real index like this pS[i + j*width]
So to do
temp = pS[i + j*width];
pS[i + j*width] = pS[j + i*width];
pS[j + i*width] = temp;
just change it to
temp = *(pS + i + j*width);
*(pS + i + j*width) = *(pS + j + i*width);
*(pS + j + i*width) = temp;
You must enable all compiler warnings. They're very helpful and help you solve most of the above problem
First, thank for the really helpful explanation! Second, I work in Ubunto enviroment, in eclips. How can I enable the warning?
– Itay Zemah
yesterday
I don't know. If you do a manual compilation then just use-Wall -Wextra
Enabling flags (Wall, pedantic) for C/C++ compilation within Eclipse
– phuclv
yesterday
add a comment |
up vote
1
down vote
To solve this, you need to understand how arrays work in C.
Lets say you have a 3 * 3 matrix, that is declared like this:
int matrix[3][3];
While you imagine this to be a square like this:
+---+---+---+
| 0 | 1 | 2 |
+---+---+---+
| 3 | 4 | 5 |
+---+---+---+
| 6 | 7 | 8 |
+---+---+---+
For the computer it is a consecutive "line" in memory looking like this:
+---+---+---+---+---+---+---+---+---+---+
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
+---+---+---+---+---+---+---+---+---+---+
^
|____matrix
And variable matrix
holds the address of the first cell
So, if you want to access any cell without using the operator, you need to calculate the address of that cell.
Lets do this for the middle cell:
matrix[1][1];
This is the second cell of the second row, so you need to add the width of the first row to the start of the matrix, and then add one more cell from the start of the second row. And you want to derefernce the address to get to the value like so:
*(matrix + (3 * 1) + 1);
What if we wanted the middle cell of the third row? Same thing, but add width of two rows:
*(matrix + (3 * 2) + 1);
To sum up: if you want to access cell x in row y of the matrix you would calculate its address like this:
*(matrix + (with * y) + x);
New contributor
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You're mixing up the pointer and the dereferenced value in the 3 lines
In the first line temp = (int)(pS);
you're assigning the pointer value to temp instead of the value that the pointer points to. Similarly you're also assigning the pointer value to the memory location like this *((int*)((pS + i) + j)) = (int*)((pS + j) + i);
which makes no sense.
Then in the last line (int*)((pS + j) + i) = temp;
doesn't work because you're a value to an address instead of a memory location
Remember to use *
to dereference a pointer to get the variable, i.e. the memory location the pointer points to. In C a[b]
is equivalent to *(a + b)
so just replace all those occurrences. I don't know why you do it "correctly" with *((int*)((pS + i) + j))
but didn't apply that to others
Even then you're calculating the index incorrectly. *((int*)((pS + i) + j))
is just pS[i + j] which is not the correct item you want. If you pass the 2D array as a 1D array you need to calculate the real index like this pS[i + j*width]
So to do
temp = pS[i + j*width];
pS[i + j*width] = pS[j + i*width];
pS[j + i*width] = temp;
just change it to
temp = *(pS + i + j*width);
*(pS + i + j*width) = *(pS + j + i*width);
*(pS + j + i*width) = temp;
You must enable all compiler warnings. They're very helpful and help you solve most of the above problem
First, thank for the really helpful explanation! Second, I work in Ubunto enviroment, in eclips. How can I enable the warning?
– Itay Zemah
yesterday
I don't know. If you do a manual compilation then just use-Wall -Wextra
Enabling flags (Wall, pedantic) for C/C++ compilation within Eclipse
– phuclv
yesterday
add a comment |
up vote
0
down vote
accepted
You're mixing up the pointer and the dereferenced value in the 3 lines
In the first line temp = (int)(pS);
you're assigning the pointer value to temp instead of the value that the pointer points to. Similarly you're also assigning the pointer value to the memory location like this *((int*)((pS + i) + j)) = (int*)((pS + j) + i);
which makes no sense.
Then in the last line (int*)((pS + j) + i) = temp;
doesn't work because you're a value to an address instead of a memory location
Remember to use *
to dereference a pointer to get the variable, i.e. the memory location the pointer points to. In C a[b]
is equivalent to *(a + b)
so just replace all those occurrences. I don't know why you do it "correctly" with *((int*)((pS + i) + j))
but didn't apply that to others
Even then you're calculating the index incorrectly. *((int*)((pS + i) + j))
is just pS[i + j] which is not the correct item you want. If you pass the 2D array as a 1D array you need to calculate the real index like this pS[i + j*width]
So to do
temp = pS[i + j*width];
pS[i + j*width] = pS[j + i*width];
pS[j + i*width] = temp;
just change it to
temp = *(pS + i + j*width);
*(pS + i + j*width) = *(pS + j + i*width);
*(pS + j + i*width) = temp;
You must enable all compiler warnings. They're very helpful and help you solve most of the above problem
First, thank for the really helpful explanation! Second, I work in Ubunto enviroment, in eclips. How can I enable the warning?
– Itay Zemah
yesterday
I don't know. If you do a manual compilation then just use-Wall -Wextra
Enabling flags (Wall, pedantic) for C/C++ compilation within Eclipse
– phuclv
yesterday
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You're mixing up the pointer and the dereferenced value in the 3 lines
In the first line temp = (int)(pS);
you're assigning the pointer value to temp instead of the value that the pointer points to. Similarly you're also assigning the pointer value to the memory location like this *((int*)((pS + i) + j)) = (int*)((pS + j) + i);
which makes no sense.
Then in the last line (int*)((pS + j) + i) = temp;
doesn't work because you're a value to an address instead of a memory location
Remember to use *
to dereference a pointer to get the variable, i.e. the memory location the pointer points to. In C a[b]
is equivalent to *(a + b)
so just replace all those occurrences. I don't know why you do it "correctly" with *((int*)((pS + i) + j))
but didn't apply that to others
Even then you're calculating the index incorrectly. *((int*)((pS + i) + j))
is just pS[i + j] which is not the correct item you want. If you pass the 2D array as a 1D array you need to calculate the real index like this pS[i + j*width]
So to do
temp = pS[i + j*width];
pS[i + j*width] = pS[j + i*width];
pS[j + i*width] = temp;
just change it to
temp = *(pS + i + j*width);
*(pS + i + j*width) = *(pS + j + i*width);
*(pS + j + i*width) = temp;
You must enable all compiler warnings. They're very helpful and help you solve most of the above problem
You're mixing up the pointer and the dereferenced value in the 3 lines
In the first line temp = (int)(pS);
you're assigning the pointer value to temp instead of the value that the pointer points to. Similarly you're also assigning the pointer value to the memory location like this *((int*)((pS + i) + j)) = (int*)((pS + j) + i);
which makes no sense.
Then in the last line (int*)((pS + j) + i) = temp;
doesn't work because you're a value to an address instead of a memory location
Remember to use *
to dereference a pointer to get the variable, i.e. the memory location the pointer points to. In C a[b]
is equivalent to *(a + b)
so just replace all those occurrences. I don't know why you do it "correctly" with *((int*)((pS + i) + j))
but didn't apply that to others
Even then you're calculating the index incorrectly. *((int*)((pS + i) + j))
is just pS[i + j] which is not the correct item you want. If you pass the 2D array as a 1D array you need to calculate the real index like this pS[i + j*width]
So to do
temp = pS[i + j*width];
pS[i + j*width] = pS[j + i*width];
pS[j + i*width] = temp;
just change it to
temp = *(pS + i + j*width);
*(pS + i + j*width) = *(pS + j + i*width);
*(pS + j + i*width) = temp;
You must enable all compiler warnings. They're very helpful and help you solve most of the above problem
answered yesterday
phuclv
14.1k846203
14.1k846203
First, thank for the really helpful explanation! Second, I work in Ubunto enviroment, in eclips. How can I enable the warning?
– Itay Zemah
yesterday
I don't know. If you do a manual compilation then just use-Wall -Wextra
Enabling flags (Wall, pedantic) for C/C++ compilation within Eclipse
– phuclv
yesterday
add a comment |
First, thank for the really helpful explanation! Second, I work in Ubunto enviroment, in eclips. How can I enable the warning?
– Itay Zemah
yesterday
I don't know. If you do a manual compilation then just use-Wall -Wextra
Enabling flags (Wall, pedantic) for C/C++ compilation within Eclipse
– phuclv
yesterday
First, thank for the really helpful explanation! Second, I work in Ubunto enviroment, in eclips. How can I enable the warning?
– Itay Zemah
yesterday
First, thank for the really helpful explanation! Second, I work in Ubunto enviroment, in eclips. How can I enable the warning?
– Itay Zemah
yesterday
I don't know. If you do a manual compilation then just use
-Wall -Wextra
Enabling flags (Wall, pedantic) for C/C++ compilation within Eclipse– phuclv
yesterday
I don't know. If you do a manual compilation then just use
-Wall -Wextra
Enabling flags (Wall, pedantic) for C/C++ compilation within Eclipse– phuclv
yesterday
add a comment |
up vote
1
down vote
To solve this, you need to understand how arrays work in C.
Lets say you have a 3 * 3 matrix, that is declared like this:
int matrix[3][3];
While you imagine this to be a square like this:
+---+---+---+
| 0 | 1 | 2 |
+---+---+---+
| 3 | 4 | 5 |
+---+---+---+
| 6 | 7 | 8 |
+---+---+---+
For the computer it is a consecutive "line" in memory looking like this:
+---+---+---+---+---+---+---+---+---+---+
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
+---+---+---+---+---+---+---+---+---+---+
^
|____matrix
And variable matrix
holds the address of the first cell
So, if you want to access any cell without using the operator, you need to calculate the address of that cell.
Lets do this for the middle cell:
matrix[1][1];
This is the second cell of the second row, so you need to add the width of the first row to the start of the matrix, and then add one more cell from the start of the second row. And you want to derefernce the address to get to the value like so:
*(matrix + (3 * 1) + 1);
What if we wanted the middle cell of the third row? Same thing, but add width of two rows:
*(matrix + (3 * 2) + 1);
To sum up: if you want to access cell x in row y of the matrix you would calculate its address like this:
*(matrix + (with * y) + x);
New contributor
add a comment |
up vote
1
down vote
To solve this, you need to understand how arrays work in C.
Lets say you have a 3 * 3 matrix, that is declared like this:
int matrix[3][3];
While you imagine this to be a square like this:
+---+---+---+
| 0 | 1 | 2 |
+---+---+---+
| 3 | 4 | 5 |
+---+---+---+
| 6 | 7 | 8 |
+---+---+---+
For the computer it is a consecutive "line" in memory looking like this:
+---+---+---+---+---+---+---+---+---+---+
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
+---+---+---+---+---+---+---+---+---+---+
^
|____matrix
And variable matrix
holds the address of the first cell
So, if you want to access any cell without using the operator, you need to calculate the address of that cell.
Lets do this for the middle cell:
matrix[1][1];
This is the second cell of the second row, so you need to add the width of the first row to the start of the matrix, and then add one more cell from the start of the second row. And you want to derefernce the address to get to the value like so:
*(matrix + (3 * 1) + 1);
What if we wanted the middle cell of the third row? Same thing, but add width of two rows:
*(matrix + (3 * 2) + 1);
To sum up: if you want to access cell x in row y of the matrix you would calculate its address like this:
*(matrix + (with * y) + x);
New contributor
add a comment |
up vote
1
down vote
up vote
1
down vote
To solve this, you need to understand how arrays work in C.
Lets say you have a 3 * 3 matrix, that is declared like this:
int matrix[3][3];
While you imagine this to be a square like this:
+---+---+---+
| 0 | 1 | 2 |
+---+---+---+
| 3 | 4 | 5 |
+---+---+---+
| 6 | 7 | 8 |
+---+---+---+
For the computer it is a consecutive "line" in memory looking like this:
+---+---+---+---+---+---+---+---+---+---+
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
+---+---+---+---+---+---+---+---+---+---+
^
|____matrix
And variable matrix
holds the address of the first cell
So, if you want to access any cell without using the operator, you need to calculate the address of that cell.
Lets do this for the middle cell:
matrix[1][1];
This is the second cell of the second row, so you need to add the width of the first row to the start of the matrix, and then add one more cell from the start of the second row. And you want to derefernce the address to get to the value like so:
*(matrix + (3 * 1) + 1);
What if we wanted the middle cell of the third row? Same thing, but add width of two rows:
*(matrix + (3 * 2) + 1);
To sum up: if you want to access cell x in row y of the matrix you would calculate its address like this:
*(matrix + (with * y) + x);
New contributor
To solve this, you need to understand how arrays work in C.
Lets say you have a 3 * 3 matrix, that is declared like this:
int matrix[3][3];
While you imagine this to be a square like this:
+---+---+---+
| 0 | 1 | 2 |
+---+---+---+
| 3 | 4 | 5 |
+---+---+---+
| 6 | 7 | 8 |
+---+---+---+
For the computer it is a consecutive "line" in memory looking like this:
+---+---+---+---+---+---+---+---+---+---+
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
+---+---+---+---+---+---+---+---+---+---+
^
|____matrix
And variable matrix
holds the address of the first cell
So, if you want to access any cell without using the operator, you need to calculate the address of that cell.
Lets do this for the middle cell:
matrix[1][1];
This is the second cell of the second row, so you need to add the width of the first row to the start of the matrix, and then add one more cell from the start of the second row. And you want to derefernce the address to get to the value like so:
*(matrix + (3 * 1) + 1);
What if we wanted the middle cell of the third row? Same thing, but add width of two rows:
*(matrix + (3 * 2) + 1);
To sum up: if you want to access cell x in row y of the matrix you would calculate its address like this:
*(matrix + (with * y) + x);
New contributor
New contributor
answered yesterday
Lev M.
1265
1265
New contributor
New contributor
add a comment |
add a comment |
Itay Zemah is a new contributor. Be nice, and check out our Code of Conduct.
Itay Zemah is a new contributor. Be nice, and check out our Code of Conduct.
Itay Zemah is a new contributor. Be nice, and check out our Code of Conduct.
Itay Zemah is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238553%2frotate-matrix-without%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
how is matrix rotation related to
?
– phuclv
yesterday
I can't swap elements by using: a[i][j] = temp ,etc.
– Itay Zemah
yesterday