• Log in or sign up for an account here: https://www.sythe.org/login

Vector vs. Dynamic Memory

Red Fuel Tank

Member
Joined
Jan 5, 2009
Sythe Gold
0
Messages
87
Location
New York CITY, Yoosah
Hello,

I created a program that uses dynamic memory to resize and fill arrays.

Then, I removed the dynamic memory and tried to implement vectors. But I receive an error.

Debug Assertion Failed!
Program: ...\vc\include\vector
Line: 932
Expression: Vector subscript is out of range.

Here is the script on pastebin.com

http://pastebin.com/xx4Fmrcs

Here is the script,

Code:
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>

using namespace std;

void RandomArrayFill( vector<int>& v, int size){

/* This function fills each element in the array with
a random number between 0 and 100. */

	for( int i = 0; i < size; ++i ){
		v[i] = rand() % 101;
		cout << i + 1 << ". " << v[i] << endl;
		cout << endl;
	}
}

int main(){
	srand( time(0) );

	// Decides whether or not to quit.
	char dec = 'a';
	// Determines size of vector.
	int size = 0;
	// array is a vector( resizable array ). In this case, each element is an integer.
	vector<int> array;

	// TITLE
	cout << "::Array Filler" << endl
		 << "  ------------" << endl
		 << endl << endl;

	bool quit = false;

	// While quit is false, begin loop.
	while( !quit == true ){

		//Enter size of array.
		cout << "Please enter the size of your array: " << endl;
		cin >> size;
		cout << endl;

		RandomArrayFill( array, size );

		// Decide if we want to redo the previous process.
		cout << "Again? (Y)es (N)o : ";
		cin >> dec;
		cout << endl;

		// If Y or y, continue.
		switch(dec){
		case 'Y':
		case 'y':
			cout << "Repeating..." << endl;
			break;

		// If N or n, quit.
		case 'N':
		case 'n':
			quit = true;
			cout << "Exiting..." << endl;
			break;

		// If anything else, quit.
		default:
			cout << "Answer Invalid! Repeating Anyway..." << endl;
			cout << endl;
		}
	}
}


EDIT:


SOLVED IT


Code:
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>

using namespace std;

void RandomArrayFill( vector<int>& v){

/* This function fills each element in the array with
a random number between 0 and 100. */

	for( int i = 0; i < v.size(); ++i ){
		v[i] = rand() % 101;
		cout << i + 1 << ". " << v[i] << endl;
		cout << endl;
	}
}

int main(){
	srand( time(0) );

	// Decides whether or not to quit.
	char dec = 'a';
	// Determines the size of array.
	int size = 0;
	// array is a vector( resizable array ). In this case, each element is an integer.
	vector<int> intVec;

	// TITLE
	cout << "::Array Filler" << endl
		 << "  ------------" << endl
		 << endl << endl;

	bool quit = false;

	// While quit is false, begin loop.
	while( !quit == true ){

		//Enter size of array.
		cout << "Please enter the size of your array: " << endl;
		cin >> size;
		intVec.resize(size);
		cout << endl;

		RandomArrayFill( intVec );

		// Decide if we want to redo the previous process.
		cout << "Again? (Y)es (N)o : ";
		cin >> dec;
		cout << endl;

		// If Y or y, continue.
		switch(dec){
		case 'Y':
		case 'y':
			cout << "Repeating..." << endl;
			break;

		// If N or n, quit.
		case 'N':
		case 'n':
			quit = true;
			cout << "Exiting..." << endl;
			break;

		// If anything else, quit.
		default:
			cout << "Answer Invalid! Repeating Anyway..." << endl;
			cout << endl;
		}
	}
}
 
Runescape Gamblingrunescape gambling
Back
Top