Question array loop

This is a discussion on Question array loop within the C++ Programming forums, part of the General Programming Boards category; You're spoiling the fun Heheh, what fun? It's not like this is an NP complete problem....

  1. #16
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    You're spoiling the fun
    Heheh, what fun? It's not like this is an NP complete problem.

  2. #17
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    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.
    Is that right? Hmm . . . *googles it*

    You're right; if you don't care about namespaces you can use either:
    The C International Standard specifies 18 headers which must be provided by a conforming hosted implementation. The name of each of these headers is of the form name.h. The C++ Standard Library includes the C Standard Library and, hence, includes these 18 headers. Additionally, for each of the 18 headers specified by the C International Standard, the C++ standard specifies a corresponding header that is functionally equivalent to its C library counterpart, but which locates all of the declarations that it contains within the std namespace. The name of each of these C++ headers is of the form cname, where name is the string that results when the ".h" extension is removed from the name of the equivalent C Standard Library header. For example, the header files <stdlib.h> and <cstdlib> are both provided by the C++ Standard Library and are equivalent in function, with the exception that all declarations in <cstdlib> are located within the std namespace.
    From http://publib.boulder.ibm.com/infoce.../stdcpplib.htm
    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.

  3. #18
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    You're right; if you don't care about namespaces you can use eithe
    Well now, miracles do happen. I was talking out my butt based on stuff I'd heard before, but actually being right is a nice change.

  4. #19
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    I am still bit confussed someone else on another forum said to do it this way as I am new to c++ and not yet heard or the rand function.
    Code:
    int myArray[10];
    int i;
    for ( i = 0; i < 5; i++) // it is offset so 0-9
    {
    //can add a cout here//
    myArray[i] = rand() % 100;
    }
    Surley what that is doing is saying i = o if 5 is less than 0 add one so that gonna got like count up 1,2,3,4,5, but what that doing how is that adding up myArray what is 10,20,30,40,50,60,70,80,90,100 ?

    Or is i setting up the 10 differnt myArrays then the next bit saying

    myArray[i](what will have 9 there = a randomd number modual 100 ?
    Edit/Delete Message

  5. #20
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    rand() produces a pseudo-random number. When you % it with a number, it's putting a limit on how big the number can be. This means you'll receive a number in between 0 and 99 (inclusive).

    Your index of i inside the for loop will not fill the array. It will only cycle 5 times. This means that array[0] to array[4] will be filled with random numbers from 0 to 99 while array[5] to array[9] will be filled with any arbitrary values that you cannot guarentee.

  6. #21
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    so basically what happen is it start at 0 and then increments it each time applying a random number so the loop goes 0 random number, 1 random number, so on so on untill 9 as I want an Array with 10 values

    then how would i work out the average once the loop has run throught all 9 and gave each a random value?

  7. #22
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Add them up (for example in the same loop) and divide by how many elements your array happens to have.

  8. #23
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    so if that add a random value to each array how do I then add them and divide by 10?

  9. #24
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Create a variable... call it total and set it to 0. Now write a loop that goes through every element of your array and adds it to total and stores the result inside total.

    Hint:

    Code:
    ...
    total += array[i];
    ...
    Now when you're done with that, divide total by the size of the array, and that will be the average of all numbers inside your array.

  10. #25
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Quote Originally Posted by MILLWALLL
    so if that add a random value to each array how do I then add them and divide by 10?
    That question has been answered in post #2, #4 and #13.

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

Similar Threads

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

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