当前位置:网站首页>C language 100 question set 004 - statistics of the number of people of all ages

C language 100 question set 004 - statistics of the number of people of all ages

2020-11-06 01:14:53 Johngo.

Series articles 《C Language classics 100 example 》 Continuous creation , Welcome your attention and support .

Like the students remember to like 、 forward 、 Collection ~

follow-up C Language classics 100 Examples will be pdf And code forms to official account.

Welcome to your attention : Computational advertising ecology Check immediately

1 subject

function :fun()

function : Count the number of people of all ages

describe :
N The age is obtained by calling a random function , And stored in the age in
The function is required to 0-9 In the age group d[0] in , hold 10-19 In the age group d[1] in , By analogy . hold 100 Age and above d[10] in
The result is output in the main function

2 Ideas

Random use rand() function , The header file is #include <stdlib.h>

rand() Function generates integers in a specified order , Therefore, the same two values are printed each time the above statement is executed , So C The randomness of language is not the real randomness , Sometimes called pseudo-random numbers , Use rand() Before generating random numbers, we need to use the initialization function of the random generator srand(unsigned seed)( Also located in stdlib.h in ) Initialize the pseudo-random number sequence ,seed Also called random seed , Generally speaking, it is , If provided each time seed The same thing , Finally, several random values generated in each round are the same , So it's called pseudo-random number , So you need to provide different seed To achieve complete randomness , We usually use time functions time(NULL) As seed , Because the time values are different every second , But it can't be used in this question time This tool

3 Code

#include <stdio.h> 
#include <stdlib.h>
#define M 11
#define N 100

/**
 function :fun()
 function : Count the number of people of all ages 
 describe :
N The age is obtained by calling a random function , And stored in the age in 
 The function is required to 0-9 In the age group d[0] in , hold 10-19 In the age group d[1] in , By analogy . hold 100 Age and above d[10] in 
 The result is output in the main function 
**/

void fun(int *age, int *d) {
	for (int i = 0; i < N; ++i) {
		if (*(age+i)<100) {
			d[(*(age+i))/10] += 1;
		} else {
			d[M-1] += 1;
		}
	}
}

int main(int argc, char const *argv[]) { 
	int age[N];		// 100 Users 
	int d[M]={0}; 		// 11 Age groups 
	for (int i = 0; i < N; ++i) {
		*(age+i) = rand()%121;	//  What is the age range 0-120
	}
	fun(age, d);
	printf(" Number of people by age :\n");
	for (int i = 0; i < M; ++i) {
		printf("%d ", d[i]);
	}
	printf("\n");
}

Sample results :

$ gcc ex004.c -o demo
$ ./demo
 Number of people by age :
10 9 8 4 10 8 7 7 6 11 20

-- END --

Please remember to like this article 、 forward 、 Collection ~

More , Welcome to our official account. : Computational advertising ecology

follow-up C Language classics 100 Examples will be pdf And code forms to official account.

It also brings more articles and dry goods !

版权声明
本文为[Johngo.]所创,转载请带上原文链接,感谢