2014-04-04 49 views
0

我是一个尝试用随机数填充3x5二维数组的初学者,然后在屏幕上显示高,低和平均值。我无法让我的阵列打印到屏幕上。有人可以帮忙吗?用随机数填充数组并打印到屏幕

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

你需要将'printf'命令放入'row'和'col'的循环中;或者,如果您首先要计算数组并​​然后*打印它们,请重复循环打印。 – usr2564301

+2

'#include '不存在,它是'#include '。 – Biduleohm

+0

我的数组打印错误,它不会停止添加nubers,你能帮我吗? – user3479702

回答

3

您已经3个主要问题:

作为Jongware在他的评论说,printf应的循环,不在外面里面。

2.#include <stido.h>不存在,这是#include <stdio.h>

row +1row = row + 1,或row += 1,或row++,或者++row(在这种情况下,我们通常用row++++row)。当然,你需要为col


次做同样的:

一个。practice_arrayi在这里没用。

b。你可能忘了printf中的\n


我纠正你的代码+我加了最小值,最大值和平均值:

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

+1好的编辑。几乎没有后续评论。你可能不需要'我'了。如果在整行之后打印,而不是每个元素,“\ n”可能会更好看。您可能需要添加计算代码并显示高,低和平均值。 – Arun

+0

@阿伦你说得对,我会编辑,谢谢;) – Biduleohm

1

你不能像这样打印数组。每个元素必须由它自己打印。

for (row = 0; row < 3; row++){ for (col = 0; col < 5; col++){ printf ("%d ", array[row][col]); } }

0

有在我为您解决代码各种事情。

要包括该库是stdio.h 山口和行的价值不能被正确 更新和printf声明需要去在环路和打印每个值,因为它是popul

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