how to add row and column using pointer or vector

firstly I already declare row and column with fixed value.such as:
column=2;
row=2;

but my data is exceed the limit set.how to add column and row in the process.anyone can help.....:)
Last edited on
http://cplusplus.com/doc/tutorial/dynamic/

Are you trying to make a 2D matrix with dimensions that depend on what the user needs? If so, you'd need to do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <cstdlib>

using namespace 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=new int*[row];  //Creates an array of pointers.
    for(i=0;i<row;i++)
        matrix[i]=new int[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 . . .
Last edited on
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.:)
Last edited on
i alreay make an experiment using vector and pointer......when i declare row=2 and column=2.......i store the data such as:

row[0]column[0]=1
row[0]column[1]=2
row[1]column[0] =3

i ignore the row[1]column[1] because i only have 3 data. 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]?

im sorry.....im am beginner in this field....thanks for ur help.:)
Last edited on
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace 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.
Last edited on
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.....:)
Last edited on
"000 00 0000" with " " (blank) as a delimiter... don't even need an array.
i dont understand........can u explain little more.........im am beginner in this field......thanks.......:)
Last edited on
So is the data stored in a text file or something? And you're trying to store it in an array?
Let's say you have the data saved in a text file called 'data', and that the contents of the file are arranged like so:

1 2 3
4 5
6 7 8 9 10

The following code puts this data into an equivalent matrix and then prints it out onto the console:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <cstdlib>
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>

using namespace std;

int main()
{
    int i,j,element;
    string line;
    stringstream ss;
    ifstream infile;
    infile.open("data.txt");
    vector<vector<int> >matrix;
    while(!infile.eof())
    {
        getline(infile,line);
        ss<<line;
        vector<int>row;
        while(!ss.eof())
        {
            ss>>element;
            row.push_back(element);
        }
        ss.clear();
        matrix.push_back(row);
    }
    infile.close();
    for(i=0;i<matrix.size();i++)
    {
        for(j=0;j<matrix[i].size();j++)
            cout<<matrix[i][j]<<" ";
        cout<<endl;
    }
    cout<<endl;
    system("pause");
    return 0;
}


The output is:

1 2 3
4 5
6 7 8 9 10

Press any key to continue . . .


I didn't include comments in the code but if you have any specific questions, just asks.
Last edited on
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.....:)
Last edited on
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.
Last edited on
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:

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]?

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:

http://www.cplusplus.com/reference/stl/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:

1 2 3
4 5
6 7 8 9 10
2 8 5 3

And we also have the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <cstdlib>
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>

using namespace 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 . . .
Last edited on
thanks for ur reply........I think our discussion is near to solve my problem.....i just to know.......

when i declare row=2 and column=2.......i only have 3 data and store the data such as:

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


if i check the size of second row........its answer will become 1 or 2?

thank you my friend.........I appreciate all your guidance........:)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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?


If you could do such a thing, it would be 2.
Last edited on
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <cstdlib>
#include <vector>

using namespace 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.
Last edited on
thanks for ur reply........when i declare row=2 and column=2.......i only have 3 data and store the data such as:

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


there is way how I want to know the data I store is only one at second row?
If you want to know, keep track.

One way would be to set the values to some default that you know isn't valid and check the value at arrray[i][j] to see if it is that number.

thanks for ur reply...........your method is easy and effective...........thanks again for cire,CJC0117 and hentaiw..........:)
Topic archived. No new replies allowed.