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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2776397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:23:17  来源:igfitidea点击:

Create a basic matrix in C (input by user !)

cmatrix

提问by NLed

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...

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

Any thoughts?

有什么想法吗?

Thanks :)

谢谢 :)

回答by Jacob

How about the following?

以下情况如何?

First ask the user for the number of rows and columns, store that in say, nrowsand ncols(i.e. scanf("%d", &nrows);) and then allocate memory for a 2D arrayof 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!

首先询问用户行数和列数,将其存储在 say, nrowsand ncols(ie scanf("%d", &nrows);) 中,然后为大小为nrows x ncols的二维数组分配内存。因此,您可以拥有一个由用户指定大小的矩阵,而不是固定在您硬编码的某个维度上!

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(i = 0; i < nrows; ++i)
{
   for(j = 0; j < ncols ; ++j) 
   {
      printf("%d\t",mat[i][j]);
   }
printf("\n");
}

回答by Yann Ramin

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.

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

回答by Keith Nicholas

need a

需要一个

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

回答by jeff

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

}

回答by TonuSree

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.

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

回答by Leonardo Amadori Lima

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

}

}

回答by CHECK HAROUN

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