# Question array loop

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 03-24-2007
MILLWALLL
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.
• 03-24-2007
KONI
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
• 03-24-2007
dwks
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.
• 03-24-2007
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';
}

:D
• 03-24-2007
KONI
Quote:

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';
}

:D

contra-productive :O
• 03-24-2007
Noir
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. :)
• 03-24-2007
dwks
It's <cstdlib> BTW.

I'm sure if the OP turns that in (assuming it's homework) the teacher would be suitably impressed . . . :)
• 03-24-2007
Noir
Quote:

It's <cstdlib> BTW.
Either works.
Quote:

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. :)
• 03-24-2007
dwks
Quote:

Quote:

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.)
• 03-24-2007
Noir
Quote:

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. ;)
Quote:

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. :)
• 03-24-2007
dwks
Quote:

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.

Quote:

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". :)

Quote:

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. :)
• 03-24-2007
MILLWALLL
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 ?
• 03-24-2007
KONI
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.
• 03-24-2007
Noir
Quote:

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. ;)
Quote:

I suppose . . . but don't get me started with "using namespace std".
I've probably heard it before anyway. :)
Quote:

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. :)
Quote:

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();
}

• 03-24-2007
KONI
Quote:

Originally Posted by Noir
Code:

for ( int i = 0; i < 10; i++ ) {
array[i] = rand();
}

You're spoiling the fun :)
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last