2011-07-06 18 views
2

我想改變行到塔和列成2-d陣列改變行到塔和列到該2D陣列

我想要一個程序來輸入並如下給出的輸出行的行。

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 

我做了一個樣本,其在硬編碼排列如下

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; 
} 

它,因爲我用C編程的時間長了,反正是有我們可以做同樣的事情的動態或更好的辦法。現在它的硬編碼。

我記得我們必須使用malloc左右,是嗎。

psuedo代碼也很好。

+0

'malloc()'和'free()'用於堆分配,是的。 –

+0

@muntoo現在我正在閱讀有關他們的文章,我只在大學讀過c,現在又讀了。 – kobe

+0

您是否正在嘗試編寫一個獨立的程序來反轉矩陣或將'transpose'函數作爲更大程序的一部分?如果它是一個獨立的程序,你的程序應該如何獲得它的輸入和輸出? –

回答

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(); 

} 

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

輸入矩陣存儲在[] []矩陣中,輸出矩陣存儲在[] []矩陣中。 如果我們增加矩陣變量值,這個程序將適用於任何行數小於10的矩陣,它也適用於大矩陣。

0

嘿這裏是一個簡單的解決方案,而不使用malloc,我這樣做,當我在0級別的C和不知道「alloc.h」函數, 你可以有方形數組有#rows =#的cols = MAX(#行#的cols),如果我們把你的例子則矩陣是一個3x3矩陣,然後添加任何特殊字符的空白項,所以矩陣將看起來像這樣

matrix:1 2 3 
     4 5 6 
     @ @ @ 

現在你可以很容易地轉換你想要的方式矩陣... 底線:爲了使矩陣操作更簡單,嘗試將它們轉換成方矩陣... 使用MALLOC的另一件事是最好的方式,這只是在CAS Ë你是不是方便與所有這些alloc.h功能DEFS ...

+0

如果我們正確顯示字符,它看起來不太好。 – kobe

+0

@你可以發佈代碼如果可能 – kobe

+0

嗨,我們需要把一個過濾器,檢查'@',每當你遇到'@',你需要做的就是繼續下一個相應的行或clumn,所以沒有'@'會得到印刷,抱歉,但我現在沒有代碼,更好地嘗試它,我想有足夠的提示在那裏...快樂編碼 – buch11

0

理論上,你有兩個數組

陣列X和Y

詮釋等級[X] [Y]

您可以交換這兩個數組,你會得到

INT等級[Y] [X]

要做到這一點有很多方法如通過將陣列複製到另外兩個1D或一個2D陣列或簡單指針交換

1

這是一個相當天真的實現。我很確定有更有效的方法,但這是我能想到的。

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); 
} 

雙指針可以表示雙數組,因此不需要在這裏分配堆。

+0

它不會工作! int **期望指向int的指針數組,而在C中,像oldar [2] [3]這樣的構造沒有這樣的「間接」;你必須使用int *,並知道cols行的大小,做一些像src [i * cols + j]或類似的東西---或者改變你傳遞數組的方式(gcc引發一個警告) – ShinTakezou

1

這是一個半完成計劃的方式,我會做它在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; 
} 

我是一個C++程序員,所以用戶輸入部分是種搞砸。

2

從哲浩毛澤東用戶以固定吧,應該是這樣的:

#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"); 
    } 
} 

原來的職位不能工作的原因是,INT **需要一個指向像指針:

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

這不是我們在說int a [n] [m]時得到的結果。相反,我們有這樣的陣列組織

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

或類似的東西。這幅圖可能不能很好地解釋,但目前我無法做得更好。

+0

哦,好的, 我明白。這是因爲雙數組實際上是一個單個數組,其中ar [1] [2]等同於ar [1 * rows + 2]。 –

+0

也許有更好的方式來說:ar [n] [m]是由m個元素構成的n個數組的數組,每個元素...如果我是對的,就好像你有「typedef int row [n]; row arr [m];「,即arr是m行的數組,其中每個」行「由n個ints組成。 – ShinTakezou