This code simulates a GPS tracking system that "moves" through...

August 23, 2025 at 12:16 AM

#include <iostream> #include <thread> #include <chrono> using namespace std; // Struct to represent a GPS coordinate struct GPS { double latitude; double longitude; }; // Function to display coordinates void showLocation(const GPS& loc) { cout << "Latitude: " << loc.latitude << ", Longitude: " << loc.longitude << endl; } int main() { // Dummy GPS path GPS path[] = { {14.5995, 120.9842}, // Manila {35.6895, 139.6917}, // Tokyo {40.7128, -74.0060}, // New York {48.8566, 2.3522}, // Paris {51.5074, -0.1278} // London }; int size = sizeof(path) / sizeof(path[0]); cout << "Simulated GPS Tracking...\n" << endl; // Simulate moving through locations for (int i = 0; i < size; i++) { showLocation(path[i]); this_thread::sleep_for(chrono::seconds(2)); // pause for 2 seconds } cout << "\nTracking finished!" << endl; return 0; }

This code simulates a GPS tracking system that "moves" through a series of predefined GPS coordinates (locations like Manila, Tokyo, New York, etc.) and displays them one by one with a delay of 2 seconds between each display. Here's a breakdown:

  1. Struct Definition (GPS): A struct is defined to represent a GPS coordinate with two fields:

    • latitude: Represents the latitude of the location.
    • longitude: Represents the longitude of the location.
  2. showLocation Function: A helper function that takes a GPS object as input (passed by constant reference) and prints out the latitude and longitude values.

  3. Predefined GPS Path: An array of 5 predefined GPS coordinates is created, representing locations in Manila, Tokyo, New York, Paris, and London.

  4. Tracking Simulation:

    • Using a loop, the program iterates through the GPS coordinates in the path array.
    • For each coordinate:
      • The showLocation function is called to print its latitude and longitude.
      • The program pauses (simulates a delay) for 2 seconds using this_thread::sleep_for(chrono::seconds(2)).
  5. Output Messages:

    • At the beginning, it prints "Simulated GPS Tracking..." to indicate the start of the simulation.
    • Once all coordinates are displayed, it prints "Tracking finished!" to indicate the end of the simulation.

Example Output:

The output of the program will appear sequentially over time (with a 2-second delay between each location):

Simulated GPS Tracking...

Latitude: 14.5995, Longitude: 120.9842
Latitude: 35.6895, Longitude: 139.6917
Latitude: 40.7128, Longitude: -74.006
Latitude: 48.8566, Longitude: 2.3522
Latitude: 51.5074, Longitude: -0.1278

Tracking finished!

Purpose:

The code demonstrates the use of structures, functions, arrays, loops, and threading to simulate time-based operations, making it useful for educational purposes or basic GPS location simulation applications.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node