Search code examples
c++csvnested-loops

C++ Nested for loops


I am writing a program that modifies data in a csv file. In the csv file, the COLUMNS are organized as follows..

X-coordinate, Y-coordinate, Z-coordinate, info, X, Y, Z, info, X, Y, Z info..

The first X-coordinate begins in column 4 and the next one is 4 columns after, in 8. For Y, it's column 5 and column 9, so on. Since I saved the data onto a deque, the first ones correspond to data[row#][3] for x, and y would be data[row#][5].

for(int k=0; k<618; k++) { //all rows  618
    for(int l=3; l<96; l=l+4) { //x columns
        for(int m=4; m<97; m=m+4) { //y columns
            data[k][l] = (data[k][l] )*(data[k][2]) + (data[k][m])*(data[k][1]);

In the calculation in the loop, I want it to replace all the x values (l) in columns (k) with the value I get from this equation (as I created for the loop)

x' = x* cos(theta) + y* sin(theta)

the values for cos(theta) and sin(theta) are found in columns 2 and 3 for all the rows (hence, data[k][2] and data[k][1].

Unfortunately, in testing this out with several cout statements, I noticed it is not doing as desired.

DESIRED BEHAVIOR OF LOOP:

  • 1st time through loop: Calculation is done for row 1, x = value inside column 4 and y= value in col.5

  • *end of loop iteration, re-start, k, l, and m get updated to 2,9,10.

  • Calculation in the loop is executed for these new values, so on.

Main issue is k, l, m are not all three being updated as desired after the data[k][l] line What could be causing this? Thank you.


Solution

  • You do not understand nested loops.

    What you intend is something like this:

    for(int k=0; k<618; k++) { //all rows  618
      for(int n=0; n<24; ++n) { //groups
        l = 4*n + 3;
        m = 4*n + 4
        data[k][l] = (data[k][l] )*(data[k][2]) + (data[k][m])*(data[k][1]);
      }
    }