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.
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 = newint[sizeOfX*sizeOfY];
Accessing 0 to every cell of the array
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;
//}
//********** 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?
int row1,col1,i; // undefined values
int** a= newint *[row1]; // row1 is undefined
for (int i=0; i<row1; i++)
{
a[i]= newint[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.