简体   繁体   中英

Why is second array writing stupid numbers? - C language

I have a little problem creating random numbers in 2 arrays. First array create random numbers fine, but the other one create always same numbers, although it sometimes change them for eg. 10 10 10 10 10 10 etc... but when I run program again it says 7 7 7 7 7 etc..

Here is program:

#include <stdio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
main()
{
    srand ( time(NULL) );
    int i,j,switchh,switching;
    int howmany = 10;
    int a[howmany],b[howmany];
    for (i=0;i<howmany;i++) {
        b[i] = rand() % 10+1;
    }
    while(1) {
        switchh=0;
        for (i=0; i<howmany-1;i++) {
            if (b[i]>b[i+1]) {
                int switching=b[i];
                b[i]=b[i+1];
                b[i+1]=switching;
                switchh = 1;
            }
        }
        if(switchh==0) {
            break;
        }
    }
    srand ( time(NULL) );
    for (j=0;j<howmany;j++) {
        a[j] = rand() % 10+1;
    }
    while(1) {
        switchh=0;
        for (j=0; j<howmany-1;j++) {
            if (a[j]>a[j+1]) {
                int switching=a[j];
                a[j]=a[j+1];
                a[j+1]=switching;
                switchh = 1;
            }
        }
        if(switchh==0) {
            break;
        }
    }
    for (j=0;j<howmany;j++) {
        printf("%d\n",a[i]);
    }
    for (i=0;i<howmany;i++) {
        printf("%d\n",b[i]);
    }
    return 0;    
}

1) Do not call srand ( time(NULL) ); second time. This restarts generator with the same numbers!!

2) a[j] = rand() % 10+1; The range is limited. If you can increase 10 to 100 or 1000.

3) Printout is wrong:

for (j=0;j<howmany;j++) {
    printf("%d\n",a[i]); // change to a[j];
}

Program:

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

int main()
{
    srand ( time(NULL) );

    int i,j,switchh,switching;
    int howmany = 10;
    int a[howmany],b[howmany];


    for (i=0;i<howmany;i++) {
        b[i] = rand() % 10 +1;
    }

    while(1) 
    {
        switchh=0;
        for (i=0; i<howmany-1;i++) {

            if (b[i]>b[i+1]) {

                int switching=b[i];
                b[i]=b[i+1];
                b[i+1]=switching;
                switchh = 1;
            }
        }

        if(switchh==0) {
            break;
        }
    }

   // srand ( time(NULL) );

    for (j=0;j<howmany;j++) {
        a[j] = rand() % 10+1;
    }

    while(1) {
        switchh=0;
        for (j=0; j<howmany-1;j++) {

            if (a[j]>a[j+1]) {

                int switching=a[j];
                a[j]=a[j+1];
                a[j+1]=switching;
                switchh = 1;
            }
        }
        if(switchh==0) {
            break;
        }
    }

    for (j=0;j<howmany;j++) {
        printf("%d\n",a[j]);
    }

    printf(" \n");

    for (i=0;i<howmany;i++) {
        printf("%d\n",b[i]);
    }

    return 0;    

}

Output:

1
2
2
3
5
6
7
8
8
9

3
3
4
5
6
6
8
8
9
9

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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