1. ## Question array loop

I want a Integer array that holds 10 RANDOM values.
Create a loop to find the average of those 10 values and print the result.

How would I do this.

2. 1. create an integer array of size 10
2. iterate over the array and fill it with a random number
3. sum the numbers
4. divide the sum by 10

3. How do I... (Level 1) - Generate random numbers?
Arrays tutorial

Note that if you want a floating-point average, be sure to divide by 2.0, not 2.

4. Code:
```#include <algorithm>
#include <iostream>
#include <numeric>
#include <stdlib.h>

int main() {
int array[10];
std::generate_n( array, 10, rand );
std::cout << std::accumulate( array, array + 10, 0 ) / 10 <<'\n';
}```

5. Originally Posted by Noir
Code:
```#include <algorithm>
#include <iostream>
#include <numeric>
#include <stdlib.h>

int main() {
int array[10];
std::generate_n( array, 10, rand );
std::cout << std::accumulate( array, array + 10, 0 ) / 10 <<'\n';
}```
contra-productive :O

6. I couldn't think of a way to offer code without solving it. The problem's too easy. So I went with a fun solution.

7. It's <cstdlib> BTW.

I'm sure if the OP turns that in (assuming it's homework) the teacher would be suitably impressed . . .

8. It's <cstdlib> BTW.
Either works.
I'm sure if the OP turns that in (assuming it's homework) the teacher would be suitably impressed . . .
That's why I posted it. I like impressing teachers who aren't my teachers but end up getting my code for assignments anyway.

9. It's <cstdlib> BTW.
Either works.
I was under the impression that <[c-header-file].h> should be <c[c-header-file]> in C++ to support namespaces. It's like <iostream.h> vs <iostream>. (Sort of. <iostream.h> is deprecated, <stdlib.h> is not.)

10. I was under the impression that <[c-header-file].h> should be <c[c-header-file]> in C++ to support namespaces.
Yeah, cstdlib is really just this:
Code:
```#ifndef CSTDLIB
#define CSTDLIB

namespace std {
#include <stdlib.h>
}

#endif```
There's not really any difference except the name suddenly goes into std instead of being global. I guess it makes things consistent with the rest of the C++ library, but if you do 'using namespace std' anyway, it doesn't matter at all and if you don't, you have less typing to do.
It's like <iostream.h> vs <iostream>. (Sort of. <iostream.h> is deprecated, <stdlib.h> is not.)
Not really. iostream.h doesn't have to be supported at all because it's not a part of the standard, but stdlib.h does because it is. stdlib.h is the one that's deprecated, but for compatibility with C and code that's already been written, it'll just stay deprecated forever. I think I got all that right, someone who really knows should double check me.

11. Originally Posted by Noir
Yeah, cstdlib is really just this:
Code:
```#ifndef CSTDLIB
#define CSTDLIB

namespace std {
#include <stdlib.h>
}

#endif```
On your compiler . . . maybe not mine. But that would be the simplest way to implement it.

There's not really any difference except the name suddenly goes into std instead of being global. I guess it makes things consistent with the rest of the C++ library, but if you do 'using namespace std' anyway, it doesn't matter at all and if you don't, you have less typing to do.
I suppose . . . but don't get me started with "using namespace std".

Not really. iostream.h doesn't have to be supported at all because it's not a part of the standard, but stdlib.h does because it is. stdlib.h is the one that's deprecated, but for compatibility with C and code that's already been written, it'll just stay deprecated forever. I think I got all that right, someone who really knows should double check me.
I don't "really know", but I think that you're assuming that all C++ compilers can compile C as well. For a C++ compiler, nothing says that it has to have <stdlib.h>, though I can't think of a reason it wouldn't.

12. its not homework lol just been watching a few c++ video and it says to try do this could it also be done like this ?

#include <iostream>
int main(){

//intilize my array with 10 values
int my array[10];

//Random value to array
1[0] = 5;
2[1] =10;
//so on so on

//how would I loop this ?

13. As a semi-regular board member, you know you should use code tags when it comes to posting code.

What you're suggesting is using the standard integer arrays. The code would look like the following (pseudo-C):

Code:
```int myArray[10];
int i, sum = 0;
double result;

/* fill the array with values */
for (...)
{
myArray[i] = random_number();
}

for (...)
{
sum = ... + myArray[i];
}

result = sum / 10;```
Of course this could be done entirely without arrays, but since you said you liked it that way, I wrote two loops,one to fill the array and one to do the calculations.

14. On your compiler . . . maybe not mine. But that would be the simplest way to implement it.
I don't even know if it's that way on my compiler, but that's logically how it works and I didn't want to type out the contents of stdlib.h.
I suppose . . . but don't get me started with "using namespace std".
I've probably heard it before anyway.
I don't "really know", but I think that you're assuming that all C++ compilers can compile C as well. For a C++ compiler, nothing says that it has to have <stdlib.h>, though I can't think of a reason it wouldn't.
Nope, C++ compilers have to support all of the C headers as well as the C++ version of those headers, but the C headers are deprecated.
just been watching a few c++ video and it says to try do this could it also be done like this ?
You get a random number by calling rand() from stdlib.h (or cstdlib). Just do that in a loop:
Code:
```for ( int i = 0; i < 10; i++ ) {
array[i] = rand();
}```

15. Originally Posted by Noir
Code:
```for ( int i = 0; i < 10; i++ ) {
array[i] = rand();
}```
You're spoiling the fun