What is wrong with my program?

T

Tadpole

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;
}
 
S

santosh

Tadpole said:
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;
}
 
S

santosh

Tadpole said:
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;
}
 
I

Ike Naar

santosh said:
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
 
T

Tadpole

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 said:
Tadpole said:
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;
}
 
S

santosh

Tadpole said:
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 said:
Tadpole said:
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.
 
T

Tadpole

santosh said:
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.
 
S

santosh

Tadpole said:
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.
 
T

Tadpole

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
 
B

bartc

santosh said:
Tadpole said:
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;
}
 
S

santosh

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.
 
S

santosh

Richard said:
santosh said:
Tadpole said:
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;-)
 
T

Tadpole

santosh said:
Tadpole said:
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.
 
T

Tadpole

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;
}

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.
 
T

Tadpole

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.
 
B

bartc

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.
 
T

Tadpole

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 */



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.
 
T

Tadpole

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.
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top