装满随机数和放大阵列;打印到屏幕 [英] Fill array with random numbers & print to screen
问题描述
我是一个初学者试图填补一个3×5的二维数组用随机数则显示高,低,平均值在屏幕上。我不能让我的数组打印到屏幕上。是否有人可以帮助?
的#include< stido.h>
#包括LT&;&math.h中GT;
#包括LT&;&time.h中GT;诠释主要(无效){
int数组[3] [5];
INT practice_array;
INT I,行,列; 函数srand(时间(NULL)); 为(行= 0;行3;;行+1){ 对于(COL = 0;山坳小于5;西+1){ 数组[行] [COL] =(RAND()%10000)+ 1;
}
}
practice_array =阵列[行] [COL];
的printf(%d个,数组[行] [COL]);
返回(0);
}
您已经3个主要问题:
1 作为Jongware说,在他的评论中的printf
应该是环内,而不是外面。
2 的#include< stido.h>
不存在,这是的#include< STDIO .H>
3。 行+1
应排排= + 1
或行+ = 1
或行++
或 ++行
(在这种情况下,我们通常会使用行++
或 ++行
)。当然,你需要为山坳
结果
中:
practice_array
和 I
是无用的在这里。
湾你可能在的printf
忘了 \\ n
。
结果
我纠正你的code + I加最小值,最大值和平均值:
的#include<&stdio.h中GT;
#包括LT&;&math.h中GT;
#包括LT&;&time.h中GT;#定义ROWS_NB 3
#定义COLS_NB 5
#定义MIN_VAL 1
#定义MAX_VAL 10000INT主要(无效)
{
int数组[ROWS_NB] [COLS_NB];
INT行;
INT关口;
INT VAL;
INT分钟= MAX_VAL;
INT最大= MIN_VAL;
INT平均= 0; 函数srand(时间(NULL)); 为(行= 0;&行LT; ROWS_NB ++行)
{
对于(COL = 0;&山坳下,COLS_NB ++ COL)
{
VAL =(RAND()%(MAX_VAL - MIN_VAL))+ MIN_VAL;
如果(VAL<分)
分= VAL;
否则,如果(VAL>最大)
最大= VAL;
平均+ = VAL;
数组[行] [山口] = VAL;
//的printf(%D,VAL); / *如果你要打印的阵列取消注释* /
}
//的printf(\\ n); / *注释去掉,如果你要打印的数组* /
}
平均/ = ROWS_NB * COLS_NB;
的printf(分钟数:%d \\ n最大数:%d \\ naverage数:%d \\ n,最小值,最大值,平均值);
返回(0);
}
I am a beginner trying to fill a 3x5 an 2D array with random number then display the high, low, and average values to the screen. I can't get my array to print to the screen. Can someone please help?
#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);
}
You've 3 main problems:
1. As Jongware says in his comment the printf
should be inside the loops, not outside.
2. #include <stido.h>
doesn't exists, it's #include <stdio.h>
3. row +1
should be row = row + 1
, or row += 1
, or row++
, or ++row
(in this case we usually use row++
or ++row
). Of course you need to do the same for col
Secondary:
a. practice_array
and i
are useless here.
b. you probably forgot the \n
in the printf
.
I corrected your code + I added min, max, and average values:
#include <stdio.h>
#include <math.h>
#include <time.h>
#define ROWS_NB 3
#define COLS_NB 5
#define MIN_VAL 1
#define MAX_VAL 10000
int main(void)
{
int array[ROWS_NB][COLS_NB];
int row;
int col;
int val;
int min = MAX_VAL;
int max = MIN_VAL;
int avg = 0;
srand(time(NULL));
for (row = 0; row < ROWS_NB; ++row)
{
for (col = 0; col < COLS_NB; ++col)
{
val = (rand() % (MAX_VAL - MIN_VAL)) + MIN_VAL;
if (val < min)
min = val;
else if (val > max)
max = val;
avg += val;
array[row][col] = val;
//printf("%d ", val);/* uncomment if you want to print the array */
}
//printf("\n");/* uncomment if you want to print the array */
}
avg /= ROWS_NB * COLS_NB;
printf("min: %d\nmax: %d\naverage: %d\n", min, max, avg);
return (0);
}
这篇关于装满随机数和放大阵列;打印到屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!