PDA

View Full Version : School assignment, arrays/matrices, help please =( (again)

VanillaCone
November 6th, 2010, 08:30 PM
i need to transpose a matrix, my function returns my matrix as

0 0 0 0
0 0 0 0
0 0 0 0

when the inputted matrix is
1 2 3 4
1 2 3 4
1 2 3 4

void transpose (int ***A, int m, int n)

{
int i;
int j;
int **matrix = createMatrix(m, n);

while (j<n)
{
for (i = 0; i < m; i++)
{
matrix[j][i]= *A[i][j];
}
j++;
}

freeMatrix(*A, m, n);

*A=matrix;

return;
}

also it wont ALWAYS be a square matrix.

worksofcraft
November 6th, 2010, 08:44 PM
i need to transpose a matrix, my function returns my matrix as

0 0 0 0
0 0 0 0
0 0 0 0

when the inputted matrix is
1 2 3 4
1 2 3 4
1 2 3 4

void transpose (int ***A, int m, int n)

{
int i;
int j;
int **matrix = createMatrix(m, n);

while (j<n)
{
for (i = 0; i < m; i++)
{
matrix[j][i]= *A[i][j];
}
j++;
}

freeMatrix(*A, m, n);

*A=matrix;

return;
}

also it wont ALWAYS be a square matrix.

Well we were told on your last thread not to actually do your assignment for you. So I just a give you a hint...

where you say

while (j<n)

What value do you think j has when your program arrives at that while statement ?

holiday
November 6th, 2010, 08:46 PM
Well we were told on your last thread not to actually do your assignment for you. So I just a give you a hint...

where you say

while (j<n)

What value do you think j has when your program arrives at that while statement ?

Dayum! I'm glad you spoke up. I was going to tell him.

You're busted, Vanilla Cone.

VanillaCone
November 6th, 2010, 09:20 PM
SOLVED

void transpose (int ***A, int m, int n)

{
int i;
int j;
int **matrix = createMatrix(m, n);

for(j = 0; j<n; j++)
{
for (i = 0; i < m; i++)
{
matrix[j][i]= (*A)[i][j];
}

}

freeMatrix(*A, m, n);

*A=matrix;

}

yes j should = 0, but that wasnt why it wasnt working, *A apparantly needs to be (*A). And Im not asking you to solve my assignments, this wasnt a problem Where i needed to logically work through a problem and find a result, this was just a syntax error That i was not aware Of because ive been doing C for 2 Months... So sue me if i ask for help

holiday
November 6th, 2010, 09:34 PM

worksofcraft
November 6th, 2010, 09:39 PM
SOLVED
...
yes j should = 0, but that wasnt why it wasnt working, *A apparantly needs to be (*A).

Kewl :) I must admit I also get a bit confused when I see things like int ***A

let's hope they don't get you to do 3 (or more) dimensional arrays next ;)

What I tend to do is make them into one dimensional arrays and then index with a calculation:

int * A = malloc(sizeof(int)*m*n);

for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
A[n*i + j] = ...
}
}

but that's probably just because I think of the whole of memory as a single big long one dimensional array anyway :shock:

VanillaCone
November 7th, 2010, 03:33 AM
that whole ***A business was just confusing :(, a pointer to a pointer that Points to a block Which points to Specific columns ><