C 用随机数填充数组&;打印到屏幕
我是一个初学者,尝试用随机数填充3x5和2D数组,然后在屏幕上显示高、低和平均值。我无法将数组打印到屏幕上。有人能帮忙吗C 用随机数填充数组&;打印到屏幕,c,arrays,C,Arrays,我是一个初学者,尝试用随机数填充3x5和2D数组,然后在屏幕上显示高、低和平均值。我无法将数组打印到屏幕上。有人能帮忙吗 #include <stido.h> #include <math.h> #include <time.h> int main (void){ int array [3][5]; int practice_array; int i, row, col; srand(time(NULL)); fo
#include <stido.h>
#include <math.h>
#include <time.h>
int main (void){
int array [3][5];
int practice_array;
int i, row, col;
srand(time(NULL));
for ( row = 0; row < 3; row +1){
for ( col = 0; col < 5; col +1){
array[row][col] = (rand()%10000) + 1;
}
}
practice_array = array[row][col];
printf("%d", array[row][col]);
return (0);
}
#包括
#包括
#包括
内部主(空){
整数数组[3][5];
int-practice_数组;
int i,行,列;
srand(时间(空));
用于(行=0;行<3;行+1){
用于(列=0;列<5;列+1){
数组[行][列]=(rand()%10000)+1;
}
}
实践_数组=数组[行][列];
printf(“%d”,数组[行][col]);
返回(0);
}
你不能就这样打印数组。每个元素必须自己打印
for(行=0;行<3;行++){
用于(列=0;列<5;列++){
printf(“%d”,数组[行][col]);
}
}
你不能就这样打印数组。每个元素必须自己打印
for(行=0;行<3;行++){
用于(列=0;列<5;列++){
printf(“%d”,数组[行][col]);
}
}
您有三个主要问题:
1.正如Jongware在评论中所说的那样,printf
应该在循环内部,而不是外部
2.#include
不存在,它是#include
3.行+1
应该是行=行+1
,或行+=1
,或行+++
,或++row
(在这种情况下,我们通常使用行+
或+++row
)。当然,对于col
次要: a.
practice\u数组和i
在这里是无用的
b.您可能忘记了printf
中的\n
我更正了您的代码+我添加了最小值、最大值和平均值:
#包括
#包括
#包括
#定义第3行
#定义COLS_NB 5
#定义最小值1
#定义最大值10000
内部主(空)
{
int数组[ROWS_NB][COLS_NB];
int行;
int col;
int-val;
int min=最大值;
int max=最小值;
int平均值=0;
srand(时间(空));
对于(行=0;行<行\u NB;++行)
{
对于(col=0;colmax)
max=val;
平均值+=val;
数组[行][col]=val;
//printf(“%d”,val);/*如果要打印数组,请取消注释*/
}
//printf(“\n”);/*如果要打印数组,请取消注释*/
}
平均值/=行数×列数;
printf(“最小值:%d\n最大值:%d\n平均值:%d\n”,最小值,最大值,平均值);
返回(0);
}
您有三个主要问题:
1.正如Jongware在评论中所说的那样,printf
应该在循环内部,而不是外部
2.#include
不存在,它是#include
3.行+1
应该是行=行+1
,或行+=1
,或行+++
,或++row
(在这种情况下,我们通常使用行+
或+++row
)。当然,对于col
次要:
a.practice\u数组和i
在这里是无用的
b.您可能忘记了printf
中的\n
我更正了您的代码+我添加了最小值、最大值和平均值:
#包括
#包括
#包括
#定义第3行
#定义COLS_NB 5
#定义最小值1
#定义最大值10000
内部主(空)
{
int数组[ROWS_NB][COLS_NB];
int行;
int col;
int-val;
int min=最大值;
int max=最小值;
int平均值=0;
srand(时间(空));
对于(行=0;行<行\u NB;++行)
{
对于(col=0;colmax)
max=val;
平均值+=val;
数组[行][col]=val;
//printf(“%d”,val);/*如果要打印数组,请取消注释*/
}
//printf(“\n”);/*如果要打印数组,请取消注释*/
}
平均值/=行数×列数;
printf(“最小值:%d\n最大值:%d\n平均值:%d\n”,最小值,最大值,平均值);
返回(0);
}
我为您修改的代码中有很多东西
要包含的库是stdio.h
col和row的值未正确更新
而且,printf
语句需要进入循环,并按原样打印每个值
include <stdio.h>
include <math.h>
include <time.h>
int main (void)
{
int array [3][5];
int i, row, col;
srand(time(NULL));
for ( row = 0; row < 3; row++)
{
for ( col = 0; col < 5; col++)
{
array[row][col] = (rand()%10000) + 1;
printf("%d ", array[row][col]);
}
printf("\n");
}
return (0);
}
包括
包括
包括
内部主(空)
{
整数数组[3][5];
int i,行,列;
srand(时间(空));
用于(行=0;行<3;行++)
{
用于(列=0;列<5;列++)
{
数组[行][列]=(rand()%10000)+1;
printf(“%d”,数组[行][col]);
}
printf(“\n”);
}
返回(0);
}
我为您修改的代码中有很多东西
要包含的库是stdio.h
col和row的值未正确更新
而且,printf
语句需要进入循环,并按原样打印每个值
include <stdio.h>
include <math.h>
include <time.h>
int main (void)
{
int array [3][5];
int i, row, col;
srand(time(NULL));
for ( row = 0; row < 3; row++)
{
for ( col = 0; col < 5; col++)
{
array[row][col] = (rand()%10000) + 1;
printf("%d ", array[row][col]);
}
printf("\n");
}
return (0);
}
包括
包括
包括
内部主(空)
{
整数数组[3][5];
int i,行,列;
srand(时间(空));
用于(行=0;行<3;行++)
{
用于(列=0;列<5;列++)
{
数组[行][列]=(rand()%10000)+1;
printf(“%d”,数组[行][col]);
}
printf(“\n”);
}
返回(0);
}
您需要将printf
命令放入行的循环中