简体   繁体   English

随机数问题(C)

[英]Problems with random numbers (C)

Here is my code. 这是我的代码。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int i;
    int j;
    int x;
    int y[100];
    int b0 = 0, A0[100];

    srand(time(NULL));

    for (x = 0; x < 100; x ++)
    {
        y[x] = (rand()% 1000)+ 1;
    }
    int ans;

    for (x = 0; x < 100; x ++)
    {
        ans = y[x] % 10;

        if(x == 0)
        {
            printf("Frist group(Last for'0')\n");
        }
        if (ans == 0)
        {
            A0[b0] = y[x];
            b0++;
        }
    }

for (i = 0; i < b0; i ++)
{
    for (j = b0; j > i; j --)
    {
        if (A0[j - 1] > A0[j])
        {
            A0[j - 1] = A0[j - 1] ^ A0[j];
            A0[j] = A0[j - 1] ^ A0[j];
            A0[j - 1] = A0[j - 1] ^ A0[j];
        }
    }
}

    for (int count0 = 0; count0 <= b0; count0 ++)
    {
        printf(" %d  ", A0[count0]);
    }
    printf("\n\n");

    system("pause");
    return (0);
}

here is my output. 这是我的输出。

Frist group(Last for'0')
 32766   1000   840   630   900   500   830   520   80   470   510   760

I don't know why when I run this program every time, the frist number is always a strange integer. 我不知道为什么每次运行该程序时,第一个数字始终是一个奇怪的整数。

Can any one please help me, I want to know what is casuing this problem. 谁能帮助我,我想知道导致此问题的原因。

(I had forgot to upload the sort part of my codes.) (我忘了上传代码的排序部分。)

Thankyou very much. 非常感谢你。

The behaviour of your code is undefined . 您的代码的行为是不确定的

A0[j - 1] is outside the array bounds on at least one occasion. A0[j - 1]至少在一种情况下位于数组范围之外。

Also, your print loop stopping conditional needs to be count0 < b0 rather than count0 <= b0 . 此外,您的打印循环停止条件需要为count0 < b0而不是count0 <= b0

Everything else looks fine. 其他一切看起来都很好。

A0第一个整数不能奇怪,但最后一个整数可能是奇怪的,因为您正在读取超出范围的数据-循环中的条件应for (int count0 = 0; count0 < b0; count0 ++)

When I use malloc and free for the A0 and y arrays, valgrind on Linux provides the following output: 当我对A0y数组使用mallocfree ,Linux上的valgrind提供以下输出:

Frist group(Last for'0')
==9169== Conditional jump or move depends on uninitialised value(s)
==9169==    at 0x108972: main (foobar.c:40)
==9169== 
==9169== Conditional jump or move depends on uninitialised value(s)
==9169==    at 0x4E8737A: vfprintf (in /usr/lib/libc-2.26.so)
==9169==    by 0x4E8EAA5: printf (in /usr/lib/libc-2.26.so)
==9169==    by 0x108A24: main (foobar.c:51)
==9169== 
==9169== Use of uninitialised value of size 8
==9169==    at 0x4E8329B: _itoa_word (in /usr/lib/libc-2.26.so)
==9169==    by 0x4E86A0F: vfprintf (in /usr/lib/libc-2.26.so)
==9169==    by 0x4E8EAA5: printf (in /usr/lib/libc-2.26.so)
==9169==    by 0x108A24: main (foobar.c:51)

Notice the last line in each section: inside your main function at line 40, there is an "uninitialised value". 请注意每个部分的最后一行:在main函数的第40行中,有一个“未初始化的值”。 This means your program is trying to read a value from an address where you never stored/wrote a value. 这意味着您的程序正在尝试从从未存储/写入值的地址中读取值。 That's the if (A0[j - 1] > A0[j]) line. 那是if (A0[j - 1] > A0[j])行。 Since it's an array access, the problem is with the value j used in the loop. 由于它是数组访问,因此问题出在循环中使用的值j

You typed b0++ after writing to A0[b0] , so if b0 is 0 when you do A0[0] , then b0++ makes it 1. In other words, you have: 您在写入A0[b0]后输入了b0++ ,因此如果在执行A0[0]b0为0,则b0++使其变为1。换句话说,您拥有:

  • b0==1 : A0[0] b0==1A0[0]
  • b0==2 : A0[0] , A0[1] b0==2A0[0]A0[1]
  • b0==3 : A0[0] , A0[1] , A0[2] b0==3A0[0]A0[1]A0[2]
  • ... ...

This means you only wrote values from 0 to b0 - 1 when the first loop that assigns values to A0 is finished. 这意味着只有在第一个将值分配给A0循环完成时,才写入从0到b0 - 1值。 As a result, j = b0 means you're using A0[b0] , which has no value written to it. 结果, j = b0表示您正在使用A0[b0] ,没有任何值写入其中。 You're swapping a random number that you assigned at A0[b0 - 1] with an unknown number that you never assigned at A0[b0] . 您正在交换在A0[b0 - 1]处分配的随机数和从未在A0[b0]处分配的未知数。 Because you never assigned it, A0[b0] is "uninitialised". 因为您从未分配过,所以A0[b0]被“未初始化”。

The same issue with b0 occurs with count0 <= b0 in your print loop, which is why line 51 is also shown to be a problem. 在打印循环中, count0 <= b0 b0会发生与b0相同的问题,这就是为什么第51行也显示为问题。 Change it to count0 < b0 , and all issues with the array access of A0 are resolved. 将其更改为count0 < b0 ,可以解决A0所有数组访问问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM