array question...

This is a discussion on array question... within the C Programming forums, part of the General Programming Boards category; If I have a float type variable for a bus and there can be up to 20 buses to keep ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    77

    array question...

    If I have a float type variable for a bus and there can be up to 20 buses to keep track of and I want to store and keep track of one float type parameter for each (speed) is it correct to have the array?

    float bus[10][20];

    Thanks!

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by jerrykelleyjr
    If I have a float type variable for a bus and there can be up to 20 buses to keep track of and I want to store and keep track of one float type parameter for each (speed) is it correct to have the array?

    float bus[10][20];

    Thanks!
    I would take another approach I would use a struct
    Code:
    typedef struct bus
    {
     int ID;
     float speed;
    }Bus;
    
     Bus Fleet[20];

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    What does "fleet" do?

    Thanks!

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >What does "fleet" do?
    Whatever you tell it to do. It's an array variable, just like your bus array.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    OK Prelude. Stupid question. What if speed is an array of 500. Is this how I would declare it?

    Code:
    typedef struct bus
    {
     int ID;
     float speed[500];
    }Bus;
    
     Bus Fleet[20];
    Thanks!

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Yes, you can do it.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >What if speed is an array of 500. Is this how I would declare it?
    Let's see:
    Code:
    $ prelude test.c
    7: warning: No entry point detected. It's called main. It returns int. All the cool kids are doing it.
    No complaints here.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    another array question along the same line.
    I have to keep track of 20 buses. Each bus has up to 20 parameters of type float that I need to keep track of. Each parameter is really a history of data so there could be 500 pieces of data I need to store for each parameter for each bus. Would this structure be correct?

    Code:
    typedef struct bus
    {
     int ID;
     float parameter[500][20];
    }Bus;
    
     Bus Fleet[20];
    Thanks!

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Let me get this straight. You have up to 20 buses, which can have up to 20 parameters, which store a history of up to 500 floating-point values? In that case you're looking at something like this if you want to use arrays:
    Code:
    struct bus {
      /* Other stuff */
      float parameter[20][500];
    };
    
    struct bus fleet[20];
    Basically just a reversal of the dimension sizes that you had before, but at this point you may have a lot of wasted space if the histories remain short.
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If each bus has 20 parameters, and each parameter has up to 500 data items, then it would be:

    parameters[20][500].

    Is it possible that the 500 history data items could be consolidated, or represented, with a smaller amount of data items?

    Adak

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    let me see sizeof(float)*20*20*500 ...
    A little bit too much to allocate on the stack at once?

    Big chance to get stack overflow just entering the function there such allocation occures...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    Prelude it's true that there may never be 500 data pieces for each parameter...but there could be. Is there an elegant way to handle this type of situation?
    Thanks!

  13. #13
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    You can use malloc (for each parameter and bus)
    Start with some reasonable value according to you knowledge about statistics behavior.

    And only if some statistics achieves the original size - increase only the required array with realloc.

    Of course such approach requires more careful planning and memory management to avoid unnecessary memory movements, memory leaks and accidental null pointer assignments
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    I would suggest you get it all working with fixed sized arrays.

    Making them variable at the end is an easy change, but you really don't want to have to debug lots of other memory problems at the same time as everything else.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Have to agree with Salem (Hmm... could be something else?)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 06:07 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 05:27 PM

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