-1

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.

KutengF
  • 51
  • 6

3 Answers3

3

The behaviour of your code is undefined.

A0[j - 1] is outside the array bounds on at least one occasion.

Also, your print loop stopping conditional needs to be count0 < b0 rather than count0 <= b0.

Everything else looks fine.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 1
    I find out that when i erase the +1 from (rand()% 1000)+1, The stange integer will turn into 0. – KutengF Nov 24 '17 at 12:03
  • Frist group(Last for'0') 0 310 360 560 580 830 880 Second group(Last for'1') 0 61 91 111 361 471 511 531 551 711 841 911 921 – KutengF Nov 24 '17 at 12:03
  • @KutengF What happens when you use the suggestion? In what way is it wrong? –  Nov 24 '17 at 12:15
  • @Chrone Kitsune Thanks for helping. Sorry, I think had forgot to upload the sort part of my codes.This "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]; } } }" would this be the thing that cause the problem?(this part is right after the separate part) – KutengF Nov 24 '17 at 12:38
2

First integer from A0 cannot be strange, but the last one can be, because you are reading data out of range - condition in loop should be for (int count0 = 0; count0 < b0; count0 ++).

rafix07
  • 20,001
  • 3
  • 20
  • 33
  • 1
    I disagree that `A0` cannot be strange. That's a manifestation of UB, and is quite rational (insofar that UB can be rational) given that the standard output is typically buffered. – Bathsheba Nov 24 '17 at 11:53
  • Thanks for helping :), but i've tried it. This isn't the cause of this problem. But when i erase the +1 from (rand()% 1000)+1 the strange integer will be gone but lefting a 0 in it's place. – KutengF Nov 24 '17 at 12:09
  • like this Frist group(Last for'0') 0 310 360 560 580 830 880 Second group(Last for'1') 0 61 91 111 361 471 511 531 551 711 841 911 921 – KutengF Nov 24 '17 at 12:09
1

When I use malloc and free for the A0 and y arrays, valgrind on Linux provides the following output:

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". 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. Since it's an array access, the problem is with the value j used in the loop.

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:

  • b0==1: A0[0]
  • b0==2: A0[0], A0[1]
  • b0==3: A0[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. As a result, j = b0 means you're using A0[b0], which has no value written to it. You're swapping a random number that you assigned at A0[b0 - 1] with an unknown number that you never assigned at A0[b0]. Because you never assigned it, A0[b0] is "uninitialised".

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. Change it to count0 < b0, and all issues with the array access of A0 are resolved.