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

  • SEARCH
  • Home
  • 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 9194775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:29:33+00:00 2026-06-17T21:29:33+00:00

How can I create the code and algorithm in C++ under this circumstances? There

  • 0

How can I create the code and algorithm in C++ under this circumstances?

  1. There are limited (at most 6) cities(nodes) which can be read from a file.
  2. Connections and distance between cities can be assigned manually or read from a file.
  3. There are cars that have routes can be assigned manually and the starting points are the cities(nodes).

Questions,

  1. How can I create nodes and assign distance between nodes?
  2. How can I make connections between nodes?
  3. How can I calculate the initial points of the cars a while after the cars start moving?

I tried connection between cities like:

    A   B   C   D   
A   0  20   0  20  
B  20   0  20  20  
C  20  20   0  15
D  0    8   9  0

If there is a connection between 2 cities, it has a value. If not, the value is “0”.
Thanks,

  • 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-06-17T21:29:34+00:00Added an answer on June 17, 2026 at 9:29 pm

    One way to do this is by building a graph of the connections. What you’re looking at here is fairly basic graph building. The most obvious method is usually to have a node type with a vector of connections to other nodes:

    struct node;
    
    // "name" may be optional here -- just the name of the road like "A5" or "US 101"
    struct connection { 
        std::string name;
        unsigned distance;
        node *destination;
    };
    
    struct node { 
        std::string name;
        std::vector<connection> connections;
    };
    

    With this, building a connection from one town to another is a matter of creating a connection object holding the distance, destination city, and (probably) the name of the road, then pushing that connection onto the connections vector for the starting city. For the moment, this assumes every connection is “simple” — that you get on one road, which goes directly to a destination (which sounds like it’s sufficient for your needs). In most cases you’ll also want these to be symmetrical, so when/if you add a connection from X to Y, you’ll also want to add a similar connection from Y to X.

    In real life, a connection might require switching roads along the way, so you might have some nameless nodes that just represent (for example) the junction of highway X with highway Y, (though it might not be nameless either — “junction of X and Y” might be a perfectly good name) so you have to travel through several nodes to get from some point A to some point B.

    Unless you’re doing this as an exercise, you might want to look at the Boost Graph Library. It’ll help not only with building a basic graph like this, but also with things like finding a route from one town to another even though the two aren’t directly connected.

    As far as positions of cars go, a lot depends on whether you’re looking at just calculating a specific distance, or whether (for example) you want a full-blown simulation where you can do things like find a route from point A to point B, find that traffic is above some threshold on a particular road, possibly find alternate routing if traffic is too heavy, etc. For the first, it’s a simple matter of computing a distance based on time and speed, then walking through nodes on their route until you’ve traveled that distance. For the latter, you’d probably add a car class, where each car knows its own current position and destination. You’d then walk through the list of cars at intervals and have each update its current position (e.g., once per simulated minute).

    If you do a bit of research, you’ll quickly find there are quite a few algorithms for things like finding routes in a graph (e.g., given graph X, find the shortest route from A to B), finding whether all the nodes are connected, etc.

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

Sidebar

Related Questions

With this code we can create a simple word document but I need to
I'm trying to create a function to read Morse code from one file, convert
The following code can be found in the NHibernate.Id.GuidCombGenerator class. The algorithm creates sequential
I can create function pointers in Fortran 90, with code like real, external ::
I want to know if I can create a WebBrowser through code in Vb.NET,
Can you create a line of code, within a while-loop, that will create a
How can I create anti-noise with code or an application? It doesn't have to
How can I create an environment in client-side javascript where any code related to
How can I create pdf documents with 2D barcodes (for example QR code http://en.wikipedia.org/wiki/QR_Code
Can I fire show create table query in Java code for mysql? What I

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.