Entering element values into array withing using cin or a file

Im really new to c++ so apologies if this is a stupid question.

Is there a way to assign random number values to an array WITHOUT using the cin statement or simply reading from a file?

For example:

I have the array Array[9];

I want to assign it the following random integer values: {-32, 23, 8, 345, 23, 0, 3, 45, -4, 2].

How would I go about doing that without using cin (which would really be inefficient if I increased the index to lets say, 10,000) or reading from a file?

Thank you.

1
2
3
4
5
6
7
8
srand(time(0));

	int arr[9];

	for (int i = 0; i < 9; i++)
	{
		arr[i] = rand();
	}


This fills the array between number 0 and 32 thousand something. If you want to limit it, do this.

arr[i] = rand() % 100 + 1 ; // Now its between 1-100.

oh, and dont forget #include <time.h>

And for next time.
http://lmgtfy.com/?q=c%2B%2B+fill+array+with+random+numbers

Would have saved us both time :)
Last edited on
To be fair, it's a forum dedicated to beginner's C++; so it's going to be rare that a novice C++ programmer asks something that can't already be found by doing a Google Search or reading the Cplusplus.com tutorials, like specifically here: http://www.cplusplus.com/reference/cstdlib/rand/

I think the reason people ask questions in a forum is to have an open conversation with a variety of people who can give them a multitude of different perspectives and paths to answer their question. For instance, perhaps I suggest they use the <random> C++ library, and the OP had no idea about its existence. By asking in a forum, they have given me the opportunity to share with them a potential piece of information they didn't even know existed. It also gives the OP a space in which to reply should they run into any issues with advice they've learned and to ask follow up questions. The point being that Google and this forum serve two entirely different purposes to someone who is looking to learn interactively.

If you wanted to save yourself time, I'd have suggested not replying at all and allowing someone less experienced to try and exercise their own troubleshooting skills; as nobody has forced you to "waste your time" by answering here. Referring someone to Google is a seemingly innocent way to "teach someone to use their resources", but it really defeats the whole purpose of a forum, as an answer to almost any question can be found anywhere if you look hard enough through google or publications.
Last edited on
I didint read most of it because Im pretty sure its a load of crap beautiful flowers, but new programmers should use google as much as possible, its a very powerful tool. He had a simple question, google could have simply answered it. Its as simple as that, dont have to write an essay about it my friend.

Edit: But thanks for your opinion and input @Aaron, I will take it into consideration next time.
Last edited on
If you don't want to accept my criticism, that's fine; but it remains valid criticism.
Thank you Aaron for your insightful post. Very well written, may I say.


And thank you Tarik for your pretentious yet informative post. I have no idea what "rand" is, nor was I aware of the existence of the <time.h> library. I suppose I'll learn this stuff in due time.
Topic archived. No new replies allowed.