C Primer+Plus(十二)编程练习

6、编写一个产生1000个1到10范围内的随机数的程序。不必保存或打印数字,仅打印每个数被产生了多少次。让程序对10个不同的种子值进行计算。

#include<stdio.h>
#include<stdlib.h>

int sides=10;
static int a[10];
void f1(int times);
int main(void)
{
  int times;
  int i;
  extern int a[10];    //可选的引用声明
  
  printf("please input the numbers for times:\n");
  scanf("%d",&times);

  f1(times);
  
  for(i=1;i<11;i++)
    printf("the number %d\'s times is:%d.\n",i,a[i-1]);
  getch();
  return 0;
}

void f1(int x)
{
  extern int sides;
  extern int a[];  //可选的引用声明
  int i,b;
  for(i=0;i<x;i++)
    {
       srand((int)time(NULL)); 
       b=rand()%sides+1;
       switch(b)
         { case 1:a[0]++;break;
           case 2:a[1]++;break;
           case 3:a[2]++;break;
           case 4:a[3]++;break;
           case 5:a[4]++;break;
           case 6:a[5]++;break;
           case 7:a[6]++;break;
           case 8:a[7]++;break;
           case 9:a[8]++;break;
           case 10:a[9]++;break;
         }
      }
}

上面程序的输出很奇怪,除了某一个数组元素为1000之外,其他都为0;这是什么原因?原因在于程序运行时间非常短,一秒都不要,所以每次通过srand()给rand()函数传递的参数都是相同的。有什么方法改进呢?方法是把srand()语句放置在rand()代码块的外部,如下例是放在main()函数中,这就不会使得每一次rand()都通过srand()获得几近相同的参数了。

#include<stdio.h>
#include<stdlib.h>

int sides=10;
static int a[10];
void f1(int times);
int main(void)
{
  int times;
  int i;

  printf("please input the numbers for times:\n");
  scanf("%d",&times);
  srand((int)time(NULL)); 

  f1(times);
  
  for(i=1;i<11;i++)
    printf("the number %d\'s times is:%d.\n",i,a[i-1]);
  getch();
  return 0;
}
//

void f1(int x)
{
  int i,b;
  for(i=0;i<x;i++)
    {
       b=rand()%sides+1;
       switch(b)
         { case 1:a[0]++;break;
           case 2:a[1]++;break;
           case 3:a[2]++;break;
           case 4:a[3]++;break;
           case 5:a[4]++;break;
           case 6:a[5]++;break;
           case 7:a[6]++;break;
           case 8:a[7]++;break;
           case 9:a[8]++;break;
           case 10:a[9]++;break;
         }
      }
} 
  
//同时省略了外部变量的引用声明  
  

 8、下面是某程序的一部分:

//pe12-8.C
#include<stdio.h>
int *make_array(int elem,int val);
void show_arry(const int ar[],int n);
int main(void)
{
  int *pa;
  int size;
  int value;

  printf("Enter the number of elements:");
  scanf("%d",&sizes);
  while(size>0)
   {
      printf("Enter the initialization value:");
      scanf("%d",&value);
      pa=make_arryay(size,value);
      if(pa)
      {
           show_arry(pa,size);
           free(pa);
       }
       printf("Enter the number of elements(<1 to quit):");
       scanf("%d",&size);
     }
     printf("Done.\n");
     return 0;
}

给出函数make_arry()和show_array()的定义使得程序完整。函数make_array()接受两个参数。第一个是int数组的元素个数,第二个是要赋给每个元素的值。函数使用malloc()来创建一个适当大小的数组,把每个元素设定为指定的值,并返回一个数组指针。函数show_array() 以8个数一行的格式显示数组内容。
如下:

int *make_array(int elem,int val)
{
  int *p;
  int i;
  p=(int *)malloc(elem*sizeof(int));
  for(i=0;i<elem;i++)
    p[i]=val;
  return p;
}
void show_array(const int p[],int n)
{ 
  int i;
  printf("-----the array begins-----\n");
  for(i=0;i<n;i++)
  {if(i%8==0)
     printf("\n");   
   printf("  %d  ",*(p+i));
  } 
  printf("\n-----the array finish-----\n");
}

 

 

posted @ 2013-06-21 12:45  tsembrace  阅读(447)  评论(0编辑  收藏  举报