Initializing an Array

This is a discussion on Initializing an Array within the C++ Programming forums, part of the General Programming Boards category; Hey, I have a class that uses random numbers on initialization. I want to create an array of my type. ...

  1. #1
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229

    Initializing an Array

    Hey,

    I have a class that uses random numbers on initialization. I want to create an array of my type. So I did this:
    Code:
    asteroid myAsteroids[5];
    It works, except that all 5 of the asteroids have the same values that were randomized.
    Is there a way to initialize the array so that it creates 5 different asteroids? Sorry the question is vague, but I'm just getting into classes and I'm not concrete on how everything works.

    Thanks,
    David

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Sounds like an error in the way you use random numbers. You should call srand() exactly once, at the start of your program.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I would first seed the random number generator. Then, make sure you have rand( ) in your class constructor. Then every time you instantiate a new class object, it should have random attributes.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Quote Originally Posted by CornedBee View Post
    Sounds like an error in the way you use random numbers. You should call srand() exactly once, at the start of your program.
    When you say at the start, you mean at the entry point, right? Like main().

    Also, (I guess this gets into how data is shared between functions, which still confuses me) if It was seeded in main(), then would I still be able to use rand() in my constructor?

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Yes and yes. The seed used by rand() and initialized by srand() is global to the program. Don't use it as an example for code design.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Thanks, you helped a lot. I have one more interesting problem. It is making each asteroid random, but every time I run the program it was producing the same result. I realized it was because I was initializing the array, on the global scope, before the rand was seeded.

    I call function draw() of my asteroid class in my drawscene() function, so if it isn't declared on the global scope, then I cant use it.

    Should I initialize the array in main() then pass the array to my drawscene() function? Or is there a better way?

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    I would make it a global pointer and use new[] in a setup_level() function to actually create the array. Then I'd make it so that I can pass the number of asteroids to setup_level and set up an according number of asteroids. This way you can easily go to the next level once the current one is clear.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    You are brilliant.

    Thanks!

    edit: Worked great
    Last edited by IdioticCreation; 04-30-2007 at 03:18 PM.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    That's good. But I wasn't very accurate.

    It's actually been a long time since I last used a raw array. Nowadays I use std::vector instead. It's got many advantages. In particular in Asteroids, where the asteroids split in two when they're hit, you'll really appreciate the vector's ability to automatically get bigger.

    Also, I wouldn't use a global for the data. I'd create an object structure to hold the relevant data and pass that around as arguments.

    But especially the second issue is only relevant in larger projects than an Asteroids game.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    I originally intended to use a vector, but had trouble getting it to work, so I just went with arrays. I'll change it before I go too much further.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Initializing a 2D Array in C
    By Cell in forum C Programming
    Replies: 20
    Last Post: 03-21-2009, 12:31 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM
  5. Initializing char * array
    By Tia in forum C Programming
    Replies: 6
    Last Post: 03-11-2003, 04:19 PM

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