Question:
C++ vector subscript out of range?
silvery_raksha
2012-07-06 19:30:48 UTC
could someone please tell me how to fix this code? It compiles, but then crashed as soon as it gets to the fitnessChart function :(



#include "stdafx.h"
#include
#include
#include
#include

using namespace std;

void fitnessChart(vector>>& array, int&, int&);

int main(int argc, char* argv[])
{
int vertices;
const int initial_value = 0;
int i;
std::cout << "Enter the number of vertices:" << std::endl;
std::cin >> vertices;
std::cout << "Enter the size of the population:" << std::endl;
std::cin >> i;

std::vector sub_sub_array(vertices, initial_value);
std::vector > sub_array(vertices, sub_sub_array);
std::vector > > array(i, sub_array);

int e;
for(int i = 0; i < vertices; ++i)
{
for(int j = 0; j < vertices; ++j)
{
for(int k = 0; k < vertices; ++k)
{
e = rand() %2;
if (j == k)
{
array[i][j][k] = 9;
break;
}
else
{
array[i][j][k] = e;
array[i][k][j] = e;
}

}
}
}



// Print out the full array contents
for(int i = 0; i < vertices; ++i)
{
for(int j = 0; j < vertices; ++j)
{
for(int k = 0; k < vertices; ++k)
{
if(j!=k)
{
std::cout << " " << array[i][j][k] << " ";
}
else
std::cout << " - ";
}
std::cout << "\n";
}
std::cout << "\n";
}

for(int i = 0; i < vertices; i++)
{
fitnessChart(array, vertices, i);
}

system ("pause");
}

void fitnessChart(vector>>& array, int& vertices, int& i)
{
int count=0, match, j=0, k=1;

for(k; k < vertices; k++)
{
match = array[i][j][k];
j=k;

for(k; k < vertices; k++)
{
if (array[i][j][k+1] == match)
{
if (array[i][j][k+1] == match)
{
count++;
}
}
}
}
cout << count << endl;
}
Three answers:
Jeff
2012-07-06 20:46:49 UTC
Simple duplicate problem in multiple spots....



see lines like this:

for(int i = 0; i < vertices; ++i)



well there are not vertices number of entries.... there are i (population) number of entries.... you are going out of bounds on at least three seperate spots within the code...



change this and this

int i;

std::cin >> i;

std::vector > > array(i, sub_array);





to ...uhh lets say p...



then you want

for(int i = 0; i < p; ++i)



do the same this for all other occurrences and you will have no errors.



I ran 30 vertices with 100 population no problem.



Something is broken on your end yes....



You have a good start with your code from ideone... problem is you didn't follow my advice to the letter...



you also cannot use array[i].size() for a size... use array[0].size()



this runs and compiles just fine (any size vertices work again)... but now the numbers at the end are all the same...

http://ideone.com/B4w5m



download codeblocks-10.05mingw-setup.exe from

http://www.codeblocks.org/downloads/26



copy and paste the first code I posted and it will work perfectly... you can edit as you see fit and compile after every edit to make sure it still works.



Here's example code and output from the second link http://ideone.com/NqbOl



here is output from the first link http://ideone.com/YpgpC
James Bond
2012-07-07 02:50:31 UTC
I dont know what you are doing.

I did twiddle your code little bit. It got compiled and working

Yet, some run time error is coming when I give number of vertices as 9





//#include "stdafx.h"

#include

#include

#include

#include



using namespace std;



void fitnessChart(std::vector > > array, int&, int&);



int main(int argc, char* argv[])

{

int vertices;

const int initial_value = 0;

int i;

std::cout << "Enter the number of vertices:" << std::endl;

std::cin >> vertices;

std::cout << "Enter the size of the population:" << std::endl;

std::cin >> i;



std::vector sub_sub_array(vertices, initial_value);

std::vector > sub_array(vertices, sub_sub_array);

std::vector > > array(i, sub_array);



int e;

for(int i = 0; i < vertices; ++i)

{

for(int j = 0; j < vertices; ++j)

{

for(int k = 0; k < vertices; ++k)

{

e = rand() %2;

if (j == k)

{

array[i][j][k] = 9;

break;

}

else

{

array[i][j][k] = e;

array[i][k][j] = e;

}



}

}

}





// Print out the full array contents

for(int i = 0; i < vertices; ++i)

{

for(int j = 0; j < vertices; ++j)

{

for(int k = 0; k < vertices; ++k)

{

if(j!=k)

{

std::cout << " " << array[i][j][k] << " ";

}

else

std::cout << " - ";

}

std::cout << "\n";

}

std::cout << "\n";

}



for(int i = 0; i < vertices; i++)

{

fitnessChart(array, vertices, i);

}



system ("pause");

}





void fitnessChart(std::vector > >array, int& vertices, int& i)

{

int count=0, match, j=0, k=1;



for(k; k < vertices; k++)

{

match = array[i][j][k];

j=k;



for(k; k < vertices; k++)

{

if (array[i][j][k+1] == match)

{

if (array[i][j][k+1] == match)

{

count++;

}

}

}

}

cout << count << endl;

}
?
2012-07-07 03:24:39 UTC
Paste your code in ideone.com and provide a link... yahoo answers cuts it off


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...