Sign in to follow this  

vectors

This topic is 2845 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

#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[i];

// 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[i] = 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

This topic is 2845 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this