为什么将值写入数组时会出现错误?
我试图确定为什么将随机值写入数组会导致问题。
我实际上要求 rand()
生成 1 到 10 之间的数字(rand() %10 +1
, with srand(time(NULL))
code> 之前)并且第一个值始终高于 10:它也是一个随机值,介于 10 和 20 之间。我真的不知道如何解决这个问题,因为它看起来像是rand
和 srand
函数。尽管如此,这是我的代码:
编辑:现在正确的代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZEA 100
#define SIZEFREQ 10
int main()
{
int a[SIZEA]={0},frequency[SIZEFREQ]={0};
int i,temp,gothrough;
srand(time(NULL));
for(i=0;i<=SIZEA-1;i++)
{
a[i]=rand() %10 +1;
++frequency[a[i]-1];
}
printf("These are the elements in the vector:\n");
for(i=0;i<=SIZEA-1;i++)
{
printf("%3d,",a[i]);
}
printf("\nLet's try to put them in order\n");
for(gothrough=0;gothrough<=SIZEA-1;gothrough++)
{
for(i=0;i<=SIZEA-2;i++)
{
if (a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
for(i=0;i<=SIZEA-1;i++)
{
printf("%3d,",a[i]);
}
printf("\n\nValue Frequency\n");
for(i=0;i<=SIZEFREQ-1;i++)
{
printf("%5d%10d\n",i+1,frequency[i]);
}
return 0;
}`
I am trying to determine why writing random values to an array is causing a problem.
I actually ask rand()
to generate numbers between 1 and 10(rand() %10 +1
, with srand(time(NULL))
before) and the first value is ALWAYS higher than 10: it's a random one too, between 10 and 20. I really do not know how to fix that, as it looks like an issue with the rand
and srand
functions. Nevertheless this is my code:
Edit: correct code, now
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZEA 100
#define SIZEFREQ 10
int main()
{
int a[SIZEA]={0},frequency[SIZEFREQ]={0};
int i,temp,gothrough;
srand(time(NULL));
for(i=0;i<=SIZEA-1;i++)
{
a[i]=rand() %10 +1;
++frequency[a[i]-1];
}
printf("These are the elements in the vector:\n");
for(i=0;i<=SIZEA-1;i++)
{
printf("%3d,",a[i]);
}
printf("\nLet's try to put them in order\n");
for(gothrough=0;gothrough<=SIZEA-1;gothrough++)
{
for(i=0;i<=SIZEA-2;i++)
{
if (a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
for(i=0;i<=SIZEA-1;i++)
{
printf("%3d,",a[i]);
}
printf("\n\nValue Frequency\n");
for(i=0;i<=SIZEFREQ-1;i++)
{
printf("%5d%10d\n",i+1,frequency[i]);
}
return 0;
}`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
原因很简单。
a[i]
介于 1 和 10 之间,因此当您写入时:您正在填充
频率
的索引 2 到 11。但是,频率只有索引 0 到 10。因此,您将遍历数组频率并进入数组 a 并写入一个[0]
。当a[i]
为 10 时,就会发生这种情况。由于有 100 个数字,有 10% 的机会得到 10,因此您递增a[0]
(通过递增频率[11]
)大约10次。由于第一个值也在 1 到 10 之间,因此最终值在 10 到 20 之间。编辑:出于同样的原因,您将
a
索引从 0 到SIZE -1
,您还应该将频率
索引为从0到10。您所做的就是创建从1到10的索引,并且还为它们+1!例如这里:应该
注意从 0 开始,而不是到 10,并且通过
i
而不是i+1
索引频率
或者,您可以
通过
i-1
对频率进行索引,以使索引正确。The reason is simple.
a[i]
is between 1 and 10 and therefore when you write:you are filling indices 2 to 11 of
frequency
. However,frequency
has only indices 0 to 10. Therefore you are going over the arrayfrequency
and into the arraya
and writing overa[0]
. This happens whena[i]
is 10. Since with 100 numbers, there is 10% chance you get 10, you incrementa[0]
(by incrementingfrequency[11]
) about 10 times. Since the first value was also between 1 and 10, the final value gets between 10 and 20.Edit: For the same reason you index
a
from 0 toSIZE-1
, you should also indexfrequency
from 0 to 10. What you are doing is to create indices from 1 to 10, and also +1 them! For example this here:should be
Notice both starting from 0, not going to 10, and indexing
frequency
byi
rather thani+1
Alternatively, you could have
that indexes
frequency
byi-1
to make the index right.我编译了你的代码,它生成了 1 到 10 之间的值。使用
%
时没有办法超过范围 0-9(或 1 - 10 +1),因为一旦达到10,这意味着/
会高 1 并且模数必须从 0 重新开始。编辑:你不能将 30 除以 10 并说它是 2 余数10,因为该余数将转换为 10 的 +1,即 30 除以 10 = 3,余数为 0。I compiled your code and it generates between 1 and 10. There is NO way to get over range 0-9 (or 1 - 10 with +1) with
%
used, since as soon as it would reach 10, that would mean that the/
would be 1 higher AND modulo would have to start again at 0. EDIT: You can't divide 30 by 10 and say it's 2 with a remainder 10, since this remainder would translate to +1 of 10ths, that is, 30 divided by 10 = 3 and remainder 0.