Is it possible to have both strings and integers in a multi dimensional array? I have searched on google but all i can find is mostly integer arrays.
Multi Dimensional arrays
Page 1 of 17 Replies - 778 Views - Last Post: 16 January 2010 - 06:06 AM
Replies To: Multi Dimensional arrays
#2
Re: Multi Dimensional arrays
Posted 15 January 2010 - 04:59 PM
I'm pretty sure it's not possible to store two different data types in the same multi-dimensional array.
According to one of my C++ books, a 2D array may be declared in C++ like this example:
Note that the data type of the array is double. You could not store a char or something in there, then.
Also, look at the way char data is stored in a 2D char array, as in this example. There is no way to have 2 different data types in that array, I guess.
Edit: or did you mean "can you store strings in a multidimensional array, like you can ints?" I don't see why not.
According to one of my C++ books, a 2D array may be declared in C++ like this example:
const int NUM_DIVS = 3; cont int NUM_QTRS = 4; double sales[NUM_DIVS][NUM_QTRS]; //2D array with 3 rows and 4 columns
Note that the data type of the array is double. You could not store a char or something in there, then.
Also, look at the way char data is stored in a 2D char array, as in this example. There is no way to have 2 different data types in that array, I guess.
Edit: or did you mean "can you store strings in a multidimensional array, like you can ints?" I don't see why not.
#3
Re: Multi Dimensional arrays
Posted 15 January 2010 - 05:02 PM
Options:
1. Wrap the two data types in a struct/class
2. Use a 2D string/char array and parse the integers out
1. Wrap the two data types in a struct/class
2. Use a 2D string/char array and parse the integers out
#4
Re: Multi Dimensional arrays
Posted 15 January 2010 - 05:30 PM
You probably don't want a multi dimensional array.
struct Item {
string s;
int i;
};
Item[10] items;
items[0].s = "foo";
items[0].i = 52;
#5
Re: Multi Dimensional arrays
Posted 15 January 2010 - 05:35 PM
I'm doing a homework assignment in CS and my teacher wants us to use a multi dimensional array.
#6
Re: Multi Dimensional arrays
Posted 15 January 2010 - 06:01 PM
You cannot have one dimension be one variable type and the second another. I guess you cant use other data structures? A map comes to mind.
This post has been edited by KYA: 15 January 2010 - 06:01 PM
#7
Re: Multi Dimensional arrays
Posted 15 January 2010 - 11:38 PM
Couldn't you have a char * array and then determine the value needed at some location within the array, like array1[x][y] or array2[x][y][z]? Something like isnum(array1[x][y]) or isalpha(array1[x][y]), then process that value accordingly.
tuck
tuck
#8
Re: Multi Dimensional arrays
Posted 16 January 2010 - 06:06 AM
parkour86, on 15 Jan, 2010 - 06:35 PM, said:
I'm doing a homework assignment in CS and my teacher wants us to use a multi dimensional array.
And they want multiple types? Any array, regardless of dimensions, may only have one type. However, that type can be anything you like. It can be any array of struct. It can be an array of (void *) and the value pointed to could whatever. Ideally, though, programming with some structure in mind is the saner way to go.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote






|