How to solve some errors in the code in c++

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>

class Person { //creating a Person class with attributes like ID, latitude (X) and longitude (y)
public:
    int x;
    int y;
    bool infected;



};  Person(int _id, int _x, int _y, bool _infected) : id(_id), x(_x), y(_y), infected(_infected) {}

void display(const Person people[], int numPeople) { // Display movements and infection status
    system("cls");  // Clear console

    for (int i = 0; i < numPeople; ++i) {
        std::cout << "Person " << people[i].id << ": ";
        std::cout << "Location (" << people[i].x << ", " << people[i].y << ")";
        std::cout << " - Infected: " << (people[i].infected ? "Yes" : "No") << std::endl;
    }
}

int main() {
    srand(time(nullptr));

    const int numPeople = 100; // creating 100 people with random locations and infections
    const int numInfected = numPeople * 0.1;
    const int n = 7;  // Number of days for simulation

    Person people[numPeople];

    for (int i = 0; i < numPeople; ++i) {
        int x = rand() % 201 - 100;  // [-100, 100]
        int y = rand() % 201 - 100;
        bool infected = i < numInfected;

        people[i] = Person(i + 1, x, y, infected);
    }

    for (int day = 1; day <= n; ++day) { // simulation movement for n days
        for (int i = 0; i < numPeople; ++i) {
            if (!people[i].infected) {
                int dx = rand() % 3 - 1;  // -1, 0, 1
                int dy = rand() % 3 - 1;

                people[i].x += dx;
                people[i].y += dy;
            }
        }

        for (int i = 0; i < numPeople; ++i) {  // Tracking infections
            if (people[i].infected) {
                for (int j = 0; j < numPeople; ++j) {
                    if (!people[j].infected) {
                        double distance = sqrt(pow(people[i].x - people[j].x, 2) + pow(people[i].y - people[j].y, 2));
                        if (distance <= sqrt(2)) {
                            if (rand() % 100 < 90) {
                                people[j].infected = true;
                            }
                        }
                    }
                }
            }
        }

        display(people, numPeople);
    }

    int numInfectedAtEnd = 0; // Count infected people
    for (int i = 0; i < numPeople; ++i) {
        if (people[i].infected) {
            numInfectedAtEnd++;
        }
    }
    std::cout << "Number of infected people at the end: " << numInfectedAtEnd << std::endl;

    return 0;
}

Recommended Answers

All 2 Replies

What's the error? What have you tried?

Looking at your post you mention some errors, you need to be more specific as to what the error messages are, on which lines do they occur etc. As it stands now you have a LOT of code and we cannot possibly try to guess at where your code errors out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.