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!
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 structOriginally Posted by jerrykelleyjr
Code:typedef struct bus { int ID; float speed; }Bus; Bus Fleet[20];
What does "fleet" do?
Thanks!
>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.
OK Prelude. Stupid question. What if speed is an array of 500. Is this how I would declare it?
Thanks!Code:typedef struct bus { int ID; float speed[500]; }Bus; Bus Fleet[20];
Yes, you can do it.
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
>What if speed is an array of 500. Is this how I would declare it?
Let's see:
No complaints here.Code:$ prelude test.c 7: warning: No entry point detected. It's called main. It returns int. All the cool kids are doing it.
My best code is written with the delete key.
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?
Thanks!Code:typedef struct bus { int ID; float parameter[500][20]; }Bus; Bus Fleet[20];
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:
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.Code:struct bus { /* Other stuff */ float parameter[20][500]; }; struct bus fleet[20];
My best code is written with the delete key.
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
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...
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
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!
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
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
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.
Have to agree with Salem (Hmm... could be something else?)
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler