-1

I'm having some trouble with outputting data from the vectors I have created.

Here is the code:

#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

int main() {

    vector<double> XY;
    vector<vector<double>> Cords;

    for (double i = 0; i < 10; i++)
    {

        cout << i << "\n";

        for (double j = 0; j < 1; j++)
        {

            double X = i;
            double Y = i + 1;

            XY.push_back(X);
            XY.push_back(Y);

            Cords.push_back(XY);

        }

        cout << "[" << Cords[i][0] << "," << Cords[i][1] << "]" << "\n";

    }

}

the output I receive is:

0
[0,1]

1
[0,1]

2
[0,1]  

3
[0,1]

4
[0,1]

5
[0,1]

6
[0,1]

7
[0,1]

8
[0,1]

9
[0,1]

The code is supposed to output the newest set of coordinates on every iteration of the main loop but it keeps outputting the first set [0,1] instead of the desired [0,1] [1,2] [2,3] etc...

genpfault
  • 51,148
  • 11
  • 85
  • 139
Dan Tea
  • 1
  • 3
  • i think you want j – Neil Butterworth Oct 19 '22 at 12:10
  • 1
    I want to note: indexing array with a floating point variable will give unexpected results. E.g. float 1.0 does not necessarily be 1 when casted to integer. – Özgür Murat Sağdıçoğlu Oct 19 '22 at 12:12
  • `XY` is defined before the loops, and therefore each `XY.push_back` you keep adding to the same vector. If you want `XY` to hold only 1 x,y coordinate, you should define it inside the inner loop so it will be a new one every iteration. – wohlstad Oct 19 '22 at 12:12
  • Perhaps at the end of your main output XY.size() ... it seems that you push back its growing copies to Cords but expect something else than you see. – Öö Tiib Oct 19 '22 at 12:12
  • `for (double j = 0; j < 1; j++)` only loops once - what did you expect? – Jesper Juhl Oct 19 '22 at 12:21
  • 1
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Oct 19 '22 at 12:21
  • 1
    You'll be glad to hear you don't need anyone's help to figure this out, just a tool you already have: your debugger! This is exactly what a debugger is for. It [runs your program, one line at a time, and shows you what's happening](https://stackoverflow.com/questions/25385173/), this is something that's every C++ developer must know how to do. With your debugger's help you'll able to quickly find all problems in this and all future programs you write, without having to ask anyone for help. Have you tried using your debugger, already? If not, why not? What did your debugger show you? – Sam Varshavchik Oct 19 '22 at 12:40

1 Answers1

1

it is not accurate to use non-integers in for-loops, So I suggest to do as the following example so you keep the accuracy of integers and you can get what you want.

but wait after changing the datatype to integers we also have a problem!! you declare the vector<int>XY outside the loop you always push new values and (0 ,1) stays as answer every iteration! so instead of declare it outside the loop you should declare it inside so you always refresh the current elements and get the expected values.

#include <iostream>
#include <vector>
using namespace std;
int main() {
    vector<vector<int>> Cords;
    for (int i = 0; i < 10; i++) {
        vector<int> XY;
        for (int j = 0; j < 1; j++) {
            int X = i;
            int Y = i + 1;
            XY.push_back(X);
            XY.push_back(Y);
            Cords.push_back(XY);
        }
        cout << "[" << Cords[i][0] << "," << Cords[i][1] << "]" << "\n";
    }
}