dimanche 16 février 2020

How to use loop to display looped information all at once? Functionality Issue

I am learning C++ and would like some help with functionality for my code below. Quick summary/usage of my code: Program is to display randomized (x,y) coordinates and then print out the coordinates in a grid.

I got everything to work regarding randomizing (x,y) coordinates and then displaying their grid location.

The problem I am having is my code displays a separate grid for each coordinate instead of showing ALL coordinates on the same grid. [I attached a picture of my current output below].

I know this is a functionality issue.. but I am having trouble thinking of how to manipulate my loops so that the coordinates can be displayed first, followed by ONE grid with all the coordinates on it... I hope this makes sense.

Snippet of my code:

//Note: value of n and k is given by user earlier in the code
vector<vector<int> > vec( n , vector<int> (n));
cout << "\nGrid with city locations:\n";
for(i=0; i<k; i++) {
    //random select int coordinates (x,y) for each K(cities) 
    x = rand() % n + 0; 
    y = rand() % n + 0;
    arrCity[i] = i;

    //display coordinates for city 1..city2.. etc
    cout << "City " << arrCity[i] <<": (" << x << "," << y << ")" << endl;

    //display cities on grid
    for (int rows=0; rows < n; rows++) {
        for (int columns=0; columns < n; columns++) {
            if ((rows == y) && (columns == x)) {
                cout << "|" << (i);
            } else {
                cout << "|_";
            }

        }
        cout << "\n";
    }
    cout << "\n";
}

Current Output:

As you can see there's a separate grid for each 'city coordinate'




Aucun commentaire:

Enregistrer un commentaire