View Single Post
Old 09-11-2015, 12:15 AM   #5
Sir Krisk
FFR Veteran
sickufully's Avatar
Join Date: Dec 2007
Location: Vic, Australia
Posts: 928
Default Re: [University - C++ Programming] Filling an Array with RNG & Display Contents

Cheers for that llyair, got it working now. Closed down VS and opened it again and it worked. Weird cause I tried that earlier and it didn't work.

Alright so now I'm pretty sure I've got values stored in all 20 elements of the array. This is code atm, I've chopped off the end bit just to ensure that the array was functioning properly.

#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <ctime>

using namespace std;
using std::srand;
using std::rand;
using std::time;

void Display(int *, int);
int main(void)
	int randArray[20];

        srand( time( NULL ) ); 

	for (int i = 0; i < 20; i++) {
		randArray[i] = rand() % 20 + 1;

	cout << randArray[0] << endl;
	cout << randArray[1] << endl;
	cout << randArray[2] << endl;
	cout << randArray[19] << endl;
Am I correct in saying that I have successfully assigned a random number to all 20 elements of the array? AND printed out the values stored in elements 0, 1, 2 and 19?

Last edited by sickufully; 09-11-2015 at 12:24 AM..
sickufully is offline   Reply With Quote