【发布时间】:2019-05-19 20:56:54
【问题描述】:
我想打印保存在数组中的 1000 个随机数。数字必须在 1 到 10000 之间。
我将srand(time(NULL)) 放入我的main 函数中,并且该数组必须在我的init 函数中填充随机数。 ausgabe 函数用于格式化输出。
但是rand 用数字填充我的数组。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ARR_SIZE 1000
void init(int * ptr, int size){
for (int i = 0; i < size; i++){
*(ptr + i) = (rand() %10000+1);
}
}
void ausgabe(int * ptr, int size){
for (int i = 0; i < size; i++){
printf("%5i\t", * ptr + i);
if ((i + 1) %10 == 0){
printf("\n");
}
}
printf("\n\n");
}
int main(){
int zufall[ARR_SIZE];
srand(time(NULL));
init(zufall, ARR_SIZE);
printf("\n\t\t\t---unsortierte Ausgabe---\n\n");
ausgabe(zufall, ARR_SIZE);
return 0;
}
【问题讨论】:
-
printf("%5i\t", * (ptr + i));
-
@DanByström 宁可:
ptr[i]. -
谢谢它现在正在工作。