I have an assignment that I'm working on, and I'm kind of at a loss. I'm supposed to take the arrays listed below and write a function that can deal with any one of them (user's choice) and then output the contents. I know how to use a for loop to output the contents, but I'm having a hard time wrapping my head around how the function chooses an array. I can have the user input the name of the array, but then how do I take that name and use it to access the array via the function? I have a feeling this is very simple and I'm just over-complicating it in my head. I've read up on arrays online and in my textbook, and I'm just not coming up with the answer. I'm still very new to this.
The minor dimension is always the same, so it's easy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void foo ( double array[][numberOfLaps], size_t len ) {
for ( size_t i = 0 ; i < len ; i++ ) {
for ( size_t j = 0 ; j < numberOfLaps ; j++ ) {
}
}
}
int main ( ) {
double seconds[30][numberOfLaps];
double topspeed[15][numberOfLaps];
double mpg[100][numberOfLaps];
double drivers[50][numberOfLaps];
// tell the function what the major dimension size is.
foo(seconds,30);
foo(topspeed,15);
foo(mpg,100);
foo(drivers,50);
}
size_t is 'usually' a map to unsigned int.
There are literally nearly 100 names for 8 integer types (signed 1,2,4,8 byte and again for unsigned) if you include unix and Microsoft and C names. Most of the time it does not matter too much. When it does matter, you have to be very careful about it due to portability issues: for those I like the C names ( uint64_t for example) as they specify # of bits.
size_t is unusual: its part of c++ (not an extension) and its used a LOT inside c++ for things that have a size (of course). You want to make friends with it or you will get warnings where you have ints = size_ts when hacking out code carelessly.