gpt4 book ai didi

c - c 中的用户定义矩阵

转载 作者:行者123 更新时间:2023-11-30 15:37:27 25 4
gpt4 key购买 nike

我明天有一个 C 课的项目,其中一部分是创建用户定义的矩阵。用户将输入矩阵有多少行和多少列,以及矩阵中的最小和最大数字。这些数字在用户定义的数字之间是随机的。输入所有内容后,应该显示矩阵。

它编译并运行,但除了显示一个随机数之外不执行任何操作。

这是我到目前为止所拥有的:

    float GetUserInfo(){ //getting the user to define the size and values of the matrix

int nrows, ncol, min, max;
int matrix[50][50],i, j;

printf("Please enter the number of rows and columns for the matrix:\n");
printf("number of rows: ");
scanf("%d", &nrows);
printf("number of columns: ");
scanf("%d", &ncol);

printf("Now enter the min and max value:\n");
printf("min value: ");
scanf("%d", &min);
printf("max value: ");
scanf("%d", &max);


for(i=0;i<nrows;i++){
for(j=0;j<ncol;j++){

}
}

matrix[i][j]=rand();
printf("The matrix generated is:\n%d \t", matrix[i][j]);

return;

}

最佳答案

您没有在循环内分配任何值。将 matrix[i][j]=rand(); 移至循环内。

此外,您需要使用嵌套循环来打印矩阵值。

要生成指定范围内的随机数,应使用matrix[i][j] = min + rand() * (max-min)/RAND_MAX;

关于c - c 中的用户定义矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22214636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com