2010-05-05 55 views
4

我想要求用戶在矩陣中輸入他們想要的列數和行數,然後在矩陣中輸入值...我'讓他們一次插入一行數字。在C中創建一個基本矩陣(由用戶輸入!)

我該如何創建這樣的功能?

#include<stdio.h> 
main(){ 

int mat[10][10],i,j; 

for(i=0;i<2;i++) 
    for(j=0;j<2;j++){ 
    scanf("%d",&mat[i][j]); 
    } 
for(i=0;i<2;i++) 
    for(j=0;j<2;j++) 
    printf("%d",mat[i][j]); 

} 

這適用於輸入數字,但它顯示他們都在同一行......這裏的問題是,我不知道用戶有多少列或行希望,所以我不能打印出來% d%d%d矩陣形式...

有什麼想法?

謝謝:)

回答

10

以下情況如何?

首先要求的行和列的數量的用戶,存儲在發言權,nrowsncols(即scanf("%d", &nrows);),然後大小的allocate memory for a 2D arrayNROWS X NCOLS。因此,您可以使用由用戶指定大小的矩陣,而不是固定在您硬編碼的某個維度上!

然後存儲與for(i = 0;i < nrows; ++i) ...元素和除了你在換行拋出的每一行之後,即

for(i = 0; i < nrows; ++i) 
{ 
    for(j = 0; j < ncols ; ++j) 
    { 
     printf("%d\t",mat[i][j]); 
    } 
printf("\n"); 
} 
+1

@Jacob感謝那正是我正在尋找!哇,它的如此簡單,只是添加另一個printf語句換一個新的線..傳奇!謝謝 !! 哦,順便說一句,是的,我同意存儲行和列的大小,我只有創建矩陣本身的問題,所以我沒有包含代碼,但感謝分享額外的信息!更多總是有幫助的:) :) – NLed 2010-05-05 20:22:16

+0

@DM啊我明白了,很高興能有所幫助!另外,不要忘記列的標籤,例如'printf(「%d \ t」,mat [i] [j]);'另外,我包含的鏈接詳細解釋瞭如何使用'malloc'作爲二維數組。你應該考慮實施......爲了好玩,至少:) – Jacob 2010-05-05 20:39:05

+0

嗯我不知道關於\ tabbing列,它是做什麼的?不能我使用浮動和例如使用5.2%f? – NLed 2010-05-07 12:19:53

2

您需要動態分配您的矩陣。例如:

int* mat; 
int dimx,dimy; 
scanf("%d", &dimx); 
scanf("%d", &dimy); 
mat = malloc(dimx * dimy * sizeof(int)); 

這創建了一個可以容納矩陣的線性數組。此時,您可以決定是否要先訪問列或行。我會建議做一個快速的宏來計算矩陣中正確的偏移量。

+1

@theatrus:感謝回答,您提供的代碼是在上下文中簡單,但更先進的,我不知道這些代碼,但(的malloc等)謝謝:) – NLed 2010-05-05 20:22:55

1

顯示相同的方式中的元素需要

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

@Keith Nicholas:謝謝你回覆,是的,這是雅各解釋它的方式,謝謝你的回覆! :) – NLed 2010-05-05 20:21:34

1
#include<stdio.h> 
int main(void) 
{ 
int mat[10][10],i,j; 

printf("Enter your matrix\n"); 
for(i=0;i<2;i++) 
    for(j=0;j<2;j++) 
    { 
    scanf("%d",&mat[i][j]); 
    } 
printf("\nHere is your matrix:\n"); 
for(i=0;i<2;i++)  
{ 
    for(j=0;j<2;j++) 
    { 
     printf("%d ",mat[i][j]); 
    } 
    printf("\n"); 
    } 

} 
1

這是我的回答

#include<stdio.h> 
int main() 
{int mat[100][100]; 
int row,column,i,j; 
printf("enter how many row and colmn you want:\n \n"); 
scanf("%d",&row); 
scanf("%d",&column); 
printf("enter the matrix:"); 

for(i=0;i<row;i++){ 
    for(j=0;j<column;j++){ 
     scanf("%d",&mat[i][j]); 
    } 

printf("\n"); 
} 

for(i=0;i<row;i++){ 
    for(j=0;j<column;j++){ 
     printf("%d \t",mat[i][j]);} 

printf("\n");} 
} 

我只是選擇行和列的近似值。我選擇的行或列不會越過該值。然後我掃描矩陣元素,然後將其設置爲矩陣大小。

0
int rows, cols , i, j; 
printf("Enter number of rows and cols for the matrix: \n"); 
scanf("%d %d",&rows, &cols); 

int mat[rows][cols]; 

printf("enter the matrix:"); 

for(i = 0; i < rows ; i++) 
    for(j = 0; j < cols; j++) 
     scanf("%d", &mat[i][j]); 

printf("\nThe Matrix is:\n"); 
for(i = 0; i < rows ; i++) 
{ 
    for(j = 0; j < cols; j++) 
    { 
     printf("%d",mat[i][j]); 
     printf("\t"); 
    } 
    printf("\n"); 
} 

}

-2
//R stands for ROW and C stands for COLUMN: 

//i stands for ROW and j stands for COLUMN: 

#include<stdio.h> 

int main(){ 

    int M[100][100]; 

    int R,C,i,j; 

    printf("Please enter how many rows you want:\n"); 

    scanf("%d",& R); 

    printf("Please enter how column you want:\n"); 

    scanf("%d",& C); 

    printf("Please enter your matrix:\n"); 

    for(i = 0; i < R; i++){ 

     for(j = 0; j < C; j++){ 

      scanf("%d", &M[i][j]); 

     } 

     printf("\n"); 

    } 
    for(i = 0; i < R; i++){ 

     for(j = 0; j < C; j++){ 

      printf("%d\t", M[i][j]); 

     } 
     printf("\n"); 

    } 

    getch(); 

    return 0; 
}