纠结纠结,十分之纠结
随机产生20个【10,50】的正整数存放到数组中,并求数组所有元素的最大值,最小值,和所有有元素之合??程序如下,但为什么a[i]的数有的大于50和小于0的出现??各位大哥大姐们,谢谢了
#include <stdlib.h>
#include <conio.h>
void main()
{
int a[21];
int i = 0;
int ran = 0;
int sum = 0;
int max = 0;
int min = 0;
int average = 0;
clrscr();
for(i = 1; i <= 20; i++)
{
randomize();
while((ran = random(51)) / 10 == 0)
a[i] = ran;
}
max = min = a[1];
sum = 0;
for(i = 1; i <= 20; i++)
{
printf("a[%d] = %d\n" , i , a[i]);
sum += a[i];
if(max < a[i])
max = a[i];
else if(min > a[i])
min = a[i];
}
average = sum / 20;
printf("\nsum = %d , max = %d , min = %d , average = %d \n" , sum , max , min , average);
puts("\nany key to exit!");
getche();
}