Sign in to follow this  
ostamo1

is this correct

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
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
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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this