2

I want to change rows into column and column into rows of that 2-D array

I want a program which takes input and gives output as below.

Input:   1    2    3                       
         4    5    6

Output:  1    4
         2    5
         3    6

Input:   1    2    3
         4    5    6
         7    8    9

Output:  1    4    7
         2    5    8
         3    6    9

I did a sample which in hardcoded array as below

int main()
{
    int i,j;
    int grades[2][3] = { {55, 60, 65},
                                           {85, 90, 95} 
                                        };
    for( j = 0; j < 3; j++)
    {       
      for( i = 0; i < 2;i++)
      {
         printf("%d\t",grades[i][j]);
      }
      printf("\n");
    }

    return 0;
}

Its long time since i programmed in C , is there anyway we can make things dynamic or better way of doing the same. Right now its hardcoded.

I remember we have to use malloc or so , is that right.

psuedo code is also fine.

5
  • malloc() and free() are for heap-allocation, yes. Commented Jul 6, 2011 at 1:28
  • @muntoo i am reading about them now ,i read c only in college and again now. Commented Jul 6, 2011 at 1:29
  • Are you trying to write a standalone program to invert matrices or write a transpose function as part of a bigger program? If it's a standalone program, how is your program supposed to get its input and output? Commented Jul 6, 2011 at 2:51
  • @david , its a standalone program only Commented Jul 6, 2011 at 3:20
  • If it's a standalone program, how is your program supposed to get its input and output? Commented Jul 6, 2011 at 3:53

6 Answers 6

2

Taking from Zhehao Mao user and fixing it, the would look like this:

#include <stdio.h>

void transpose(int *src, int *dest, int rows, int cols){
    int i,j;
    for(i=0; i<rows; i++){
        for(j=0; j<cols; j++){
            dest[j*rows + i] = src[i*cols + j];
        }
    }
}

int main(void)
{
    int oldar[2][3] = {{1,2,3},{4,5,6}};
    int newar[3][2];
    transpose(&oldar[0][0], &newar[0][0], 2, 3);
    int i, j;

    for(i = 0; i < 2; i++)
    {
      for(j = 0; j < 3; j++)
         printf("%d ", oldar[i][j]);
      printf("\n");
    }

    for(i = 0; i < 3; i++)
    {
      for(j = 0; j < 2; j++)
         printf("%d ", newar[i][j]);
      printf("\n");
    }
}

The reason the original post can't work is that int ** expects a pointer to pointers like:

 int **a --------->     int *int1    --> 1
                        int *int2    --> 2
                        int *int3    --> 3

which is not what we get when we say int a[n][m]. Rather we have the array organized like this

               a[0][0] 
                  \
                   1 2 3 4 5 6
                   \___/ \___/
            "a[0]" /      \____ "a[1]"

or something like this. The picture likely does not explain it well, but currently I can't do better.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh, OK, I understand. It's because a double array is actually a single array where ar[1][2] is equivalent to ar[1*rows+2].
maybe there's a better way to say it: ar[n][m] is an array of n arrays made of m elements each... if I am right it is like if you have "typedef int row[n]; row arr[m];", i.e. arr is an array of m rows, where each "row" is made of n ints.
1
void main()

{

   clrscr();

   int in[10][10];
   int out[10][10];

   int row,column,i,j;
   printf("enter row");
   scanf("%d",&row);
   printf("Enter column");
   scanf("%d",&column);
   //storing values in matrix
   for(i=1;i<=row;i++)
   {
      for(j=1;j<=column;j++)
      {
        printf("Enter (%d,%d)th value",i,j);
        scanf("%d",&in[i-1][j-1]);
      }
   }
   //show stored values
   printf("\ninput is\n\n");
   for(i=0;i<row;i++)
   {
      for(j=0;j<column;j++)
      {
        printf("%d\t",in[i][j]);
      }
      printf("\n");
   }
   //show transposed value. it is also stored in out matrix
   printf("\nOutput is\n\n");
   for(i=0;i<column;i++)
   {
      for(j=0;j<row;j++)
      {
        printf("%d\t",in[j][i]);
        out[i][j]=in[j][i];
      }
      printf("\n");
   }

   getch();

}

//////////////////////////////////////

input matrix is stored in in[][] matrix and output matrix stored in out[][] matrix. this program will work for any matrix with row and column below 10 if we increase the matrix variable value ,it will work for larger matrix also .

Comments

1

Here is a rather naive implementation. I'm pretty sure there are more efficient ways, but this is all I could think of.

void transpose(int **src, int **dest, int rows, int cols){
    int i,j;
    for(i=0; i<rows; i++){
        for(j=0; j<cols; j++){
            dest[j][i] = src[i][j];
        }
    }
}

int main(void){
    int oldar[2][3] = {{1,2,3},{4,5,6}};
    int newar[3][2];
    transpose(oldar, newar, 2, 3);
}

Double pointers can represent double arrays, so there is no need to allocate on the heap here.

1 Comment

it won't work! int ** expect an array of pointers to int, while in C something like oldar[2][3] construct no such kind of "indirection"; you have to use int , and knowing the size of cols rows, do something like src[icols + j] or something like that --- or change how you pass the arrays (gcc raises a warning)
1

This is a half-done program the way I would do it in C:

int main()
{
    int **data;
    int rows = 0,
        columns = 0;

    char in[256];

    int *irow;

    // Get user input.
    for(rows = 0; 1; ++rows)
    {
        scanf("%255s", in);

        if(strcmp(in, "exit") == 0)
            break;

        // Parse row here. Remove all the tabs. Set column count.
        for(int icolumn = 0; 1; ++icolumn)
        {
            /* ... */
        }

        // Set columns if first time.
        if(rows == 0)
            columns = icolumn;

        // Check to make sure user inputs correct amount of columns.
        if(columns != icolumns)
        {
            printf("OMG! The user is a hacker!\n");
            break;
        }

        // Push parsed row into **data.
        data[rows] = irow;
    }

    // Display output.
    for(int i = 0; i < columns; ++i)
    {       
        for(int j = 0; j < rows; ++j)
        {
            printf("%d\t", data[j][i]);
        }

        printf("\n");
    }

    return 0;
}

I'm a C++ programmer, so the user input part is kind of messed up.

Comments

0

hey here is a simple solution without using malloc,i did this when i was on the 0th level for c and had no idea about "alloc.h" functions, You can have the square array having #rows = #cols = max(#rows,#cols),if we take your example then the matrix would be a 3x3 matrix,then add any special char in the blank entries,so the matrix will look like this

matrix:1 2 3 
       4 5 6 
       @ @ @

now you can easily convert the matrix in the way you want... Bottom line:To make the matrix operations simpler try to convert them in square matrix... One more thing using MALLOC is the best possible way ,this is just in case you are not handy with all those alloc.h function defs...

1 Comment

hey we need put a filter that checks for '@' and whenever you encounter '@' all you need to do is to continue to next respective row or clumn so no '@'s will get printed ,sorry but i don't have the code right now,better you try it ,i guess enough hints are there...Happy coding
0

theoretically, you have two arrays

Array x and y

Int grades [x] [y]

you can swap these two arrays and you get

int grades [y] [x]

to do that there are many methods e.g. by copying the arrays to another two 1D, or one 2D Array, or simple Pointer Swap

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.