Sign in to follow this  

is this correct

This topic is 4884 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 need to create an integer array that holds 10 random values. Create a loop to find the average of those 10 values and print the result. i was wondering if my code was correct
#include <iostream>

main()
{
	int intarray[10]={0};
	int x = 0;
	int average = 0;
	for (x; x < 10; x++)
	{
		intarray[x] = rand();
		
		average += intarray[x];
		
	}
	std::cout << average/10  << std::endl;;
	return 0;
}


Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Looks alright to me, why don't you try running it and see if it works? (Unless you know of some specific problem with it already...)

Share this post


Link to post
Share on other sites
int intarray[10]={0};

I don't think that's valid. Try:

int intarray[10];


Also, you have one too many semicolons on the second last line.

Share this post


Link to post
Share on other sites
it works i was just checking to see if i was doing it the right way and to make sure i understood the assignment thats all
thanks for the reply

int intarray[10]={0};
that line initializes the whole array to zero

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You allways get the same number becouse you dont seed the random number generator. You should call srand() with a different value every time at the start of the program.

Share this post


Link to post
Share on other sites
Seed it with the system time, since that changes everytime you run the program:


#include <time.h>

srand( (unsigned)time( NULL ) )

Share this post


Link to post
Share on other sites
Quote:
Original post by ostamo1
it works i was just checking to see if i was doing it the right way and to make sure i understood the assignment thats all
thanks for the reply

int intarray[10]={0};
that line initializes the whole array to zero


Actually it only sets intarray[0] to 0. YOur compiler in debug mode is likely zeroing the array, so it looks zeroed out to you. Try changing it to int intarray[10]={1}; and you will see what I mean. The correct way to zero it is with meset, a for loop or int array[0]={0,0,0,0,0,0,0,0,0,0}; .

// Only the first element will be one
#include <iostream>

int main()
{
int a[10]={1};
for (int i=0; i<10; ++i)
std::cout << "a["<<i<<"]="<< a[i]<< std::endl;;
std::cin.ignore();
return 0;
}


Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
I've just finished the code from Game Coding Complete - it works a treat for generating random numbers.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this