生成一个范围内的随机整数(无限循环)

时间:2013-05-27 17:45:53

标签: c loops random

我遇到了这段代码的问题,因为当我运行它时,我会得到一个带有数字随机生成器的无限循环。我要做的是分配一个数组,从1到9的99个数字,然后做一些数学简单的操作。

#include <stdio.h>
#include <stdlib.h>
#define SIZE 99
void mean(const int a[]);
void median( int a[]);
void mode(int freq[] , const int a[]);

int main (void) {
   int response[SIZE];
   int frequency [10];
   int i;
   srand(time(NULL));
   for (i = 0; i<= SIZE ; i++) {
      response[i] = (rand() % 6) +1 ;
      printf("%d", response[i]);
   }
   mean(response);
   median( response);
   mode(frequency , response);

return 0;
}


void mean(const int a[]){
   int j , total = 0;
   float mean;
   printf("********\n%6s\n********\n\nThe mean is the average value of the data\nitems.", "Mean");
   printf("The mean is equal to the total of\n all the data items");
   printf("divided by the number\n of data items (%d):", SIZE);
   for( j = 0 ; j <= SIZE ; j++){
      total += a[j];
   }
   mean = (float) total / SIZE;
   printf("The mean value for\nthis run is %d / %d = %f", total, SIZE, mean);
}

void median( int a[]){
   int i, j, n, median, hold;
   n=1;
   hold = 0;
   printf("********\n%7s\n********\n\nThe unsorted array of responses is\n", "Median");
   for (i=0;i<=SIZE;i++){
      if ((i/10) <= n){
         printf("%d", a[i]);
      }
      else{
         printf("\n");
         n++;
      }
   }
   printf("The sorted array is\n");
   for(i=0;i<=SIZE;i++){
      for(j=0;j<=SIZE-1;j++){
         if (a[j]>a[(j+1)]){
            hold = a[j];
            a[j] = a[ (j + 1)];
            a[ (j + 1)] = hold;
         }
      }
   if ((i/10) <= n){
         printf("%d", a[i]);
      }
      else{
         printf("\n");
         n++;
      }
   }
   median = a[SIZE/2];
   printf("The median is element %d of\nthe stored %d element array.\n", SIZE/2 , SIZE);
   printf("For this run the median is %d", median);
}

void mode ( int freq [] , const int a[]){
   int j, o, mode , i, rating;
   printf("********\n%6s\n********\n\n%10s%12s%12s", "Mode" ,"Response" ,"Frequency", "Histogram");
   for(j=0; j<= SIZE ; j++){
      ++freq[a[j]];
   }
   for (i=0 ; i <= 10 ; i++){
      printf("%10d%12d            ", i, freq[i]);
      for (o=0; o<=freq[i];o++){
         printf("*");
      }
      printf("\n");
      if (freq[i] > freq[i+1]){
         mode = freq[i];
         rating = i;
      }
   }
   printf("The mode is the most frequent value.\n");
   printf("For this run the mode is %d which occured %d times", rating ,mode);
}

2 个答案:

答案 0 :(得分:3)

C数组从零开始,因此

的有效索引
int response[SIZE];

是[0..SIZE-1]。您的循环写入response[SIZE],这超出了为response分配的内存的末尾。这会导致未定义的行为。

如果您获得无限循环,则听起来好像response[SIZE]的地址与循环计数器i的地址相同。 (rand() % 6) +1将在[1..6]范围内,因此退出前循环的最后一次迭代将始终将i重置为较低的值。

您可以通过更改循环来更快地退出一次迭代来解决此问题。即改变

for (i = 0; i<= SIZE ; i++) {

for (i = 0; i< SIZE ; i++) {

请注意,您的其他功能都有类似的错误。所有for循环都应使用<=

替换其<退出条件

答案 1 :(得分:1)

当您访问array[SIZE]时,您会在数组末尾写入。 任何声明的数组

type_t array[SIZE];

没有元素array[SIZE]。所以所有循环必须从0到&lt; SIZE,而不是&lt; = SIZE。 这在计算机文献中被称为 off-by-one error 。你不是第一个,也不会是最后一个,如果有任何安慰: - )

从技术上讲,这会调用未定义的行为,其中无限循环是一种方式。但请参阅下面的评论,以便对这里发生的事情进行疯狂的猜测。

相关问题