Real-Time Fleet Tracking Application in C++

This program simulates the tracking of a fleet of vehicles in real-time. It uses basic data structures to store vehicle information such as position (latitude and longitude), speed, and direction. The program continuously updates the vehicle's location every second, simulating real-time movement. The application also calculates the distance traveled by each vehicle and prints a report with the status of all vehicles in the fleet.

Group

Real-Time Programming in C++

Objective

1. Create a class Vehicle to store the vehicle's attributes: id, latitude, longitude, speed, and direction.
2. Implement a method to simulate real-time movement by updating the latitude and longitude based on speed and direction.
3. Continuously print the status of each vehicle in the fleet, including its current position and distance traveled.
4. The program should simulate the movement of vehicles in a loop, with each vehicle's data being updated every second.

Develop a real-time tracking application for a fleet of vehicles.

Example C++ Exercise

 Copy C++ Code
#include <iostream>
#include <cmath>
#include <thread>
#include <chrono>
#include <vector>

using namespace std;

// Vehicle class to store vehicle information
class Vehicle {
public:
    int id; // Vehicle ID
    double latitude; // Vehicle latitude
    double longitude; // Vehicle longitude
    double speed; // Speed in km/h
    double direction; // Direction in degrees (0-360)

    // Constructor to initialize a new vehicle
    Vehicle(int id, double latitude, double longitude, double speed, double direction)
        : id(id), latitude(latitude), longitude(longitude), speed(speed), direction(direction) {}

    // Function to simulate movement
    void move() {
        // Convert speed to km per second
        double speedPerSec = speed / 3600.0;

        // Calculate change in position based on speed and direction
        latitude += speedPerSec * cos(direction * M_PI / 180.0);
        longitude += speedPerSec * sin(direction * M_PI / 180.0);
    }

    // Function to print the current status of the vehicle
    void printStatus() {
        cout << "Vehicle ID: " << id << ", Latitude: " << latitude << ", Longitude: " << longitude << endl;
    }
};

// Function to simulate the tracking of a fleet of vehicles
void trackFleet(vector<Vehicle> &fleet) {
    while (true) {
        for (auto &vehicle : fleet) {
            vehicle.move(); // Move the vehicle
            vehicle.printStatus(); // Print current status
        }
        cout << "---------------------------------" << endl;
        this_thread::sleep_for(chrono::seconds(1)); // Wait for 1 second before updating
    }
}

int main() {
    // Create a fleet of vehicles
    vector<Vehicle> fleet;
    fleet.push_back(Vehicle(1, 34.0522, -118.2437, 60, 90)); // Vehicle 1 (Speed: 60 km/h, Direction: East)
    fleet.push_back(Vehicle(2, 40.7128, -74.0060, 80, 180)); // Vehicle 2 (Speed: 80 km/h, Direction: South)

    // Start tracking the fleet
    trackFleet(fleet);

    return 0;
}

 Output

Vehicle ID: 1, Latitude: 34.0522, Longitude: -118.2437
Vehicle ID: 2, Latitude: 40.7128, Longitude: -74.006
---------------------------------
Vehicle ID: 1, Latitude: 34.0522, Longitude: -118.2437
Vehicle ID: 2, Latitude: 40.7128, Longitude: -74.006
---------------------------------
...

Share this C++ Exercise


More C++ Programming Exercises of Real-Time Programming in C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.