2d dynamic array help please?

Hi,

I am writing a program that deals with 2d arrays. The program inputs the number of rows and columns and asks for the entries. When the program run and compiles it works perfectly until it outputs then it gives me a warning. Im new to this and would like any help or opinions.

Here is my code:

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{

int row1=0,col1=0,i,j;
//int a[row1][col1];

int** a= new int*[row1];
for (int i=0; i<row1; i++)
{
a[i]= new int[col1];
}

cout<<"Enter the rows and colums"<<endl;
cin>>row1>>col1;



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

for (int j=0;j<col1;j++)
{
cout<<"Enter numbers for the matrix"<<endl;
cin>>a[i][j];
cout<<endl;
}
}

//Display the matrix

for (int i=0;i<row1;i++)
{
for (int j=0;j<col1;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<endl;


//Deallocate
for (int i=0; i<row1; i++)
{
delete a[i];
}

}

cout<<" Number of Rows :"<<row1<<endl;
cout<<" Number of Columns :"<<col1<<endl;

//delete [] a[] ;

return 0;
}

I am learning how to do this before I can move on so it can read a text file of numbers.

Also I am having problems with ////delete [] a[];///// I took it out because it made my code compile and run but when I add it in, it gives me an error:


matrixtesting.cpp|56|error: expected primary-expression before ']' token|

I know this expression is suppose to deallocate the array.

Thanks for any help in advance.
First, please use the code tags for readability. See http://www.cplusplus.com/articles/jEywvCM9/
(You can edit your post.)


The delete must match the allocation.
You have:
1
2
int* * a;
a = new int*[row1];

The a is a pointer that holds the address of a block of memory. Therefore:
delete [] a;


It seems like you have a closing brace in wrong place. (Having intuitive indentation with code tags really helps "seeing" the code.
I think you didn't delete the array as you should.
1
2
3
4
int** a= new int*[row1];
for (int i=0; i<row1; i++){
   a[i]= new int[col1];
}

Then
1
2
3
4
for (int i=0; i<row1; i++){
   delete[] a[i];
}
delete [] a ;


Also line int row1=0,col1=0,i,j; declares variables i,j which you also declare again in each for loops (for (int i=0;i<row1;i++)). It is not error because this i is different variable then is in for loop (different scope). Just to be careful I would delete them from that line.


Much better use of 2D array is in my oppinion this:
Creating
int* a = new int[sizeOfX*sizeOfY];
Accessing 0 to every cell of the array
1
2
3
4
5
for(int x=0;x<sizeOfX;++x){
   for(int y=0;y<sizeOfY;++y){
      a[x + y*sizeOfX] = 0;
   }
}

Deleting
delete[] a;

3D is simple
a[x + y*sizeOfX + z*sizeOfX*sizeOfY];
Thanks guys, I got it working. How different would it be if i wanted to fstream it? would i have to change all the cin to fin?
No different at all; both are istream objects.

You should check whether each read operation succeeds, but that is equally true with cin.
***Update***

I was able to output a matrix by reading the text file.

Here is my code:

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{
int row1,col1,row2,col2,i,j;
int** a= new int *[row1];
//int** b= new int *[row2];

for (int i=0; i<row1; i++)
{
a[i]= new int[col1];
}

//for (int i=0; i<row2; i++)
//{
// b[i]= new int[col2];
//}

ifstream matrix("matrixnum.txt");

if (!matrix)
{
cout<<"The file cannot be open"<<endl;
return 1;
}
matrix>>row1>>col1;

//****************************** Set up for matrix a ************************************
for (int i=0; i<row1; i++)
{
for (int j=0; j<col1; j++)
{
matrix>>a[i][j];
}
}
//**************************** Set up for matrix b ****************************************************
//for (int i=0; i<row2; i++)
//{
// for (int j=0; j<col2; j++)
// {
// matrix>>b[i][j];
// }
//}
//********************* Display matrix a *********************************************************
for (int i=0; i<row1; i++)
{
for (int j=0; j<col1; j++)
{
cout<<a[i][j]<<"\t";
}

cout<<endl;
}
//********************* Display matrix b ********************************************************
//for (int i=0; i<row2; i++)
//{
// for (int j=0; j<col2; j++)
// {
// cout<<b[i][j]<<"\t";
// }

// cout<<endl;
//}
//********** Deallocate a ******************************************************************************

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

}
//**************************** Deallocate b *************************************************

//for (int i=0; i<row2; i++)
//{

//}

cout<<"Number of rows for matrix a:"<<row1<<endl;
cout<<"Number of columns for matrix a:"<<col1<<endl;
//cout<<"Number of rows for matrix b:"<<row2<<endl;
//cout<<"Number of columns for matrix b:"<<col2<<endl;

delete [] a;
//delete [] b;

return 0;

}

The problem is, i need to output another matrix possibly a 1x2 but when i tried to do it, i received an error that said could not allocate error. The lines that I opted out are the ones that gave me the error. What can i do to make it output another matrix?

Thanks in advance
Also now, when i make a matrix that more than 4x4, the numbers are not good. I am also trying to make it so that it can read and output doubles.

Please help
Your code contains:
1
2
3
4
5
6
7
8
9
int row1,col1,i; // undefined values
int** a= new int *[row1]; // row1 is undefined
for (int i=0; i<row1; i++)
{
  a[i]= new int[col1]; // col1 is undefined
}

ifstream matrix("matrixnum.txt");
matrix>>row1>>col1; // Now you set row1 and col1 

A point of dynamic allocation is that you can do it at runtime after you have acquired the size information.

It was sheer (un)luck that the program appered to function with some input.


PS. Use the code tags, please.
Topic archived. No new replies allowed.