#include <iostream>
#include <cstdlib>
usingnamespace std;
int main()
{
int row,column,i,j;
int**matrix; //Declares 'matrix' as a pointer to pointers.
//Ask for and get user input:
cout<<"Enter number of rows: ";
cin>>row;
cout<<"Enter number of columns: ";
cin>>column;
//create the matrix:
matrix=newint*[row]; //Creates an array of pointers.
for(i=0;i<row;i++)
matrix[i]=newint[column]; //Each pointer points to another array.
//Initialize all elements to zero:
for(i=0;i<row;i++)
{
for(j=0;j<column;j++)
matrix[i][j]=0;
}
matrix[row-1][column-1]=1;
//Print out the matrix:
for(i=0;i<row;i++)
{
for(j=0;j<column;j++)
cout<<matrix[i][j]<<" ";
cout<<endl;
}
system("pause");
}
example output:
Enter number of rows: 5
Enter number of columns: 4
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 1
Press any key to continue . . .
thanks for the reply.......actually i want to cluster the data.....if u want cluster the data u must break the data according to their respective group.Therefore every group has different row or different colums.I have seen every example only shows that every row has same column.such as:
row1 has 4 column
row2 has 4 column
row3 has 4 column
0 0 0 0
0 0 0 0
0 0 0 0
but i want every row has different column.such as:
row1 has 3 column
row2 has 2 column
row3 has 4 column
0 0 0
0 0
0 0 0 0
it is possible to do it?
im sorry.....im am beginner in this field....thanks for ur help.:)
I'm no expert myself but I came up with the code below. I decided to use the vector class instead of the new[] operator, so that I wouldn't need to store the sizes of each row vector to print out the matrix:
#include <cstdlib>
#include <iostream>
#include <vector>
usingnamespace std;
int main()
{
int i,j;
vector<vector<int> >matrix; //Creates a vector called 'matrix' whose elements are integer vectors.
vector<int>row1(3,0); //Creates an integer vector called 'row1' with 3 elements initialized to 0.
matrix.push_back(row1); /*Adds the 'row1' vector to the end of the 'matrix' vector,
making the 'row1' vector the first element of the 'matrix' vector.*/
vector<int>row2(2,0); //Creates an integer vector called 'row2' with 2 elements initialized to 0.
matrix.push_back(row2); /*Adds the 'row2' vector to the end of the 'matrix' vector,
making the 'row2' vector the second element of the 'matrix' vector.*/
vector<int>row3(4,0); //Creates an integer vector called 'row3' with 4 elements initialized to 0.
matrix.push_back(row3); /*Adds the 'row3' vector to the end of the 'matrix' vector,
making the 'row3' vector the third element of the 'matrix' vector.*/
//Print out the matrix:
for(i=0;i<3;i++)
{
for(j=0;j<matrix[i].size();j++)
cout<<matrix[i][j]<<" ";
cout<<endl;
}
cout<<endl;
system("pause");
return 0;
}
And here's the output:
0 0 0
0 0
0 0 0 0
Press any key to continue . . .
I don't really see the point of this though. You might as well just create a 3x4 array and only print out the elements you want to be shown for each row.
thanks for ur reply......when we need to cluster some data to groups.....actually we dont know the length of columns and rows......that my target.......i want to create the unknomn lenght of columns and rows........
sometimes our data are as follows-
0 0 0
0 0
0 0 0 0
and sometimes our data are as follows-
0 0 0 0 0
0 0 0
0 0 0 0
and sometimes our data are as follows-
0 0 0 0 0
0 0 0 0 0 0
0 0 0 0
it is possible to do?.....im sorry.....im am beginner in this field....thanks for ur help.....:)
thanks for ur reply..........now this I have related projects using mobile robot......when i run my mobile robot.....i will get the data.......and i need cluster the data to groups......and my target to create an array using vector or pointer with unknown length of columns and rows.....
sometimes our data are as follows-
0 0 0
0 0
0 0 0 0
and sometimes our data are as follows-
0 0 0 0 0
0 0 0
0 0 0 0
and sometimes our data are as follows-
0 0 0 0 0
0 0 0 0 0 0
0 0 0 0
it is possible to do?.....im sorry.....im am beginner in this field....thanks for ur help.....:)
I don't really understand what it is you're asking for. You want to know how to create a 2D array with unknown row and column lengths? Why? I don't know what that would accomplish, if it's even possible. Please be more descriptive. Give an example of the input and desired output.
EDIT: You still haven't provided example output. Give an example of the input and the desired output. Otherwise I don't know how to help you.
thanks for ur reply.......before this i use matlab to make a simulation.......when we use matlab to store data in the array...........we do not need to declare the lenght of row and column............but when i using c++.........firstly we need to declare the lenght of column and row.........
i alreay make an experiment using vector and pointer......when i declare row=2 and column=2.......i only have 3 data and store the data such as:
if i using pointer it display-:
1 2
3 -9.25596e+061
why it happen like that because no data is store at row[1]column[1]?
we consider the first row is first group.....and the the second row is second group
i will use the data in the first group to make a calculation...........but the problems is at second group.......how we want to know how much data we actually store at second group?
pardon me for not being able to explain my problem well.......thnak for ur help....:)
if i using pointer it display-:
1 2
3 -9.25596e+061
why it happen like that because no data is store at row[1]column[1]?
Yes, that is why.
we consider the first row is first group.....and the the second row is second group
i will use the data in the first group to make a calculation...........but the problems is at second group.......how we want to know how much data we actually store at second group?
To find the number of elements in each group/row, just use vector::size:
And if you want to know whether there is a data value stored in the matrix at a certain coordinate before performing a calculation you can create a parallel matrix whose elements take one of the two boolean values true or false depending on whether or not there's a data value stored in the corresponding element of the data matrix. Then use if statements when performing calculations. Make sure the parallel matrix is large enough to accommodate for the maximum amount of rows and columns.
Let's do an example.
Assume that we have a file, data.txt, that has the following data:
#include <cstdlib>
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
usingnamespace std;
int main()
{
int i,j,element,sum=0;
bool isThereData[10][10];
string line;
stringstream ss;
ifstream infile;
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
isThereData[i][j]=false;
}
infile.open("data.txt");
vector<vector<int> >matrix;
i=0;
while(!infile.eof())
{
getline(infile,line);
ss<<line;
vector<int>row;
j=0;
while(!ss.eof())
{
ss>>element;
row.push_back(element);
isThereData[i][j]=true;
j++;
}
ss.clear();
matrix.push_back(row);
i++;
}
infile.close();
cout<<"The matrix is: "<<endl<<endl;
for(i=0;i<matrix.size();i++)
{
for(j=0;j<matrix[i].size();j++)
cout<<matrix[i][j]<<" ";
cout<<endl;
}
cout<<endl;
cout<<"The number of rows in the matrix is: "<<matrix.size()<<endl;
for(i=0;i<matrix.size();i++)
cout<<"The number of elements in row "<<i+1<<" is: "<<matrix[i].size()<<endl;
cout<<endl;
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(isThereData[i][j]==true)
sum=sum+matrix[i][j];
}
if(isThereData[i][0]==true)
{
cout<<"The sum of the elements in row "<<i+1<<" is: "<<sum<<endl;
sum=0;
}
}
cout<<endl;
system("pause");
return 0;
}
The output is:
The matrix is:
1 2 3
4 5
6 7 8 9 10
2 8 5 3
The number of rows in the matrix is: 4
The number of elements in row 1 is: 3
The number of elements in row 2 is: 2
The number of elements in row 3 is: 5
The number of elements in row 4 is: 4
The sum of the elements in row 1 is: 6
The sum of the elements in row 2 is: 9
The sum of the elements in row 3 is: 40
The sum of the elements in row 4 is: 18
Press any key to continue . . .
row[0]column[0]=1;
row[0]column[1]=2;
row[1]column[0] =3;
when i debug it.....it display:
if i using vector it display-:
1 2
3 0
if i using pointer it display-:
1 2
3 -9.25596e+061
why it happen like that because no data is store at row[1]column[1]?
If I understand correctly, array_name[1][1] is never initialized or assigned to. In the case of a std::vector, all elements are initialized with a default value.
In the case of an array, that isn't true unless they're initialized with {} syntax. It's not that there is no data at the position you haven't initialized/assigned to, it's that what is there is random junk.
if i check the size of second row........its answer will become 1 or 2?
Yes, cire is right. if you declare an array with a fixed amount of rows and columns, and then print out the spaces, you will get garbage output unless you initialize each space. If you declare the array globally, it will initialize all the elements to zero automatically.
#include <iostream>
#include <cstdlib>
#include <vector>
usingnamespace std;
int A[2][2]; //matrix A has been declared globally, and so it's elements have all been initialized to zero.
int main()
{
int i,j;
int B[2][2]; //matrix A and B have been declared locally, so there elements
int C[2][2]; //have not been initialized and contain garbage data right now.
vector<int>row(2); //This creates an integer vector with two elements that are
//initialized to zero.
vector<vector<int> >D(2,row); //This creates a vector with two elements
//that are themselves vectors with two elements.
//Initialize matrix B's elements to zero:
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
B[i][j]=0;
}
//Print out each matrix.
cout<<"Matrix A is: "<<endl<<endl;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
cout<<A[i][j]<<" ";
cout<<endl;
}
cout<<endl<<"Matrix B is: "<<endl<<endl;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
cout<<B[i][j]<<" ";
cout<<endl;
}
cout<<endl<<"Matrix C is: "<<endl<<endl;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
cout<<C[i][j]<<" ";
cout<<endl;
}
cout<<endl<<"Matrix D is: "<<endl<<endl;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
cout<<D[i][j]<<" ";
cout<<endl;
}
cout<<endl;
system("pause");
return 0;
}
The output is:
Matrix A is:
0 0
0 0
Matrix B is:
0 0
0 0
Matrix C is:
0 4274496
2293456 2293512
Matrix D is:
0 0
0 0
Press any key to continue . . .
Every matrix has had its elements automatically or explicitly initialized to zero, except for matrix C, which was declared locally, but not initialized. Therefore, the console window prints out garbage data for matrix C. The first element of the first row is zero purely by coincidence.