Question

Ok so I have a 10x10 array that I need to flip and print. I made this function to do just that

void flip(int d[][10], int rows)
{
    int temp, x, y, cols;

    cols=rows;

    for(x=0; x<rows; x++)
    {
        for(y=0; y<cols; y++)
        {
            temp=d[x][y];
            d[x][y]=d[y][x];
            d[y][x]=temp
        }
    }
}

Now I know arrays are passed by reference but I also read somewhere that arrays itself act as pointers so you don't have to use pointer notation which seems right. My problem is that when I try to print after flipping it, it does not print the flipped array, but prints out the original one making me think that it isn't flipping the original array.

Here is the print function.

void printArray(int d[][10])
{
    int rows, cols,x,y;

    rows = sizeof(d[0])/sizeof(d[0][0]);

        cols = rows;

    for(x=0;x<rows; x++)
    {
        for(y=0;y<cols;y++)
            printf("%2d ",d[x][y]);
        printf("\n");
    }   
}

Odd thing is if I change temp into a "hard" value like the number 10, then it does print out a 10x10 array with half of them being 10s. I am at a loss here why the simple swapping isn't working :(

Was it helpful?

Solution

From what I can tell, by "flip" you mean "transpose"...

Also, if you work out the code by hand, your code works, but twice - aka, you get the original matrix. You can try changing the inner for loop to start at x, not zero.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top