Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 16th, 2006, 2:09 AM   #1
veiga2
Newbie
 
veiga2's Avatar
 
Join Date: May 2006
Location: Philippines
Posts: 21
Rep Power: 0 veiga2 is an unknown quantity at this point
multi dimensional array error

i hav a problem bout arrays... why cant i allocate the variable in this array?
thnx for your help

#include<iostream>
using namespace std;


main()
{
int r=1,c=1,r2,c2;

cout<<"Enter the no. of rows you desire for matrix1:";
cin>>r;
cout<<"Enter the no. of coloumns you desire for matrix1:";
cin>>c;
cout<<"Enter the no. of rows you desire for matrix2:";
cin>>r2;
cout<<"Enter the no. of coloumns you desire for matrix2:";
cin>>c2;

if(r2>c)
cerr<<"invalid row input";


int **matri1[]=new int[r][c];// source of error<----- this is it





for(int i=0;i<r;i++)
{

for(int j=0;j<c;j++)
cin>>matri1[i][j];
}

for(int k=0;k<r;k++)
{
for(int j=0;j<c;j++)
{
cout<<matri1[k][j];
}
cout<<endl;
}







}
veiga2 is offline   Reply With Quote
Old Nov 16th, 2006, 2:52 AM   #2
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
int **matri1[]=new int[r][c];// source of error<----- this is it

new returns a pointer to whatever you just made.

you just made a 2-d array of ints.

you're trying to assign a 2-d array of ints to a pointer to a pointer to a single array of ints or some other weird crap. your types don't jive...

apple = 3;

purple = 3.14159;

this shit doesn't work...the type of the thing on the left has to be the same as the type of the thing on the right.

actually i'm not even sure what you did, but this is weird!

if you have X = Y;

then X and Y have to be the same kind of thing.
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Nov 16th, 2006, 3:05 AM   #3
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 751
Rep Power: 3 Jimbo is on a distinguished road
First off, you're making a pointer to a pointer to an array of integers. From a simplistic view (though not entirely accurate, grumpy or Narue can and have explained much better if you want the nitty-gritty) this could be simplified as either int*** or int[][][]. I'm guessing that's not what you want.

For the sake of I-should-be-in-bed-right-now, I'll just give a quick sample which should work... but if it doesn't, well, sorry
int **matri1 = new int*[r];
for(int i = 0; i < r; i++)
  matri1[i] = new int[c];
Note that I dropped the [] from the declaration of matri1. Then I initialized it as an array of int*. Then I made each of the int* into an array of size c.
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

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 On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C# corruption!!! Kilo C++ 32 May 21st, 2006 8:44 PM
Masm rsnd Assembly 4 May 20th, 2006 9:05 PM
libraries matko C 1 Jan 22nd, 2006 2:12 PM
HELP please!!! hamacacolgante C 7 Nov 21st, 2005 5:36 AM
Installing IPB 2.03 bh4575 Other Web Development Languages 0 Apr 23rd, 2005 2:36 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:35 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC