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.
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.
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
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.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
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 :OOriginally Posted by 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.
It's <cstdlib> BTW.
I'm sure if the OP turns that in (assuming it's homework) the teacher would be suitably impressed . . .
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Either works.It's <cstdlib> BTW.
That's why I posted it. I like impressing teachers who aren't my teachers but end up getting my code for assignments anyway.I'm sure if the OP turns that in (assuming it's homework) the teacher would be suitably impressed . . .
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.)Either works.It's <cstdlib> BTW.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Yeah, cstdlib is really just this:I was under the impression that <[c-header-file].h> should be <c[c-header-file]> in C++ to support namespaces.
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.Code:#ifndef CSTDLIB #define CSTDLIB namespace std { #include <stdlib.h> } #endif
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.It's like <iostream.h> vs <iostream>. (Sort of. <iostream.h> is deprecated, <stdlib.h> is not.)
On your compiler . . . maybe not mine. But that would be the simplest way to implement it.Originally Posted by Noir
I suppose . . . but don't get me started with "using namespace std".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 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.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.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
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 ?
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):
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.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;
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.On your compiler . . . maybe not mine. But that would be the simplest way to implement it.
I've probably heard it before anyway.I suppose . . . but don't get me started with "using namespace std".
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.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.
You get a random number by calling rand() from stdlib.h (or cstdlib). Just do that in a loop:just been watching a few c++ video and it says to try do this could it also be done like this ?
Code:for ( int i = 0; i < 10; i++ ) { array[i] = rand(); }
You're spoiling the funOriginally Posted by Noir