Gogo gogo
Gogo gogo

Reputation: 13

Vector subscript out of range VS2013

Can somebody explane to me what I am doing wrong? After I compile this code in VS2013 the program starts and prints 2-3 columns and I get this error:

Vector subscript out of range

what could be wrong?

#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <ctime>
#include <vector>
using namespace std;


vector<int> printVector(vector<int> vectorToPrint)
{
        for (unsigned int i = 0; i < vectorToPrint.size(); i++)
        {
                cout << vectorToPrint[i] << ",";
        }
        cout << endl;
        return vectorToPrint;
}

int getRandomNumber(int min,int topNumber)
{
        int number = min + (rand() % (int)(topNumber - min + 1));
        return number;
}

vector<int> getSixNumbers(vector<int> numbers){
        vector<int> randomNumbers;
        for (int i = 0; i < 6; i++)
        {

                int(*pointerToRandomNumber)(int, int) = getRandomNumber;
                int randomNumber = pointerToRandomNumber(0, numbers.size());
                int numberRandomNumber = numbers[randomNumber];
                numbers.erase(numbers.begin() + randomNumber);
                randomNumbers.push_back(numberRandomNumber);
        }
        return  randomNumbers;
}
int _tmain(int argc, _TCHAR* argv[])
{ 

        srand(time(NULL));
        int g;
        vector<int> numbers;
        for ( int i = 1; i < 50; i++)
        {
                numbers.push_back(i);
        }
        int a = 1;

        do
                {
                        vector<int>(*pointerToSixNumbers)(vector<int>) = getSixNumbers;
                        vector<int> newVector = pointerToSixNumbers(numbers);
                        printVector(newVector);
                        ++a;

        }while (a < 100);


        cout << endl;
        cin >> g;
        return 0;

}

Upvotes: 0

Views: 99

Answers (2)

Dan
Dan

Reputation: 393

int number = min + (rand() % (int)(topNumber - min + 1));

int randomNumber = pointerToRandomNumber(0, numbers.size());

These two lines are important. Say you have a vector of size 6, which means that you can access vector[0] - vector[5]. You pass (0,6) in to your get random number function which translates to the first line being:

int number = 0 + (rand() % (int)(6 - 0 + 1));

or

int number = rand() % (int)(7); 

Which has the ability to return 6 and is out of the vectors range when you access the numbers array with the subsequent line. Remove the +1 and that problem should go away since you are using the vector.size() function.

int number = min + (rand() % (int)(topNumber - min));

Upvotes: 1

R Sahu
R Sahu

Reputation: 206667

The problem is in the line

int number = min + (rand() % (int)(topNumber - min + 1));

It should be

int number = min + (rand() % (int)(topNumber - min));

Otherwise, you end up getting an index that is out of range.

I used

  cout << "size: " << numbers.size() << endl;
  cout << "randomNumber: " << randomNumber << endl;

before the line

  int numberRandomNumber = numbers[randomNumber];

to track the problem.

Upvotes: 0

Related Questions