Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multi-dimensional arrays

Status
Not open for further replies.

ewan91

Programmer
Jun 25, 2001
13
0
0
GB
hi

i have this code which i think will access a multi-dimensional array, however it comes out with -85763640!!

it is
the main function
Code:
#include <iostream.h>
#include &quot;lookUp.h&quot;

void main( )
{
	lookUp look;
	cout << &quot;hello world&quot; << endl;
	cout << look.labSeats( 3 ) << &quot;  &quot; << endl;

}

part of lookUp.cpp

Code:
int multiArray [5][4] = {
		{567,13,53,26},  
		{557,19,57,26},
		{552,23,58,26},
		{544,30,59,26},
		{467,108,56,28},
		};

int lookUp::labSeats( int j )
{
	int lab = multiArray[j][0];
	cout << lab << endl;
	return lab;
}

does any one know what i'm doing wrong

thanks
ewan
 
Well... just a guess right now but where is the initialization of multiarray being done? My guess is you have it declared as a private data member but the initialization above is not initializing the member data. Instead it is declaring a global most likely in the cpp. WHen the class is called it is returning the un-initialized multiArray.

Try using

int lookUp::multiArray[5][4] ={...
};

Matt
 
Hi
Even if multiArray is initialised as a global in the lookUp.cpp its giving correct results .I don't know why its not working for you. Since I am able to get the correct results with your code.
 
I agree with Zyrenthian.
But it's not sure because lookUp.h was not listed.

I think class 'lookUp' has the data member which
name is 'multiArray' and if so, 'multiArray'
used in the method labSeats is not global
'multiArray' but the data member contained in
the class 'lookUp'.

If multiArray declared in the class 'lookUp' is
a static member, the code suggested by Zyrenthian
is suitable. If it's not a static member, you
need to write constructor to initialize it.
Hee S. Chung
heesc@netian.com
 
Hi,

If try to run the program using VC++ compiler, it will give u the expected result which is 544.

hsk007 :cool:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top