在C中创建一个基本矩阵(由用户输入!)

时间:2010-05-05 20:07:15

标签: c matrix

我正在尝试让用户在矩阵中输入他们想要的列数和行数,然后在矩阵中输入值...我将让他们一次插入一行数字

我该如何创建这样的功能?

#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以矩阵形式......

有什么想法吗?

谢谢:)

7 个答案:

答案 0 :(得分:11)

以下情况如何?

首先向用户询问行数和列数,然后将其存储为nrowsncols(即scanf("%d", &nrows);),然后将allocate memory for a 2D array存储为 nrows 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 :(得分:3)

您需要动态分配矩阵。例如:

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

这会创建一个可以容纳矩阵的线性数组。此时,您可以决定是首先访问列还是行。我建议制作一个快速宏来计算矩阵中的正确偏移量。

答案 2 :(得分:1)

需要一个

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

答案 3 :(得分: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");  
  }  

}

答案 4 :(得分: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");}
}

我只选择行和列的近似值。我选择的行或列不会越过该值。然后我扫描矩阵元素,然后将其设置为矩阵大小。

答案 5 :(得分: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");
}

}

答案 6 :(得分:-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;
}