is this correct

Started by
15 comments, last by corrington_j 19 years, 7 months ago
actually unless my C++ book is wrong "if you partially initialise an array the compiler sets the remaining elements to zero" so sometype whatever[somenumber]={0} initialize the whole array to zero
Advertisement
just tested it (both are the same prog both in debug mode i only did the 1st test & added the initialization on 2nd so nothing else is changed)

1st one : nothing is initialized (so if everything is 0 in 2nd screenshoot it's not because compiler initialise at 0 by default in debug)

http://www.luclin.org/files/ranakor/array_noinitialize.jpg

& initialized they are all 0

http://www.luclin.org/files/ranakor/array_initialize.jpg
In Mike McShaffrey's "Game Coding Complete", there is a random-number generator class on page 84.

I'll give it a go tonight and see what it does...

Languages; C, Java. Platforms: Android, Oculus Go, ZX Spectrum, Megadrive.

Website: Mega-Gen Garage

the array seems kind of pointless here...

int main(){	int average = 0;	for (int x = 0; x < 10; x++)	{	     average += rand();	}	std::cout << average / 10  << std::endl;;	return 0;}
FTA, my 2D futuristic action MMORPG
using STL it would look like this:
#include <algorithm>#include <numeric>#include <iterator>#include <iostream>#include <vector>#include <cstdlib>#include <ctime>using namespace std;int main(){	srand( time( 0));	vector<float> v;	generate_n( back_inserter( v), 10, rand);	cout << accumulate( v.begin(), v.end(), 0.f) / v.size() << endl;}


cute no?
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
I've just finished the code from Game Coding Complete - it works a treat for generating random numbers.

Languages; C, Java. Platforms: Android, Oculus Go, ZX Spectrum, Megadrive.

Website: Mega-Gen Garage

not to be picky, but you should rename average to be total or something, because average never actually holds the average and is therefore a missleading name. I'm really bored, so that explains my post.

This topic is closed to new replies.

Advertisement