Note that there are some explanatory texts on larger screens.

plurals
  1. POassignment of a value to a value changing in for loop
    text
    copied!<p>in a previous question i had a problem in some code,, after revealed, i had other problem in the code:</p> <pre><code>i=1; double smallest=diff[i][2]; int nxtcty; for (j=2; j&lt;=n; j++) { if (j!=i &amp;&amp; diff[i][j]&lt;smallest) {smallest=diff[i][j]; nxtcty=j;} } </code></pre> <p>the value nxtcty is not changing to j.. it outputs a strange number which i believe it is the memory location content for it >> '134520820'.. what went wrong?? thank you in advance...</p> <p>EDIT for further debugging, here the full code.. it is a very beginner code to calculate distance between several cities with their (x,y) coordinates given:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;math.h&gt; int main() { int i, j, n; ////////////// getting the number of cities /////////////////// printf("how many cities are there?\n"); scanf("%d", &amp;n); printf("so there is %d cities\n", n); int x[n]; int y[n]; ///////////// getting the x coordinates ////////////////////// printf("enter their x coordinates, each followed by return\n"); for (i=0; i&lt;n; i++) { scanf("%d", &amp;x[i]); printf("the %dth city x coordinate is %d\n", i+1, x[i]); } ///////////// getting the y cordinates ///////////////////// printf("enter their y coordinates, each followed by return\n"); for (i=0; i&lt;n; i++) { scanf("%d", &amp;y[i]); printf("the %dth city y coordinate is %d\n", i+1, y[i]); } ////////////// showing information /////////////////////// for (i=0; i&lt;n; i++) { printf("city number %d is at (%d,%d)\n", i+1, x[i], y[i]); } //////////// get the distances ///////////////// double diff[n][n]; double h; for (i=0; i&lt;n; i++) { for (j=0; j&lt;n; j++) { h=((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])); printf("city #%d distance %lf from city #%d\n\n", i+1, sqrt(h), j+1); diff[i][j]=sqrt(h); } } for (i=0; i&lt;n; i++) {for (j=0; j&lt;n; j++) { printf("%lf ", diff[i][j]); if (j!=0 &amp;&amp; n % (j+1)==0){printf("\n");}} } i=0; double smallest=diff[i][1]; printf("smallest_initial %lf \n",smallest); int nxtcty=77; printf("nxtcty_initial %d\n",nxtcty); for (j=1; j&lt;=n; j++) { printf("j_b4_if: %d, smallest_b4_if: %lf, nxtcty_b4_if: %d\n",j, smallest, nxtcty); if ( diff[i][j]&lt;smallest ) {smallest=diff[i][j]; nxtcty=j; printf("j_in_if: %d, smallest_in_if: %lf, nxtcty_in_if: %d\n",j, smallest, nxtcty); } printf("j_in_for: %d, smallest_in_for: %lf, nxtcty_in_for: %d\n",j, smallest, nxtcty); } printf("j %d,, nxtcty %d\n", j, nxtcty); return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload