我的代码应生成5个数组。
但是,在VisualStudio 2k8和2k12上,第一个数组为0,第三个数值远大于( - )20,第五个数组的偶数小于0。
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include "winbgi2.h"
int s, w;
int i;
int x[20], y[20], vx[20], vy[20], m[20];
void init(int a[20], int b[20], int c[20], int d[20], int e[20]);
void main() {
srand((unsigned) time(NULL));
int min;
int max;
min = 100;
max = 1000;
s = min + rand() % max;
w = min + rand() % max;
graphics(s, w);
init(&x[20], &y[20], &vx[20], &vy[20], &m[20]);
for (i = 1; i <= 20; i++) {
printf("Array element x=%ld \t is equal to %ld\n", i, x[i]);
};
for (i = 1; i <= 20; i++) {
printf("Array element y=%ld \t is equal to %ld\n", i, y[i]);
};
for (i = 1; i <= 20; i++) {
printf("Array element vx=%ld \t is equal to %ld\n", i, vx[i]);
};
for (i = 1; i <= 20; i++) {
printf("Array element vy=%ld \t is equal to %ld\n", i, vy[i]);
};
for (i = 1;i <= 20; i++) {
printf("Mass of ball m=%ld \tis %ld\n", i, m[i]);
};
wait();
}
void init(int a[20], int b[20], int c[20], int d[20], int e[20]) {
for (i = 1; i <= 20; i++)
a[i] = rand() % s;
for (i = 1; i <= 20; i++)
b[i] = rand() % w;
for (i = 1; i <= 20; i++)
c[i] =- 20 + rand() % 40;
for (i = 1; i <= 20; i++)
d[i] =- 20 + rand() % 40;
for (i = 1; i <= 20; i++)
e[i] = i * i * 2 + 1;
}
我感到茫然:为什么这段代码一次性工作不好,下次一切都很好。