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

发布于 2024-08-31 14:49:12 字数 423 浏览 2 评论 0原文

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

我怎样才能创建这样的功能?

#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]);

}

这适用于输入数字,但它会将它们全部显示在一行中...这里的问题是我不知道用户想要多少列或行,所以我无法在 a 中打印出 %d %d %d矩阵形式...

有什么想法吗?

谢谢 :)

I'm trying to ask the user to enter the number of columns and rows they want in a matrix, and then enter the values in the matrix... I'm going to let them insert numbers one row at a time.

How can I create such function ?

#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]);

}

This works for entering the numbers, but it displays them all in one line... The issue here is that I don't know how many columns or rows the user wants, so I cant print out %d %d %d in a matrix form...

Any thoughts?

Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

一笑百媚生 2024-09-07 14:49:12

下面的怎么样?

首先询问用户行数和列数,将其存储在 nrowsncols 中(即 scanf("%d", &nrows) ;),然后分配内存大小为 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");
}

How about the following?

First ask the user for the number of rows and columns, store that in say, nrows and ncols (i.e. scanf("%d", &nrows);) and then allocate memory for a 2D array of size nrows x ncols. Thus you can have a matrix of a size specified by the user, and not fixed at some dimension you've hardcoded!

Then store the elements with for(i = 0;i < nrows; ++i) ... and display the elements in the same way except you throw in newlines after every row, i.e.

for(i = 0; i < nrows; ++i)
{
   for(j = 0; j < ncols ; ++j) 
   {
      printf("%d\t",mat[i][j]);
   }
printf("\n");
}
习惯成性 2024-09-07 14:49:12

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

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

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

You need to dynamically allocate your matrix. For instance:

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

This creates a linear array which can hold the matrix. At this point you can decide whether you want to access it column or row first. I would suggest making a quick macro which calculates the correct offset in the matrix.

未蓝澄海的烟 2024-09-07 14:49:12

需要一个

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

need a

for(i=0;i<2;i++)
{
  for(j=0;j<2;j++)
  {
     printf("%d",mat[i][j]);
  }
  printf("\n");
}
暮凉 2024-09-07 14:49:12
#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");  
  }  

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

}
孤独难免 2024-09-07 14:49:12

这是我的答案,

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

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

This is my answer

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

I just choose an approximate value for the row and column. My selected row or column will not cross the value.and then I scan the matrix element then make it in matrix size.

追风人 2024-09-07 14:49:12
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");
}

}

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

}

淡淡的优雅 2024-09-07 14:49:12

我希望下面的代码对您有用。

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

I hope the below code will work for you.

#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");
    }
}
丘比特射中我 2024-09-07 14:49:12
//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;
}
//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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文