Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am getting this error when I attempt to operate the line: double r = Adata[i];

ID: 3721190 • Letter: I

Question

I am getting this error when I attempt to operate the line: double r = Adata[i]; . This is what the error says: Unhandled exception at 0x0FCECAB6 (ucrtbased.dll) in DTW_2.exe: An invalid parameter was passed to a function that considers invalid parameters fatal.

Here is my code:

#include <iostream>

#include <vector>

#include <fstream>

#include <algorithm>

#include <cmath>

using namespace std;

int main()

{

vector<double> Adata;

vector<double> Bdata;

int i = 0;

ifstream in("ecg_1.txt");

if (in.is_open()) {

int num;

while (in >> num) {

Adata.push_back(num);

}

}

cout << endl;

in.close();

ifstream aa("ecg_2.txt");

if (aa.is_open()) {

int num;

while (aa >> num) {

Bdata.push_back(num);

}

}

cout << endl;

aa.close(); // close file

// put data into a 2D ditance matrix

vector<vector<double> > Distance_M;

Distance_M.resize(51, vector<double>(51));

Distance_M[0][0] = 0;

for (int i = 0; i < 50; i++) {

double r = Adata[i];

Distance_M[0][i + 1] = r;

}

for (int j = 0; j < 50; j++) {

double rr = Bdata[j];

Distance_M[j + 1][0] = rr;

}

for (int i = 1; i < 50; i++) {

for (int j = 1; j < 50; j++) {

Distance_M[i][j] = abs(Adata[i] - Bdata[j]) + min(min(Distance_M[i - 1][j - 1], Distance_M[i - 1][j]), Distance_M[i][j - 1]);

}

}

// Finding the allignment

vector<int> a_points;

vector<int> b_points;

vector<double> allignment;

int l = 50;

int k = 50;

int x = 50;

int y = 50;

while (l > 1 && k > 1) {

// Minimum point is to the left

if ((Distance_M[x - 1][y] > Distance_M[x - 1][y - 1]) && (Distance_M[x - 1][y] > Distance_M[x][y - 1])) {

l = l - 1;

x = x - 1;

a_points.push_back(l);

b_points.push_back(k);

allignment.push_back(Distance_M[x][y]);

// Minimum point is to the down left and right

if ((Distance_M[x - 1][y - 1] > Distance_M[x - 1][y]) && (Distance_M[x - 1][y - 1] > Distance_M[x][y - 1]))

l = l - 1;

k = k - 1;

x = x - 1;

y = y - 1;

a_points.push_back(l);

b_points.push_back(k);

allignment.push_back(Distance_M[x][y]);

// Minimum point is to the bottom

if ((Distance_M[x][y - 1] > Distance_M[x - 1][y - 1]) && (Distance_M[x][y - 1] > Distance_M[x - 1][y]))

k = k - 1;

y = y - 1;

a_points.push_back(l);

b_points.push_back(k);

allignment.push_back(Distance_M[x][y]);

}

}

}

Explanation / Answer

#include <iostream>

#include <vector>

#include <fstream>

#include <algorithm>

#include <cmath>

using namespace std;

int main()

{

vector<double> Adata;

vector<double> Bdata;

int i = 0;

ifstream in("ecg_1.txt");

if (in.is_open()) {

int num;

while (in >> num) {

Adata.push_back(num);

}

}

cout << endl;

in.close();

ifstream aa("ecg_2.txt");

if (aa.is_open()) {

int num;

while (aa >> num) {

Bdata.push_back(num);

}

}

cout << endl;

aa.close(); // close file

// put data into a 2D ditance matrix

vector<vector<double> > Distance_M;

Distance_M.resize(51, vector<double>(51));

Distance_M[0][0] = 0;
i=0;

for (vector<double>::iterator it=Adata.begin() ; i < 50&& it != Adata.end(); i++,++it) {

double r = *it;

Distance_M[0][i + 1] = r;

}

int j = 0;
for(vector<double>::iterator it1=Bdata.begin() ; j< 50&& it1 != Bdata.end(); j++,++it1)

{

double rr = *it1;

Distance_M[j + 1][0] = rr;

}
cout<<"hi";
for (int i = 1; i < 50; i++) {

for (int j = 1; j < 50; j++) {

Distance_M[i][j] = abs(Adata[i] - Bdata[j]) + min(min(Distance_M[i - 1][j - 1], Distance_M[i - 1][j]), Distance_M[i][j - 1]);

}

}

// Finding the allignment

vector<int> a_points;

vector<int> b_points;

vector<double> allignment;

int l = 50;

int k = 50;

int x = 50;

int y = 50;

while (l > 1 && k > 1) {

// Minimum point is to the left

if ((Distance_M[x - 1][y] > Distance_M[x - 1][y - 1]) && (Distance_M[x - 1][y] > Distance_M[x][y - 1])) {

l = l - 1;

x = x - 1;

a_points.push_back(l);

b_points.push_back(k);

allignment.push_back(Distance_M[x][y]);

// Minimum point is to the down left and right

if ((Distance_M[x - 1][y - 1] > Distance_M[x - 1][y]) && (Distance_M[x - 1][y - 1] > Distance_M[x][y - 1]))

l = l - 1;

k = k - 1;

x = x - 1;

y = y - 1;

a_points.push_back(l);

b_points.push_back(k);

allignment.push_back(Distance_M[x][y]);

// Minimum point is to the bottom

if ((Distance_M[x][y - 1] > Distance_M[x - 1][y - 1]) && (Distance_M[x][y - 1] > Distance_M[x - 1][y]))

k = k - 1;

y = y - 1;

a_points.push_back(l);

b_points.push_back(k);

allignment.push_back(Distance_M[x][y]);

}

}

}