What is wrong with my program?

Discussion in 'C Programming' started by Tadpole, Feb 27, 2010.

  1. Tadpole

    Tadpole Guest

    Hi,
    I am trying to print 5 unique random numbers 0 to 4. But each time I
    print, I get funny result. What is wrong with my program? Please see
    below.
    Rgds
    Khoon

    //Result
    X[0]= 0
    X[1]= 3
    X[2]= 4208527
    X[3]= 4
    X[4]= 4208527

    Press any key to continue . . .

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

    int main()
    { int x=0;

    int X[5];

    srand (time(NULL));

    X[0] =rand()%5;

    x=rand()%5;
    if (x!=X[0])
    X[1]=x;

    x=rand()%5;
    if ((x!= X[0])&& ( x!=X[1]))
    X[2]=x;

    x=rand()%5;
    if ((x!= X[0]) && (x!=X[1])&& (x!= X[2]))
    X[3]=x;

    x=rand()%5;
    if ((x!= X[0]) && ( x!= X[1]) && ( x!=X[2]&& x!=X[3]))
    X[4]=x;

    for (int i=0; i<5 ; i++)
    printf ("X[%d]= %d \n",i, X);
    printf ("\n");
    return 0;
    }
    Tadpole, Feb 27, 2010
    #1
    1. Advertising

  2. Tadpole

    santosh Guest

    Tadpole <> writes:

    > Hi,
    > I am trying to print 5 unique random numbers 0 to 4. But each
    > time I
    > print, I get funny result. What is wrong with my program? Please
    > see below.
    > Rgds
    > Khoon
    >
    > //Result
    > X[0]= 0
    > X[1]= 3
    > X[2]= 4208527
    > X[3]= 4
    > X[4]= 4208527
    >
    > Press any key to continue . . .
    >
    > //Program
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <time.h>
    >
    > int main()
    > { int x=0;
    >
    > int X[5];
    >
    > srand (time(NULL));
    >
    > X[0] =rand()%5;
    >
    > x=rand()%5;
    > if (x!=X[0])
    > X[1]=x;
    >
    > x=rand()%5;
    > if ((x!= X[0])&& ( x!=X[1]))
    > X[2]=x;
    >
    > x=rand()%5;
    > if ((x!= X[0]) && (x!=X[1])&& (x!= X[2]))
    > X[3]=x;
    >
    > x=rand()%5;
    > if ((x!= X[0]) && ( x!= X[1]) && ( x!=X[2]&& x!=X[3]))
    > X[4]=x;
    >
    > for (int i=0; i<5 ; i++)
    > printf ("X[%d]= %d \n",i, X);
    > printf ("\n");
    > return 0;
    > }


    You're program is needlessly complex because you've avoided loops for
    some reason. The strange results you've observed are because you
    don't store any number when the return value of rand() is already
    equal to any previous element of X. Thus the printf loop prints
    whatever "random" value that happens to be in those cells. Here's
    another version. Notice the cast of the return value of time().

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <time.h>
    #define ARR_SIZE 5

    int occurs(int *arr, size_t n, int val) {
    size_t i;
    for (i = 0; i < n; i++)
    if (arr == val) return 1;
    return 0;
    }

    int main(void) {
    int arr[ARR_SIZE], num;
    size_t i;

    memset(arr, 0, sizeof arr);
    srand((unsigned)time(NULL));
    for (i = 0; i < ARR_SIZE; i++) {
    do {
    num = rand() % 5;
    } while (occurs(arr, i, num));
    arr = num;
    }
    for (i = 0; i < ARR_SIZE; i++)
    printf("arr[%zu] == %d\n", i, arr);
    return 0;
    }
    santosh, Feb 27, 2010
    #2
    1. Advertising

  3. Tadpole

    santosh Guest

    Tadpole <> writes:

    > Hi,
    > I am trying to print 5 unique random numbers 0 to 4. [...]


    Here's also a shorter program that meets your spec;-)

    #include <stdio.h>

    int main(void) {
    printf("0, 1, 2, 3, 4\n");
    return 0;
    }
    santosh, Feb 27, 2010
    #3
  4. Tadpole

    Ike Naar Guest

    In article <-september.org>,
    Richard <> wrote:
    >santosh <> writes:
    >> Here's also a shorter program that meets your spec;-)
    >> [snip]

    >
    >You really have been doing your best to enter the c.l.c arsehole clique
    >Santosh. Well done.


    You probably missed:
    - the smiley in Santosh' post
    - the fact that Santosh posted a helpful answer in a parallel subthread
    Ike Naar, Feb 27, 2010
    #4
  5. Tadpole

    Tadpole Guest

    Hi Santosh,
    You did not get my point.
    The numbers 0,1,2,3,4 must be in random sequence. The sequence order must
    be randomly generated.
    Your suggestion is a fixed sequence which is not correct.
    For example, it could be 23104 in one run or 34102 in another. All
    numbers must be unique. So 32110 is wrong because 1 is repeated.
    Thank you.
    Khoon


    "santosh" <> wrote in message
    news:hmaovr$iuj$-september.org...
    > Tadpole <> writes:
    >
    >> Hi,
    >> I am trying to print 5 unique random numbers 0 to 4. [...]

    >
    > Here's also a shorter program that meets your spec;-)
    >
    > #include <stdio.h>
    >
    > int main(void) {
    > printf("0, 1, 2, 3, 4\n");
    > return 0;
    > }
    >
    >
    Tadpole, Feb 27, 2010
    #5
  6. Tadpole

    santosh Guest

    Tadpole <> writes:

    > Hi Santosh,
    > You did not get my point.
    > The numbers 0,1,2,3,4 must be in random sequence. The sequence
    > order must be randomly generated.
    > Your suggestion is a fixed sequence which is not correct.
    > For example, it could be 23104 in one run or 34102 in another.
    > All
    > numbers must be unique. So 32110 is wrong because 1 is repeated.
    > Thank you.
    > Khoon
    >
    >
    > "santosh" <> wrote in message
    > news:hmaovr$iuj$-september.org...
    >> Tadpole <> writes:
    >>
    >>> Hi,
    >>> I am trying to print 5 unique random numbers 0 to 4. [...]

    >>
    >> Here's also a shorter program that meets your spec;-)
    >>
    >> #include <stdio.h>
    >>
    >> int main(void) {
    >> printf("0, 1, 2, 3, 4\n");
    >> return 0;
    >> }


    Yes, see my other post in the thread. That does provide a solution to
    your requirements I believe.
    santosh, Feb 27, 2010
    #6
  7. Tadpole

    Tadpole Guest

    >> "santosh" <> wrote in message
    >> news:hmaovr$iuj$-september.org...
    >>> Tadpole <> writes:
    >>>
    >>>> Hi,
    >>>> I am trying to print 5 unique random numbers 0 to 4. [...]
    >>>
    >>> Here's also a shorter program that meets your spec;-)
    >>>
    >>> #include <stdio.h>
    >>>
    >>> int main(void) {
    >>> printf("0, 1, 2, 3, 4\n");
    >>> return 0;
    >>> }

    >
    > Yes, see my other post in the thread. That does provide a solution to
    > your requirements I believe.
    >
    >

    ...// 1 st run
    arr[zu] == 0
    arr[zu] == 1
    arr[zu] == 2
    arr[zu] == 3
    arr[zu] == 4
    Press any key to continue . . .

    // 2nd run
    arr[zu] == 0
    arr[zu] == 1
    arr[zu] == 2
    arr[zu] == 3
    arr[zu] == 4
    Press any key to continue . . .

    Hi Santosh,
    Why zu? Why not array numbers 1,2,3 etc.
    Why the 1st run and 2 run gives identical results? I dont see the random
    sequence of the numbers.

    I was expecting
    arr[0] == 4
    arr[1] == 2
    arr[2] == 0
    etc
    etc

    Rgds,
    Khoon.
    Tadpole, Feb 27, 2010
    #7
  8. Tadpole

    santosh Guest

    Tadpole <> writes:

    >>> "santosh" <> wrote in message
    >>> news:hmaovr$iuj$-september.org...
    >>>> Tadpole <> writes:
    >>>>
    >>>>> Hi,
    >>>>> I am trying to print 5 unique random numbers 0 to 4. [...]
    >>>>
    >>>> Here's also a shorter program that meets your spec;-)
    >>>>
    >>>> #include <stdio.h>
    >>>>
    >>>> int main(void) {
    >>>> printf("0, 1, 2, 3, 4\n");
    >>>> return 0;
    >>>> }

    >>
    >> Yes, see my other post in the thread. That does provide a solution
    >> to your requirements I believe.
    >>
    >>

    > ..// 1 st run
    > arr[zu] == 0
    > arr[zu] == 1
    > arr[zu] == 2
    > arr[zu] == 3
    > arr[zu] == 4
    > Press any key to continue . . .
    >
    > // 2nd run
    > arr[zu] == 0
    > arr[zu] == 1
    > arr[zu] == 2
    > arr[zu] == 3
    > arr[zu] == 4
    > Press any key to continue . . .


    This cannot be the output from the source I posted. It shouldn't
    print "zu" but the array element number. Perhaps your compiler
    doesn't recognise %zu. I believe that might be the problem here.

    The zu conversion specifier for printf is to print values of type
    size_t. It was added with the C99 standard. If your compiler doesn't
    support them, as seems to be the case here, just change the %zu to
    %lu in printf and cast the corresponding argument to unsigned long.
    Here is a modified version. Try this and see if you still get these
    strange output.

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <time.h>
    #define ARR_SIZE 5

    int occurs(int *arr, size_t n, int val) {
    size_t i;
    for (i = 0; i < n; i++)
    if (arr == val) return 1;
    return 0;
    }

    int main(void) {
    int arr[ARR_SIZE], num;
    size_t i;

    memset(arr, 0, sizeof arr);
    srand((unsigned)time(NULL));
    for (i = 0; i < ARR_SIZE; i++) {
    do {
    num = rand() % 5;
    } while (occurs(arr, i, num));
    arr = num;
    }
    for (i = 0; i < ARR_SIZE; i++)
    printf("arr[%lu] == %d\n", (unsigned long)i, arr);
    return 0;
    }

    > Hi Santosh,
    > Why zu? Why not array numbers 1,2,3 etc.
    > Why the 1st run and 2 run gives identical results? I dont see the
    > random sequence of the numbers.
    >
    > I was expecting
    > arr[0] == 4
    > arr[1] == 2
    > arr[2] == 0
    > etc
    > etc
    >
    > Rgds,
    > Khoon.
    santosh, Feb 27, 2010
    #8
  9. Tadpole

    Tadpole Guest


    >
    > This cannot be the output from the source I posted. It shouldn't
    > print "zu" but the array element number. Perhaps your compiler
    > doesn't recognise %zu. I believe that might be the problem here.
    >
    > The zu conversion specifier for printf is to print values of type
    > size_t. It was added with the C99 standard. If your compiler doesn't
    > support them, as seems to be the case here, just change the %zu to
    > %lu in printf and cast the corresponding argument to unsigned long.
    > Here is a modified version. Try this and see if you still get these
    > strange output.
    >
    > #include <stdlib.h>
    > #include <stdio.h>
    > #include <string.h>
    > #include <time.h>
    > #define ARR_SIZE 5
    >
    > int occurs(int *arr, size_t n, int val) {
    > size_t i;
    > for (i = 0; i < n; i++)
    > if (arr == val) return 1;
    > return 0;
    > }
    >
    > int main(void) {
    > int arr[ARR_SIZE], num;
    > size_t i;
    >
    > memset(arr, 0, sizeof arr);
    > srand((unsigned)time(NULL));
    > for (i = 0; i < ARR_SIZE; i++) {
    > do {
    > num = rand() % 5;
    > } while (occurs(arr, i, num));
    > arr = num;
    > }
    > for (i = 0; i < ARR_SIZE; i++)
    > printf("arr[%lu] == %d\n", (unsigned long)i, arr);
    > return 0;
    > }
    >
    >


    arr[0] == 3
    arr[1] == 1
    arr[2] == 2
    arr[3] == 0
    arr[4] == 4
    Press any key to continue . . .


    Yes. Now it is working perfectly. Very very well.
    I am using Microsoft Visual Studio using the C++ platform to do my C.
    I am a beginner student on C in College.
    I am awed and amazed by the depth and breadth of your enormous knowledge
    in C programming.
    I will need sometime to spend digesting and learn from your program.
    100 more complex than my ealier program. Ha..ha..
    Thank you very very much for your great help.
    Rgds,
    khoon
    Tadpole, Feb 27, 2010
    #9
  10. Tadpole

    bartc Guest

    "santosh" <> wrote in message
    news:hmaovr$iuj$-september.org...
    > Tadpole <> writes:
    >
    >> Hi,
    >> I am trying to print 5 unique random numbers 0 to 4. [...]

    >
    > Here's also a shorter program that meets your spec;-)
    >
    > #include <stdio.h>
    >
    > int main(void) {
    > printf("0, 1, 2, 3, 4\n");
    > return 0;
    > }


    Here's one along similar lines, not shorter but almost as simple:

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

    int numbers[][5]={
    {0,1,2,3,4},
    {0,1,2,4,3},
    {0,1,3,2,4},
    {0,1,3,4,2},
    {0,1,4,2,3},
    {0,1,4,3,2},
    {0,2,1,3,4},
    {0,2,1,4,3},
    {0,2,3,1,4},
    {0,2,3,4,1},
    {0,2,4,1,3},
    {0,2,4,3,1},
    {0,3,1,2,4},
    {0,3,1,4,2},
    {0,3,2,1,4},
    {0,3,2,4,1},
    {0,3,4,1,2},
    {0,3,4,2,1},
    {0,4,1,2,3},
    {0,4,1,3,2},
    {0,4,2,1,3},
    {0,4,2,3,1},
    {0,4,3,1,2},
    {0,4,3,2,1},
    {1,0,2,3,4},
    {1,0,2,4,3},
    {1,0,3,2,4},
    {1,0,3,4,2},
    {1,0,4,2,3},
    {1,0,4,3,2},
    {1,2,0,3,4},
    {1,2,0,4,3},
    {1,2,3,0,4},
    {1,2,3,4,0},
    {1,2,4,0,3},
    {1,2,4,3,0},
    {1,3,0,2,4},
    {1,3,0,4,2},
    {1,3,2,0,4},
    {1,3,2,4,0},
    {1,3,4,0,2},
    {1,3,4,2,0},
    {1,4,0,2,3},
    {1,4,0,3,2},
    {1,4,2,0,3},
    {1,4,2,3,0},
    {1,4,3,0,2},
    {1,4,3,2,0},
    {2,0,1,3,4},
    {2,0,1,4,3},
    {2,0,3,1,4},
    {2,0,3,4,1},
    {2,0,4,1,3},
    {2,0,4,3,1},
    {2,1,0,3,4},
    {2,1,0,4,3},
    {2,1,3,0,4},
    {2,1,3,4,0},
    {2,1,4,0,3},
    {2,1,4,3,0},
    {2,3,0,1,4},
    {2,3,0,4,1},
    {2,3,1,0,4},
    {2,3,1,4,0},
    {2,3,4,0,1},
    {2,3,4,1,0},
    {2,4,0,1,3},
    {2,4,0,3,1},
    {2,4,1,0,3},
    {2,4,1,3,0},
    {2,4,3,0,1},
    {2,4,3,1,0},
    {3,0,1,2,4},
    {3,0,1,4,2},
    {3,0,2,1,4},
    {3,0,2,4,1},
    {3,0,4,1,2},
    {3,0,4,2,1},
    {3,1,0,2,4},
    {3,1,0,4,2},
    {3,1,2,0,4},
    {3,1,2,4,0},
    {3,1,4,0,2},
    {3,1,4,2,0},
    {3,2,0,1,4},
    {3,2,0,4,1},
    {3,2,1,0,4},
    {3,2,1,4,0},
    {3,2,4,0,1},
    {3,2,4,1,0},
    {3,4,0,1,2},
    {3,4,0,2,1},
    {3,4,1,0,2},
    {3,4,1,2,0},
    {3,4,2,0,1},
    {3,4,2,1,0},
    {4,0,1,2,3},
    {4,0,1,3,2},
    {4,0,2,1,3},
    {4,0,2,3,1},
    {4,0,3,1,2},
    {4,0,3,2,1},
    {4,1,0,2,3},
    {4,1,0,3,2},
    {4,1,2,0,3},
    {4,1,2,3,0},
    {4,1,3,0,2},
    {4,1,3,2,0},
    {4,2,0,1,3},
    {4,2,0,3,1},
    {4,2,1,0,3},
    {4,2,1,3,0},
    {4,2,3,0,1},
    {4,2,3,1,0},
    {4,3,0,1,2},
    {4,3,0,2,1},
    {4,3,1,0,2},
    {4,3,1,2,0},
    {4,3,2,0,1},
    {4,3,2,1,0}};

    int main(void)
    {
    int *X;

    srand (time(NULL));
    X=numbers[rand()%120];

    printf ("X = (%d,%d,%d,%d,%d)\n",X[0],X[1],X[2],X[3],X[4]);
    return 0;
    }

    --
    Bartc
    bartc, Feb 27, 2010
    #10
  11. "Tadpole" <> writes:

    > I am trying to print 5 unique random numbers 0 to 4.


    The best way to phrase this is that you want to generate a random
    permutation of the numbers 0 to 4.

    <snip code>
    You've had your problem explained and a solution proposed, but there
    is a simpler solution which is to put 0 to 4 into your array and use
    random numbers to shuffle the elements:

    http://en.wikipedia.org/wiki/Fisher–Yates_shuffle

    --
    Ben.
    Ben Bacarisse, Feb 27, 2010
    #11
  12. Tadpole

    santosh Guest

    Tadpole <> writes:
    <snip>

    > Yes. Now it is working perfectly. Very very well.
    > I am using Microsoft Visual Studio using the C++ platform to do my
    > C.


    And MS has very poor support for C99 it seems. If you have time, you
    might also investigate alternative compilers for Windows like
    Cygwin/MinGW, PellesC, lcc-win32 and so on. Most of these support
    most of C99, so are better than Visual C in this aspect.

    > I am a beginner student on C in College.
    > I am awed and amazed by the depth and breadth of your enormous
    > knowledge in C programming.
    > I will need sometime to spend digesting and learn from your
    > program.
    > 100 more complex than my ealier program. Ha..ha..


    Thanks for your praises, but you've massively overestimated my skill
    in C. Compared to many of the participants in this group, I'm sure we
    both would seem like beginners.

    So this is a very good place to learn the subtler aspects of C, and
    programming in general. You'll get insights here that you're unlikely
    to see in most C books.

    > Thank you very very much for your great help.
    > Rgds,
    > khoon


    You're welcome.
    santosh, Feb 27, 2010
    #12
  13. Tadpole

    santosh Guest

    Richard <> writes:

    > santosh <> writes:
    >
    >> Tadpole <> writes:
    >>
    >>> Hi,
    >>> I am trying to print 5 unique random numbers 0 to 4. [...]

    >>
    >> Here's also a shorter program that meets your spec;-)
    >>
    >> #include <stdio.h>
    >>
    >> int main(void) {
    >> printf("0, 1, 2, 3, 4\n");
    >> return 0;
    >> }
    >>

    >
    > You really have been doing your best to enter the c.l.c arsehole
    > clique Santosh. Well done.


    Thank you master. I seek only to serve you;-)
    santosh, Feb 27, 2010
    #13
  14. Tadpole

    Tadpole Guest

    "santosh" <> wrote in message
    news:hmaoec$iuj$-september.org...
    > Tadpole <> writes:
    >
    >> Hi,
    >> I am trying to print 5 unique random numbers 0 to 4. But each
    >> time I
    >> print, I get funny result. What is wrong with my program? Please
    >> see below.
    >> Rgds
    >> Khoon
    >>
    >> //Result
    >> X[0]= 0
    >> X[1]= 3
    >> X[2]= 4208527
    >> X[3]= 4
    >> X[4]= 4208527
    >>
    >> Press any key to continue . . .
    >>
    >> //Program
    >> #include <stdio.h>
    >> #include <stdlib.h>
    >> #include <time.h>
    >>
    >> int main()
    >> { int x=0;
    >>
    >> int X[5];
    >>
    >> srand (time(NULL));
    >>
    >> X[0] =rand()%5;
    >>
    >> x=rand()%5;
    >> if (x!=X[0])
    >> X[1]=x;
    >>
    >> x=rand()%5;
    >> if ((x!= X[0])&& ( x!=X[1]))
    >> X[2]=x;
    >>
    >> x=rand()%5;
    >> if ((x!= X[0]) && (x!=X[1])&& (x!= X[2]))
    >> X[3]=x;
    >>
    >> x=rand()%5;
    >> if ((x!= X[0]) && ( x!= X[1]) && ( x!=X[2]&& x!=X[3]))
    >> X[4]=x;
    >>
    >> for (int i=0; i<5 ; i++)
    >> printf ("X[%d]= %d \n",i, X);
    >> printf ("\n");
    >> return 0;
    >> }

    >
    > You're program is needlessly complex because you've avoided loops for
    > some reason. The strange results you've observed are because you
    > don't store any number when the return value of rand() is already
    > equal to any previous element of X. Thus the printf loop prints
    > whatever "random" value that happens to be in those cells. Here's
    > another version. Notice the cast of the return value of time().



    >because you
    > don't store any number when the return value of rand() is already
    > equal to any previous element of X


    Why not stored?
    In my program, everytime when x is generated a random number that
    qualifies the if statement, it is stored as X = x:;
    In the first instance it is stored in X[0] then the next instance X[1] =
    x;
    Why is the x value generated not stored? How to make the program that
    it stores permanently and not disappears whcn it comes to the printf
    statement? Causing the printf statement to print something else?
    Rgds,
    Khoon.
    Tadpole, Feb 27, 2010
    #14
  15. Tadpole

    Tadpole Guest


    >
    > int main(void)
    > {
    > int *X;
    >
    > srand (time(NULL));
    > X=numbers[rand()%120];
    >
    > printf ("X = (%d,%d,%d,%d,%d)\n",X[0],X[1],X[2],X[3],X[4]);
    > return 0;
    > }
    >
    > --
    > Bartc


    Hi Bartc,
    Your method is good and workable. But not practical.
    It is OK for 5 numbers. But what if it is 25 numbers?

    My college assignment is to solve the Travelling Saleman Problem using the
    Hill Climbing algorithm.
    There are 25 cities. So eventually, I have to deal with 25 numbers.
    The salesman will need to travel at random through 25 cities and back.
    I am developing the algorithm working from first principle starting from
    the basic.
    Thanks for your suggestion.
    Most appreciate.
    Rgds
    Khoon.
    Tadpole, Feb 27, 2010
    #15
  16. Tadpole

    Tadpole Guest


    >>>
    >>>You're program is needlessly complex because you've avoided loops for
    >>>some reason. The strange results you've observed are because you
    >>>don't store any number when the return value of rand() is already
    >>>equal to any previous element of X. Thus the printf loop prints
    >>>whatever "random" value that happens to be in those cells. Here's
    >>>another version. Notice the cast of the return value of time().

    >>
    >>
    >>
    >>>because you
    >>>don't store any number when the return value of rand() is already
    >>>equal to any previous element of X

    >>
    >>
    >> Why not stored?
    >> In my program, everytime when x is generated a random number that
    >> qualifies the if statement, it is stored as X = x:;

    >
    > What happens when
    > when x is generated a random number that
    > *doesn't* qualifies the if statement?


    OH YES !! Now I get it.
    The memory cell is empty because nothing is stored there because the if
    statement is not fulfilled.
    Thanks a zillion.
    This alogrithm must be in a continuous while loop so that the if
    statement has a chance to be fulfilled.

    Rgds,
    khoon.
    Tadpole, Feb 27, 2010
    #16
  17. Tadpole

    bartc Guest

    "Tadpole" <> wrote in message
    news:4b891a74$...

    > Your method is good and workable. But not practical.
    > It is OK for 5 numbers. But what if it is 25 numbers?


    No problem, just use:

    int numbers[][25]={
    {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24},
    {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,24,23},
    {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,22,24},
    {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,22},
    {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,24,22,23},
    {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,24,23,22},
    <snip 15,511,210,043,330,985,983,999,994 further lines>

    No, you should use some random shuffle method in this case (already
    mentioned in the thread). Although I don't know how that would apply to your
    travelling salesman problem.

    --
    bartc
    bartc, Feb 27, 2010
    #17
  18. Tadpole

    Tadpole Guest


    > void
    > shuffle(int *array, int n)
    > {
    > int i, r;
    >
    > array[0] = 0;
    > for (i = 1; n > i; ++i) {
    > r = rand() % (i + 1);
    > array = 0;
    > array = array[r];
    > array[r] = i;
    > }
    > }
    >
    > /* END new.c */
    >
    > --
    > pete



    Thanks Pete,
    However you forgot to put in srand..
    I have added it and now the program runs perfectily.
    I will need some time to run through the algorithm.
    There are a few commands new to me.
    Rgds
    Khoon.
    Tadpole, Feb 27, 2010
    #18
  19. Tadpole

    Tadpole Guest

    > No problem, just use:
    >
    > int numbers[][25]={
    > {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24},
    > {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,24,23},
    > {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,22,24},
    > {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,22},
    > {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,24,22,23},
    > {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,24,23,22},
    > <snip 15,511,210,043,330,985,983,999,994 further lines>
    >
    > No, you should use some random shuffle method in this case (already
    > mentioned in the thread). Although I don't know how that would apply to
    > your travelling salesman problem.
    >
    > --
    > bartc

    Bart
    The assingment is to solve the travelling salesman problem using the
    simulated annealing methodology by the Hill Climbing Method.
    Please see http://www.autonlab.org/tutorials/hillclimb.html

    I am thinking for the annealing part I will begin the analysis by shuffling
    all the cities. So the salesman keeps travelling randomly through all the
    cities and work out the loop having the shortest distance he can discover.
    This is important because the distance from city 0 to city 1 is different
    from city 1 to city 0. Then gradually he refines his loop by exchanging
    cities to work out the Local Maxima or Local Peak in his Hill Climbing.
    As this is not using "brute force method" we may not get the Global Maxima.
    This is OK because the assignment ask for 1000 loops by the Saleman. The
    assignment is also is a test on the programming skill other than solving the
    Saleman problem.
    Rgds,
    Khoon
    Tadpole, Feb 27, 2010
    #19
  20. Tadpole

    Ike Naar Guest

    In article <4b88ca69$>, Tadpole <> wrote:
    >Hi,
    >I am trying to print 5 unique random numbers 0 to 4. But each time I
    >print, I get funny result. What is wrong with my program? Please see
    >below.


    You might also have a look at clc faq question 13.19:

    http://c-faq.com/lib/shuffle.html
    Ike Naar, Feb 27, 2010
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VisionSet

    what's wrong in this program

    VisionSet, Sep 24, 2004, in forum: Java
    Replies:
    12
    Views:
    547
    Thomas G. Marshall
    Sep 28, 2004
  2. Replies:
    7
    Views:
    415
  3. Matthew
    Replies:
    7
    Views:
    665
    Priscilla Walmsley
    Jan 7, 2005
  4. jose luis fernandez diaz

    why this program is wrong on Tru64 OS ?

    jose luis fernandez diaz, Nov 25, 2003, in forum: C++
    Replies:
    2
    Views:
    418
    Stephan Br?nnimann
    Nov 26, 2003
  5. Replies:
    5
    Views:
    1,587
Loading...

Share This Page