0

It says my vector is out of range for ArrayD. But I do not understand why. Everything else works. Is says that the vector is out of range after double deez. Therefore, it will not ascend fully.

using namespace std;

void hello();

void Infile();

double x, y, z;

double P, B;

int E = 0;

double V[16];

double C[16];

double D[16];

int m, n;

int main()

{

Infile();
return 0;

}

void Infile()

{

ifstream fileA("P_matrix.txt");
ifstream fileB("b_matrix.txt");
ifstream fileC("d_matrix.txt");
vector<double>ArrayP;
vector<double>ArrayB;
vector<double>ArrayD;


cout << "P Matrix Values \n";
while (fileA.good())
{
    fileA >> P;
    ArrayP.push_back(P);
}
for (int i = 0; i<ArrayP.size(); i++)
{
    cout << ArrayP[i] << ",";
}
system("pause");



cout << "B Matrix Values \n";
while (fileB.good())
{
    fileB >> B;
    ArrayB.push_back(B);
}
for (int j = 0; j < 16; j++)
{
    cout << ArrayB[j] << ",";
}
system("pause");
while (fileC.good())
{

    fileC >> D;
    ArrayD.push_back(D);


}
for (int k = 0; k <16; k++)
{
    cout << ArrayD[k] << ",";

}

system("pause");


for (int m = 0; m < 16; m++)
{
    V[m] = ArrayP[m] * ArrayB[m];
    cout << V[m] << ",";
}
system("pause");

for (int n = 0; n < 16; n++)
{
    C[n] = V[n] * ArrayD[n];
    cout << C[n] << ",";
}

//outfile.close();
system("pause");

double deez;
for (int d = 0; d < 16; d++) //acscending
{
    for (int q = 0; q < 16; q++)
    {
        if (ArrayD[q] < ArrayD[q - 1])
        {
            deez = ArrayD[q];
            ArrayD[q] = ArrayD[q - 1];
            ArrayD[q - 1] = deez;
        }
    }
}
for (int q = 0; q < 16; q++)
    cout << C[q] << ",";
system("pause");



double nutz;
for (int d = 0; d < 16; d++) //descending
{
    for (int q = 0; q < 16; q++)
    {
        if (V[q] < V[q + 1])
        {
            nutz = V[q];
            V[q] = V[q + 1];
            V[q + 1] = nutz;
        }
    }
}
for (int q = 0; q < 16; q++)
    cout << V[q] << ",";
system("pause");
return;

}


  • Note: When writing C++ steer strongly towards solutions using Standard Library containers like `std::vector` or `std::array` and avoid C-style arrays. `std::vector` is dynamic, you `push_back` rows of data, meaning you'll never overflow it by accident. – tadman Nov 19 '19 at 01:08
  • You define `row` and `column` constants, then completely ignore those for the rest of the program, instead using hard-coded `4` everywhere. That's worth fixing to keep it consistent. Remember an `int[4]` has 4 positions in it numbered 0..3, and `P[4]` is *undefined behaviour* as you've walked off the end of the array. You're also reading everything into the same (non-existent) element in each array. – tadman Nov 19 '19 at 01:09
  • Also remember `argv`! You can use that to specify which file to open, meaning it's easy to re-run tests: `myprogram qap.txt` for example. – tadman Nov 19 '19 at 01:11
  • Your `while` loop is missing `{` and `}` braces. Auto-formatting in any decent IDE would have revealed that. You're reading into `data`, but never doing anything with it, effectively skipping a line. Maybe that's intended? – 3Dave Nov 19 '19 at 01:22
  • ifstream in_file("qap.txt"); if (in_file.is_open()) { while (getline(in_file, data)) So I named the txt file that I wanted to use and put the {} for the while loop but it still not picking up the matrices – Avian Richardson Nov 19 '19 at 03:50
  • you read the input badly, see https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons – M.M Nov 21 '19 at 04:00

1 Answers1

1

Here, q starts at zero, so you are attempting to access index [-1].

ArrayD[q - 1]

When iterating over ArrayP, you correctly do:

    for (int i = 0; i<ArrayP.size(); i++)

However for ArrayB and ArrayC, you seem to assume there are only ever 16 elements in those arrays (are you sure?). I can't see any evidence that the arrays are restricted to always having 16 elements in code, so that could also be the issue.

Later still, you then seem to assume all of those arrays have 16 elements?

    for (int m = 0; m < 16; m++)
    {
        V[m] = ArrayP[m] * ArrayB[m];
        cout << V[m] << ",";
    }

Those are the obvious places that could cause an out of range error.

robthebloke
  • 6,791
  • 4
  • 10