is this correct
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
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
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...
I'll give it a go tonight and see what it does...
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;}
using STL it would look like this:
cute no?
#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?
I've just finished the code from Game Coding Complete - it works a treat for generating random numbers.
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
Popular Topics
Advertisement