7 Replies - 899 Views - Last Post: 16 January 2010 - 06:06 AM Rate Topic: -----

#1 parkour86  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 26-January 09

Multi Dimensional arrays

Post icon  Posted 15 January 2010 - 04:21 PM

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.
Is This A Good Question/Topic? 0
  • +

Replies To: Multi Dimensional arrays

#2 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

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:
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.
Was This Post Helpful? 1
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3088
  • View blog
  • Posts: 19,136
  • Joined: 14-September 07

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
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5641
  • View blog
  • Posts: 12,358
  • Joined: 16-October 07

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;


Was This Post Helpful? 1
  • +
  • -

#5 parkour86  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 26-January 09

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.
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3088
  • View blog
  • Posts: 19,136
  • Joined: 14-September 07

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

Was This Post Helpful? 0
  • +
  • -

#7 tuck  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 16
  • Joined: 11-January 10

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
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5641
  • View blog
  • Posts: 12,358
  • Joined: 16-October 07

Re: Multi Dimensional arrays

Posted 16 January 2010 - 06:06 AM

View Postparkour86, 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1