1

I have a code that is written in C++17 takes values x and y as inputs and give some value as output. I want to change it to take as many inputs ( x and y values) and give outputs. What changes needed to be done in the code

what is happening in the code is : with some x and y coordinates it finds the coordinate number.

int main(void) {
    const std::vector<Tile> tiles{ Tile(0),Tile(1),Tile(2),Tile(3) };

    // Test values
      const double  x{ 3700 };  // want to  add multiple entries here
      const double  y{ 11261 };  // want to  add multiple entries here

    // Check cell number
    for (const Tile& tile : tiles) {
        if (const auto [isInTile, cellNumber] = tile.getCellNumber(x, y); isInTile) {
            std::cout << "\nCellnumber: " << cellNumber << "\n:)\n\n\n\n\n\n";

        }
    }


    return 0;
}

I have tried many changes but always end in some error also I am new to c++ my primary language is python.

zack
  • 53
  • 1
  • 11
  • 2
    it isnt obvious what you consider as "inputs" and "outputs". Do you refer to the fucntion `getCellNumber` ? – 463035818_is_not_an_ai Feb 12 '20 at 14:31
  • 1
    inputs can be any , const double x{ 3700 }; const double y{ 11261 }; – zack Feb 12 '20 at 14:33
  • 2
    @moeen It is not clear what your problem is. The analogue of a Python list is usually `std::vector` which is already used in the code. And Python `for`/`in` loops usually map to the range-for as is used in `for (const Tile& tile : tiles)`. If you don't know how these work, I suggest you have a look at a [good C++ introductory book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – walnut Feb 12 '20 at 14:39
  • 1
    please check the snap in the post , with x and y coordinates are given and code gives the grid box number. i dont want to change it to python , just want to change in the existing code so that it takes multiple inputs to give multiple outputs – zack Feb 12 '20 at 14:44
  • "inputs can be any , const double x{ 3700 }; const double y{ 11261 };" ok, how does this relate to the code you posted? `x` and `y` are parameters you want to pass to the function `getCellNumber` ? – 463035818_is_not_an_ai Feb 12 '20 at 14:44
  • "in the existing code so that it takes multiple inputs to give multiple outputs" what is "it" ? – 463035818_is_not_an_ai Feb 12 '20 at 14:44
  • i want to input multiple values of x ={3435,5345,4354, 345......} , x ={435,6785,4554, 4545.....} and will give outputs – zack Feb 12 '20 at 14:50
  • take input as array or list – zack Feb 12 '20 at 14:51

1 Answers1

1

If you want multiple input values, then put x,y into a vector, like you did with tiles:

    // Test values
    const std::vector<std::pair<double, double>> test_values = {
        { 3700, 11261 },
        { 2500, 10000 },
        { 1000, 5000 }
    };

    // Check cell number
    for (const Tile& tile : tiles) {
        for (const auto [x,y] : test_values) {
            if (const auto [isInTile, cellNumber] = tile.getCellNumber(x, y); isInTile) {
                std::cout << "\nCellnumber: " << cellNumber << "\n:)\n\n\n\n\n\n";
            }
        }
    }
PooSH
  • 601
  • 4
  • 11
  • 1
    this is exactly what i was looking for , but my inputs are larges in numbers can code take xy values directly from csv file ? and give outputs – zack Feb 12 '20 at 15:06
  • 2
    @moeen Nobody prevents you from opening and parsing the file. But that's another question ;) – PooSH Feb 12 '20 at 15:13