c语言学习--数组篇

数组声明并生成随机数赋值

要求生成的随机数的范围在20-50之间

#include <stdio.h>
#include <time.h>

void main(){
	/**
	数组声明并初始化赋值

	生成随机数范围在20-50之间
	*/
	time_t ts;
	unsigned int times = time(&ts);
	srand(times);

	int nums[20];
	int random;
	int length = sizeof nums/sizeof nums[0];
	for (int i = 0; i < length; i++){
		random = rand()%10;
		nums[i] = 20+3*random;
	}

	for (int j = 0; j < length; j++){
		printf("%d\n",nums[j]);
	}
	getchar();
}

【注】如何确定一个数组中含有多少个元素?

 sizeof是c/c++中的一个操作符,其作用是返回一个对象或者类型所占内存的字节数;

猜你喜欢

转载自www.cnblogs.com/liwuming/p/10197492.html