-1

I'm new to C++. There are three functions in my program. The first one works same as np.linspace in python. The later two both take in a 2d vector, an integer and return a 2d vector. Here is my code:

#include <iostream>
#include <cmath>
#include <vector>
using namespace std;


vector<float> linspace(float start, float end, int step);
vector< vector<float> > angleDisc(vector < vector<float> > data, int theta_loc);
vector< vector<float> > ringDisc(vector < vector<float> > data, int r_loc);

const float R_M = 18, RESO_R = 80;
const float RESO_THETA = 60;
const float PI = 3.14159;

const vector<float> DIS_R = linspace(0, R_M, RESO_R);
const vector<float> DIS_THETA = linspace(0, PI/2, RESO_THETA);

int main() {
    vector < vector<float> > inner = {{0.1, 0.000001},{1,-2},{-2,3},{1,3},{6,1},{4,-4},{1,3.2},{4,5.7}};

    vector< vector<float> > angled = angleDisc(inner, 0);
    //vector< vector<float> > ringd = ringDisc(inner, 0);

    for(int j = 0; j < angled.size(); j++){

        for(int i = 0; i < 2; i++){
        cout << angled[j][i] << " ";
        }
        cout << "next" << endl;

        return 0;
    }

vector<float> linspace(float start, float end, int step){
    vector<float> stairs;
    float increment = (end - start) / step;
    for (int i = 0; i <= step; i++){
        stairs.push_back(i * increment);
    }
    return stairs;
}

vector< vector<float> > angleDisc(vector < vector<float> > data, int theta_loc){
   vector< vector < vector<float> > > pieced_disc( DIS_THETA.size() );
   float angle;

   for (int i = 0; i <= DIS_THETA.size() - 1; i++){
       for (int k = 0; k <= data.size() - 1; k++){
           angle = abs( atan( data[k][1] / data[k][0]));
           if ( (angle > DIS_THETA[i]) && (angle <= DIS_THETA[i+1]) ){
               pieced_disc[i].push_back(data[k]);
               data.erase(data.begin() + k - 1 );
           } 
       }
   }
   return pieced_disc[theta_loc];
}


vector< vector<float> > ringDisc(vector < vector<float> > data, int r_loc){
   vector< vector < vector<float> > > ringed_disc( DIS_R.size() );
   float r;

   for (int i = 0; i <= DIS_R.size() - 1; i++){
       for (int k = 0; k <= data.size() - 1; k++){
           r = sqrt(pow(data[k][0], 2) + pow(data[k][1], 2));
           if ( (r > DIS_R[i]) && (r < DIS_R[i+1]) ){
               ringed_disc[i].push_back(data[k]);
               data.erase(data.begin() + k - 1 );
           }
       }
   }
   return ringed_disc[r_loc];
}

Though this works fine but if I uncomment the:

vector< vector<float> > ringd = ringDisc(inner, 0);

in the main function. The code could still be compiled successfully but executing the object file doesn't return anything, I have to press "Control + C" to exit.

I have tried: 1) flip the order of function angleDisc() and ringDisc(), in this case, ringDisc(), which goes first, could print the result in main function but the angle disc can't.

2) Let ringDisc() be exactly the same as angleDisc, but only the function names are different, this time, both function could work properly.

I am really confused, could anyone help me with this?

Veronica
  • 43
  • 5

2 Answers2

2

Your program has undefined behaviour, because (initially, when k == 0) you try to

data.erase(data.begin() - 1);

and again when i == DIS_R.size() - 1 you try

DIS_R[i+1]

Why are you removing elements from data whilst iterating over it? That would result in only visiting half the elements

Caleth
  • 52,200
  • 2
  • 44
  • 75
0

You are modifying the vector data inside the functions and yet you have passed it as pass by value. Instead, you should have passed it to the function as pass by reference

Check this out. This works correctly I guess.

#include <iostream>
#include <cmath>
#include <vector>
using namespace std;

vector<float> linspace(float start, float end, int step);
vector< vector<float> > angleDisc(vector < vector<float> > &data, int theta_loc);
vector< vector<float> > ringDisc(vector < vector<float> > &data, int r_loc);

const float R_M = 18, RESO_R = 80;
const float RESO_THETA = 60;
const float PI = 3.14159;

const vector<float> DIS_R = linspace(0, R_M, RESO_R);
const vector<float> DIS_THETA = linspace(0, PI/2, RESO_THETA);

vector<float> linspace(float start, float end, int step){
    vector<float> stairs;
    float increment = (end - start) / step;
    for (int i = 0; i <= step; i++){
        stairs.push_back(i * increment);
    }
    return stairs;
}

vector< vector<float> > angleDisc(vector < vector<float> > &data, int theta_loc){
   vector< vector < vector<float> > > pieced_disc( DIS_THETA.size() );
   float angle;

   for (int i = 0; i < DIS_THETA.size(); i++){
       for (int k = 0; k < data.size(); k++){
           angle = abs(atan( data[k][1] / data[k][0]));
           cout<<"angle: "<<angle<<"\n";
           if ( (angle > DIS_THETA[i]) && (angle <= DIS_THETA[i+1]) ){
               pieced_disc[i].push_back(data[k]);
               data.erase(data.begin() + k);
           } 
       }
   }
   return pieced_disc[theta_loc];
}


vector< vector<float> > ringDisc(vector < vector<float> > &data, int r_loc){
   vector< vector < vector<float> > > ringed_disc( DIS_R.size() );
   float r;

   for (int i = 0; i < DIS_R.size(); i++){
       for (int k = 0; k < data.size(); k++){
           r = sqrt(pow(data[k][0], 2) + pow(data[k][1], 2));
           if ( (r > DIS_R[i]) && (r < DIS_R[i+1]) ){
               ringed_disc[i].push_back(data[k]);
               data.erase(data.begin() + k);
           }
       }
   }
   return ringed_disc[r_loc];
}


int main() {
    vector < vector<float> > inner = {{0.1, 0.000001},{1,-2},{-2,3},{1,3},{6,1},{4,-4},{1,3.2},{4,5.7}};
    vector< vector<float> > angled = angleDisc(inner, 0);
    vector< vector<float> > ringd = ringDisc(inner, 0);

    for(int j = 0; j < angled.size(); j++){
        for(int i = 0; i < 2; i++){
        cout << angled[j][i] << " ";
        }
        cout << "next" << endl;
        return 0;
    }

    for(int j = 0; j < ringd.size(); j++){
        for(int i = 0; i < 2; i++){
        cout << ringd[j][i] << " ";
        }
        cout << "next" << endl;
        return 0;
    }
}
Chirag Shah
  • 87
  • 1
  • 3
  • 13