用C语言创建一个基本矩阵(用户输入!)

e5njpo68  于 2023-03-01  发布在  其他
关注(0)|答案(8)|浏览(165)

我尝试让用户输入他们想要的矩阵中的列数和行数,然后在矩阵中输入值...我将让他们一次插入一行数字。
如何创建这样的函数?

#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 ...
有什么想法吗?
谢谢:)

nwwlzxa7

nwwlzxa71#

下面这些怎么样?
首先询问用户行数和列数,将其存储在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");
}
rsaldnfx

rsaldnfx2#

您需要动态分配基准表。例如:

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

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

w6mmgewl

w6mmgewl3#

需要一个

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

lb3vh1jj4#

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

}
wa7juj8i

wa7juj8i5#

这就是我的答案

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

我只是选择一个近似值的行和列。我所选择的行或列将不会交叉的值。然后我扫描矩阵元素,然后使其在矩阵大小。

4nkexdtk

4nkexdtk6#

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

}

vi4fp9gy

vi4fp9gy7#

我希望下面的代码将为您工作。

#include<stdio.h>
int main()
{
    int i,j,a_row,a_col,b_row,b_col;
    printf("\n Enter the rows and columns of matrix a: \n");
    scanf("%d", &a_row);
    scanf("%d", &a_col);
    int a[a_row][a_col];
    printf("\n Enter the elements of matrix a: ");
    for(i=0 ; i < a_row ; i++)
    {
        for(j=0; j < a_col ;j++)
        {
            scanf("%d", &a[i][j]);
        }
    }

    i = 0;
    j = 0;
    printf("\n Enter the rows and columns of matrix b: \n");
    scanf("%d",&b_row);
    scanf("%d",&b_col);
    int b[a_row][a_col];
    printf("\n Enter the elements of matrix b: ");
    for(i=0;i<b_row;i++)
    {
        for(j=0;j<b_col;j++)
        {
            scanf("%d", &b[i][j]);
        }
    }

    printf("\n contents of matrix a are: \n");
    for(i=0;i<a_row;i++)
    {
        for(j=0;j<b_col;j++)
        {
            printf("%d\t", a[i][j]);
        }
        printf("\n");
    }

    printf("\n contents of matrix b are: \n");
    for(i=0;i<b_row;i++)
    {
        for(j=0;j<b_col;j++)
        {
            printf("%d\t", b[i][j]);
        }
        printf("\n");
    }
}
polhcujo

polhcujo8#

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

相关问题