为什么将值写入数组时会出现错误?

发布于 2024-12-18 01:22:08 字数 1241 浏览 0 评论 0原文

我试图确定为什么将随机值写入数组会导致问题。

我实际上要求 rand() 生成 1 到 10 之间的数字(rand() %10 +1, with srand(time(NULL)) code> 之前)并且第一个值始终高于 10:它也是一个随机值,介于 10 和 20 之间。我真的不知道如何解决这个问题,因为它看起来像是randsrand 函数。尽管如此,这是我的代码:

编辑:现在正确的代码

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小清晰的声音 2024-12-25 01:22:08

原因很简单。

a[i] 介于 1 和 10 之间,因此当您写入时:

++frequency[a[i]+1];

您正在填充 频率 的索引 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!例如这里:

for(i=1;i<=10;i++)
    printf("%5d%10d\n",i,frequency[i+1]);

应该

for(i=0;i<10;i++)
    printf("%5d%10d\n",i,frequency[i]);

注意从 0 开始,而不是到 10,并且通过 i 而不是 i+1 索引频率

或者,您可以

for(i=1;i<=10;i++)
    printf("%5d%10d\n",i,frequency[i-1]);

通过 i-1 对频率进行索引,以使索引正确。

The reason is simple.

a[i] is between 1 and 10 and therefore when you write:

++frequency[a[i]+1];

you are filling indices 2 to 11 of frequency. However, frequency has only indices 0 to 10. Therefore you are going over the array frequency and into the array a and writing over a[0]. This happens when a[i] is 10. Since with 100 numbers, there is 10% chance you get 10, you increment a[0] (by incrementing frequency[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 to SIZE-1, you should also index frequency from 0 to 10. What you are doing is to create indices from 1 to 10, and also +1 them! For example this here:

for(i=1;i<=10;i++)
    printf("%5d%10d\n",i,frequency[i+1]);

should be

for(i=0;i<10;i++)
    printf("%5d%10d\n",i,frequency[i]);

Notice both starting from 0, not going to 10, and indexing frequency by i rather than i+1

Alternatively, you could have

for(i=1;i<=10;i++)
    printf("%5d%10d\n",i,frequency[i-1]);

that indexes frequency by i-1 to make the index right.

温折酒 2024-12-25 01:22:08

我编译了你的代码,它生成了 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文