Question array loop

This is a discussion on Question array loop within the C++ Programming forums, part of the General Programming Boards category; I want a Integer array that holds 10 RANDOM values. Create a loop to find the average of those 10 ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    6

    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. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    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. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    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.

  4. #4
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    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. #5
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    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';
    }
    contra-productive :O

  6. #6
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    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. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    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.

  8. #8
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    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. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    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.)
    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.

  10. #10
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    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. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    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.

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

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    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. #13
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    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. #14
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    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. #15
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Quote Originally Posted by Noir
    Code:
    for ( int i = 0; i < 10; i++ ) {
      array[i] = rand();
    }
    You're spoiling the fun

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21