What is wrong with my program?

T

Tadpole

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

Your suggestion is absolutely brilliant.
It exactly solves my problem with only just a few programming lines. As
shown below: I shuffle 20 times.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{ int A[]= {0,1,2,3,4};
srand (time(NULL));
int x,y,z;
for (int i=0; i<20 ;i++)
{ x =rand()%5;
y =rand ()%5;
printf ("%d %d \n",x,y);
z= A[x];
A[x]=A[y];
A[y]=z;
}
printf ("\n");
for (int i=0; i<5; i++)
printf ("A[%d]= %d\n",i,A);
return 0;
}

Is there one "include" statement that I can write to substitute the 3 "
#include " statement in the header?
Meaning #include < XXX.h > where XXX represent the statement I am
searching for?
Rgds,
Khoon
 
J

James Dow Allen

Your suggestion is absolutely brilliant.
It exactly solves my problem with  only just a few programming lines.  As
shown below:  I shuffle 20 times.
[excerpting code...]
 for (int i=0; i<20 ;i++)
 { x =rand()%5;
     y =rand ()%5;
        printf ("%d  %d \n",x,y);
        z= A[x];
     A[x]=A[y];
     A[y]=z;
 }

Except that the code you wrote is NOT the suggested code
(though it shares the general form "for ... rand ... swap"
and thus has a superficial similarity). That your version
approaches randomness (as number of "shuffles" increases)
while the Knuth/Fisher/Yates method is "perfect" with just
N-1 swaps is discussed to death twice a year or so in the
comp.programming ng.

The interesting question here, I think, is how you
followed the "absolutely brilliant" suggestion (which does
point to the ordinary correct code), and ended up with your
version?

BTW, your completed TSP program will need TWO shufflings:
the initialization, and the single-swaps for each step
in your hill-climbing. It appears you might be trying
to reuse the code for the latter to do the former.
Since the key Knuth/Fisher/Yates loop is just 4 lines
of code (see below), one wonders what's the point of
such a reuse.
for(i = 0; i < nvalues-1; i++) {
int c = randrange(nvalues-i);
int t = a; a = a[i+c]; a[i+c] = t; /* swap */
}


James Dow Allen
 
M

Morris Keesan

Here's another version. Notice the cast of the return value of time(). ....
#include <stdlib.h> ....
#include <time.h> ....
srand((unsigned)time(NULL));


Since you have #included <stdlib.h> and <time.h>, which provide proper
declarations for time(), srand(), and time_t, what's the point of adding
an extra (ugly, IMO) explicit cast for the argument to srand()?
srand() is declared with a prototype which says that its argument is an
(unsigned int), and time() is declared to return time_t, which is declared
in <time.h> as an arithmetic type, so the conversionm if necessary, to
(unsigned) will take place automatically. Why muddy up the code by doing
it manually?
 
B

blmblm

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.


(I hope the OP is still around and reading .... )

In my thinking the notion of "empty memory cell" is -- misleading,
though perhaps there are circumstances in which it's a meaningful
description.

My thinking is that all memory cells contain *something*, some
pattern of ones and zeros, but what those ones and zeros mean,
if indeed they mean anything, depends on the program that's
accessing them. Better to consider that a memory cell that hasn't
been explicitly set to some known value contains random junk.

(I await correction by the experts. :)? )
Thanks a zillion.
This alogrithm must be in a continuous while loop so that the if
statement has a chance to be fulfilled.

[ snip ]
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top