c - 使用两个函数生成随机数数组
问题描述
我正在尝试生成一个数组,其元素是一个范围内的随机数(在最小值和最大值之间),但是出了点问题。代码就在下面。
void cria_aleatorio(int *vetor, int tamanho, int min, int max) {
int i=0;
for(i=0; i<tamanho; i++)
{
vetor[i] = (rand() % (max - min + 1)) + min;
}
}
void print_vetor(int *vetor, int tamanho){
printf("%d", vetor[tamanho]);
printf("%d", tamanho);
}
解决方案
正如评论中所指出的,您的 print_vetor 函数是错误的。您需要一个循环来遍历数组中的每个元素,就像您在生成随机数时所做的那样。
void print_vetor(int *vetor, int tamanho){
unsigned int i = 0;
for (i = 0; i < tamanho; i++) {
printf("%d, ", vetor[i]);
}
printf("\n%d\n", tamanho);
}
推荐阅读
- python - 使用 OpenCV cv2.VideoCapture 在 Python 中从 IP 摄像机进行视频流传输
- c# - 并行启动多个任务,每个任务都有自己的超时时间
- android - 在没有适当锁的情况下转储所有线程
- node.js - 使用 JWT 和本地存储安全访问路由
- java - 改造后。将 JSON 参数添加到每个 post 请求
- r - 将 r 中的月份从意大利语翻译成英语
- javascript - 文件下载 - 如何控制文件名并尊重用户偏好?
- java - 保存时Heroku请求超时代码H12
- ios - 侧栏菜单图标在基于选项卡栏的视图控制器中不可见
- spring-boot - 如何连接到不同 dc 中的多个 Cassandra