Jump to content
  • Advertisement
Sign in to follow this  
phil67rpg

vectors

This topic is 3064 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to fill an array with random numbers using a vector.The array size is input by the user. The random numbers are from 0 to 100. I am confused about how to use vectors.
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <time.h>

using namespace std;

void RandomArrayFill(std::vector<int> &vec);

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

	int size;

	cout << "Enter the size of an array to create: ";
	cin >> size;
	cout << endl;

	cout << "Creating array and filling it with random numbers..." << endl;

	cout << "Array = {";

 //RandomArrayFill(std::vector<int> &vec);

	cout << "}" << endl;

	return 0;
}

void RandomArrayFill(std::vector<int> &vec)
{



}

Share this post


Link to post
Share on other sites
Advertisement

#include <iostream>
#include <vector>
#include <stdlib.h>
#include <time.h>

using namespace std;

void RandomArrayFill(std::vector<int> &vec);

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

int size;

cout << "Enter the size of an array to create: ";
cin >> size;
cout << endl;

cout << "Creating array and filling it with random numbers..." << endl;

std::vector<int> intVector;

// Give the correct size. You must control that size >= 0.
intVector.resize(size);

cout << "Array = {";

RandomArrayFill(intVector);

for(unsigned int i=0; i<intVector.size(); i++)
{

cout << intVector;

// If the current element isn't the last, we must write ", "
if(i<intVector.size()-1)
cout << ", ";

}

cout << "}" << endl;

return 0;
}

void RandomArrayFill(std::vector<int> &vec)
{

for(unsigned int i=0; i<vec.size(); i++)
vec = rand % 100;

}



I recommend that you research before ask these questions. You will learn a lot by searching and reading.

Share this post


Link to post
Share on other sites
You keep re-making this thread with every tiny bit of progress on the program or alternate approach.

You've also been a forum member for over 6 years, and also have posts asking about significantly more involved problems, such as working with DX.

Just what is going on here?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!