开发者

changing rows into column and column into rows of that 2d array

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.


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.


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 .


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.


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.


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...


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜