Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6093241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:32:15+00:00 2026-05-23T12:32:15+00:00

in a previous question i had a problem in some code,, after revealed, i

  • 0

in a previous question i had a problem in some code,, after revealed, i had other problem in the code:

i=1;
double smallest=diff[i][2];
int nxtcty;
for (j=2; j<=n; j++)
        {
          if (j!=i && diff[i][j]<smallest) {smallest=diff[i][j]; nxtcty=j;}
        }

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…

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:

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

int main()
        {
          int i, j, n;
////////////// getting the number of cities ///////////////////
                printf("how many cities are there?\n");
                scanf("%d", &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<n; i++)
                        {
                          scanf("%d", &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<n; i++)
                        {
                          scanf("%d", &y[i]);
                          printf("the %dth city y coordinate is %d\n", i+1, y[i]);
                        }
////////////// showing information ///////////////////////
                for (i=0; i<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<n; i++) {
        for (j=0; j<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<n; i++) {for (j=0; j<n; j++)
{ printf("%lf  ", diff[i][j]);
if (j!=0 && 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<=n; j++)
        {
          printf("j_b4_if: %d,  smallest_b4_if:  %lf, nxtcty_b4_if:   %d\n",j, smallest, nxtcty);
          if ( diff[i][j]<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;
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:32:15+00:00Added an answer on May 23, 2026 at 12:32 pm

    If would be a good idea to initialise nxtcty to 2 so that if diff[i][2] really is the smallest then will be the index of the smallest and not still uninitialised.

    If you think that an assignment should be taking place, try printing out diff[i][j] each time round the loop to check.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my previous question on this portal, I had asked about some insight about
This is a follow-up to a previous question I had about interfaces. I received
I was looking for an answer for a previous question and had an ingenious
I was asked this question in some interview. I was required to write code
[Edit] I have reedited the previous question - since I had misunderstood what was
I had a previous question that touched this topic ( Rails: how to get
I had a look at this previous question however this doesn't seem to work
From my previous question, Converting chinese character to Unicode , I had a good
I had asked in my previous question for a package that lets me draw
This question is a followup to a previous question I had about discovering unused

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.