资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

c语言一百个函数,c语言100实例

用C语言随机函数生成100个不大于100的数的具体算法怎么写?

;Key=0BlogID=382219PostID=4547421

肇东网站制作公司哪家好,找创新互联公司!从网页设计、网站建设、微信开发、APP开发、自适应网站建设等网站项目制作,到程序开发,运营维护。创新互联公司从2013年开始到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联公司

以上网有详细的说明

/*已经上机通过测试:*/

#include stdlib.h

main()

{

int a[100],i; /*定义数组存放100个数*/

for(i=0;i100;i++)

a[i]=random(100); /*产生100以内的数*/

for(i=0;i100;i++)

printf("%d ",a[i]); /*打印输入*/

getch();

}

在C语言函数库中包含了一个产生随机数的函数:

int rand( void );

在函数库中对这个函数的说明是:

The rand function returns a pseudorandom integer in the range

0 to RAND_MAX. Use the srand function to seed the pseudorandom

-number generator before calling rand.

而在C语言函数库中是这样定义RAND_MAX的:

/* Maximum value returned by "rand" function

*/

#define RAND_MAX 0x7FFF

所以,函数int rand( void );返回的是一个界于0~32767(0x7FFF)之

间的伪随机数,包括0和32767。注意,这里产生的是伪随机数,不是真正意

义上的随机数,看下面的程序:

#include "stdlib.h"

#include "stdio.h"

void main( void )

{

/* Display a number. */

printf( " %6d\n", rand() );

getchar();

}

程序运行的结果是:

346

多次运行这个程序,发现每次产生的结果都是346(不同的机器可能产生

的结果不一样),这就是所谓的伪随机数。伪随机数是通过一个公式来运算

出来的,所以,每次产生的伪随机数都一样。那么,如何才能产生真正意义

上的随机数呢?这就有一个随机种子的问题。在C语言标准函数库中,有这

么一个函数:

void srand( unsigned int seed );

在《The c programming language》中对这个函数是这样描述的:

srand uses seed(函数变量声明中的seed) as the seed(随机函数中种子

的意思) for a new sequence of pseudo-random numbers. The

initial seed is 1.

所以,要产生真正意义上的随机数,那么就要求每次提供的种子不一样,一

般情况下,都设置时间为随机函数的种子。看下面的一段程序:

/* RAND.C: This program seeds the random-number generator

* with the time, then displays 10 random integers.

*/

#include "stdlib.h"

#include "stdio.h"

#include "time.h"

void main( void )

{

int i;

/* Seed the random-number generator with current time so that

the numbers will be different every time we run.

将当前时间设置成随机函数的种子,所以每次产生的数都不一样

*/

srand( (unsigned)time( NULL ) );

/* Display 10 numbers. */

for( i = 0; i 10;i++ )

printf( “ %6d\n”, rand() );

}

Output

6929

8026

21987

30734

20587

6699

22034

25051

7988

10104

每次运行这个程序,产生的随机数都不一样,这样就达到了随机数的要求了

C语言100个函数依次调用

可以设一个函数指针数组,数组中的元素分别指向每个函数,然后用for循环依次调用。

C语言一共有多少个函数

c语言本身是没有函数的,函数是基于语言实现的,一门语言首先是有基本语意单位,语法,

c语言有32个关键字,这个自己百度下,

你之所以会以为c语言有函数,

一是编译器约定,如main();

二是引用库,如stdio.h;

三是自己定义,如int myfun();

C语言:通过随机函数产生100个( 0--100 )之间的整数,统计其中各个数据段的数据数量,已经给出框架?

#include stdio.h

#include time.h

#include stdlib.h

//产生n个随机数,依次放在x指向的内存空间

void funRand(int *x, int n) {

srand((unsigned) time(NULL));

for (int i = 0; i n; i++) {

  *(x + i) = rand() % (n + 1);

}

}

//统计x指向的内存空间中数据的分段数量,保存在以y指向的内存空间

void funCount(int *x, int n, int *y) {

int count_09 = 0;

int count_1019 = 0;

int count_2029 = 0;

int count_3039 = 0;

int count_4049 = 0;

int count_5059 = 0;

int count_6069 = 0;

int count_7079 = 0;

int count_8089 = 0;

int count_9099 = 0;

int count_100 = 0;

for (int i = 0; i n; i++) {

  if (*(x + i) = 0 *(x + i) 10)

      *y = ++count_09;

  if (*(x + i) = 10 *(x + i) 20)

      *(y + 1) = ++count_1019;

  if (*(x + i) = 20 *(x + i) 30)

      *(y + 2) = ++count_2029;

  if (*(x + i) = 30 *(x + i) 40)

      *(y + 3) = ++count_3039;

  if (*(x + i) = 40 *(x + i) 50)

      *(y + 4) = ++count_4049;

  if (*(x + i) = 50 *(x + i) 60)

      *(y + 5) = ++count_5059;

  if (*(x + i) = 60 *(x + i) 70)

      *(y + 6) = ++count_6069;

  if (*(x + i) = 70 *(x + i) 80)

      *(y + 7) = ++count_7079;

  if (*(x + i) = 80 *(x + i) 90)

      *(y + 8) = ++count_8089;

  if (*(x + i) = 90 *(x + i) 100)

      *(y + 9) = ++count_9099;

  if (*(x + i) == 100)

      *(y + 10) = ++count_100;

}

}

//输出x执行的内存空间中的n个数据

void arrayOut(int *x, int n) {

int m = 0;

if (n == 11) {

  printf("0-9\t10-19\t20-29\t30-39\t40-49\t50-59\t60-69\t70-79\t80-89\t90-99\t100\n");

  for (int i = 0; i n; i++) {

      printf("%d\t", *(x + i));

  }

} else {

  for (int i = 0; i n; i++) {

      m = m + 1;

      printf("%d\t", *(x + i));

      if (m % 10 == 0)

          printf("\n");

  }

}

printf("\n");

}

int main() {

int i, a[100], num[11] = {0};

funRand(a, 100); //产生随机数

funCount(a, 100, num); //统计

printf("产生的随机数为:\n");

arrayOut(a, 100); //输出数据

printf("各随机数区间分布:\n");

arrayOut(num, 11); //输出统计数据

return 0;

}


当前题目:c语言一百个函数,c语言100实例
网页网址:http://cdkjz.cn/article/hdjjdj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220