Current location: Hot Scripts Forums » Programming Languages » C/C++ » Multi-Dimensional Arrays


Multi-Dimensional Arrays

Reply
  #1 (permalink)  
Old 04-09-06, 06:13 AM
IanRandles IanRandles is offline
New Member
 
Join Date: Apr 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy Multi-Dimensional Arrays

Hi guys,

I was wondering if some1 out there could help me a little with my c++ work i gotta do!? Im a bit of a c++ rookie and am having issues setting up a multi-dimensional array in which the user can input the rows and columns. Its a relatively simple program (to which loads more has to be added!) but I cant even get this bit to work. Any help would be greatly appreciated!

int main()
{
int dx,dy;

cout << "Enter the width of the matrix\n";
cin >> dx;
cout << "Enter the height of the matrix\n";
cin >> dy;

int MyArray[dx][dy][5] = {0};

for (int i=0; i<dx; i++)
for (int j=0; j<dy; j++)
for (int k=0; k<5; k++)
{
cout << "MyArray[" << i << "][ " << j << "]: ";
cout << MyArray[i][j][k] << endl;
}
return 0;
}
Reply With Quote
  #2 (permalink)  
Old 04-27-06, 04:50 AM
sasidhar sasidhar is offline
Newbie Coder
 
Join Date: Apr 2006
Location: India
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
The mistake lies here

In the program that u had given..
int main()
{
int dx,dy;

cout << "Enter the width of the matrix\n";
cin >> dx;
cout << "Enter the height of the matrix\n";
cin >> dy;

int MyArray[dx][dy][5] = {0};

for (int i=0; i<dx; i++)
for (int j=0; j<dy; j++)
for (int k=0; k<5; k++)
{
cout << "MyArray[" << i << "][ " << j << "]: ";
cout << MyArray[i][j][k] << endl;
}
return 0;
}



int MyArray[dx][dy][5] = {0};

Is the mistake..
An array can never be assigned to a single element.
Dont give that "=0"at the end ..
That will do.
Reply With Quote
  #3 (permalink)  
Old 04-27-06, 04:51 AM
sasidhar sasidhar is offline
Newbie Coder
 
Join Date: Apr 2006
Location: India
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
The mistake lies here

In the program that u had given..
int main()
{
int dx,dy;

cout << "Enter the width of the matrix\n";
cin >> dx;
cout << "Enter the height of the matrix\n";
cin >> dy;

int MyArray[dx][dy][5] = {0};

for (int i=0; i<dx; i++)
for (int j=0; j<dy; j++)
for (int k=0; k<5; k++)
{
cout << "MyArray[" << i << "][ " << j << "]: ";
cout << MyArray[i][j][k] << endl;
}
return 0;
}



int MyArray[dx][dy][5] = {0};

Is the mistake..
An array can never be assigned to a single element.
Dont give that "={0}"at the end ..
That will do.

And u will get your required output.
Reply With Quote
  #4 (permalink)  
Old 04-28-06, 05:15 AM
King Coder King Coder is offline
Community VIP
 
Join Date: Jan 2006
Posts: 703
Thanks: 0
Thanked 0 Times in 0 Posts
It looks like you want to initialize the elements to zero. C++ should automatically do that for you, so there is no need for the assignment statement (plus it should be illegal).
__________________
my site
Reply With Quote
  #5 (permalink)  
Old 05-21-06, 09:06 PM
appu's Avatar
appu appu is offline
New Member
 
Join Date: May 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by sasidhar
int MyArray[dx][dy][5] = {0};

Is the mistake..
An array can never be assigned to a single element.
Dont give that "={0}"at the end ..
There is no problem in above statement. You can very well do that.
Reply With Quote
  #6 (permalink)  
Old 05-21-06, 09:08 PM
appu's Avatar
appu appu is offline
New Member
 
Join Date: May 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by King Coder
It looks like you want to initialize the elements to zero. C++ should automatically do that for you, so there is no need for the assignment statement
Only global arrays get initialized automatically not the local one's.
Quote:
Originally Posted by King Coder
(plus it should be illegal).
I don't think so Wiz.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
2 dimensional arrays pikaz PHP 2 01-24-06 06:32 PM
Arrays with sub arrays perleo PHP 1 09-30-05 07:15 PM
arrays and drop boxes minority JavaScript 1 09-06-05 01:14 PM
Can record arrays on database? mhs12grade1992 PHP 5 02-17-05 11:20 AM
arrays in functions Slypher PHP 3 01-30-05 05:36 AM


All times are GMT -5. The time now is 04:35 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.