1

I wanted to get familiar with 2D variable sized arrays in c++, so I wrote a little program, but it doesn't work. Here is the code:

#include <iostream>

using namespace std;

int main(){
int a,i;
cin>>a; //the width of the array is variable
int **p2darray;
p2darray = new int*[2]; //the height is 2
for (i = 0; i < 2; i++){
    p2darray[i] = new int[a];
}
i=0;
while(i!=a){
    p2darray[0][i]=i; //filling some numbers in the array
    p2darray[1][i]=2*i;
    i++;
}
i=0;
while(i!=a){
    cout<<p2darray[0][i]<<endl;
    cout<<p2darray[1][i]<<endl;
    i++;
}
return 0;
}

So why doesn't it work?

5
  • 2
    What doesn't work? What does it do? Commented Jul 29, 2012 at 15:50
  • @chris :it should print the values of the 2d array, but it just stops working Commented Jul 29, 2012 at 15:52
  • 1
    As an aside, unless you have very specific performance requirements that a std::vector can't fulfill I would recommend against using raw arrays. Commented Jul 29, 2012 at 15:54
  • Yes, this itself might be a learning exercise, but you should always prefer the given containers over raw arrays and pointers. Commented Jul 29, 2012 at 15:56
  • By the way you shouldn't use using namespace std; like that. Commented Jul 29, 2012 at 15:58

4 Answers 4

3

The main problem is that when you say p2darray[i][0], your indices are backwards because you set the second dimension to the size the user enters, but you're incrementing the first dimension to that number instead. This would normally cause a segfault. It should be p2darray[0][i] in all four cases. You also didn't set i back to 0 before entering the printing loop, so it's skipping the entire printing process.

For a running program that illustrates the said corrections, see here.

Sign up to request clarification or add additional context in comments.

Comments

2

You forgot to reset i.

i=0;
while(i!=a){
    p2darray[i][0]=i; //filling some numbers in the array
    p2darray[i][1]=2*i;
    i++;
}
// Now i == a, so the next loop doesn't run
while(i!=a){
    cout<<p2darray[i][0]<<endl;
    cout<<p2darray[i][1]<<endl;
    i++;
}

Insert i = 0; between the two loops.

Also, you have the indices in the wrong order, the first index can only take the values 0 and 1, otherwise you access memory outside the allocated area.

i=0;
while(i!=a){
    p2darray[0][i]=i; //filling some numbers in the array
    p2darray[1][i]=2*i;
    i++;
}
i = 0;
while(i!=a){
    cout<<p2darray[0][i]<<endl;
    cout<<p2darray[1][i]<<endl;
    i++;
}

is correct.

Comments

0

Why don't you place the 2d array in to a function.

#include <iostream>

using namespace std;

int** CreateArray(int valuea, int valueb)
{
     int** array;
     array = new int *[valuea];
     for(int i =0;i< row;i++)
     {
         array[i] = new int [valueb];
     }
     return array;
}

Then you must not forget to return the memory.

int main(){
int a;
int i = 0;

cin>>a;
int** array = CreateArray(a,i);

while(i!=a){
  array[i][0]=i;
  array[i][1]=2*i;
  i++;
}
i=0;
    while(i!=a){
      cout<<array[i][0]<<endl;
      cout<<array[i][1]<<endl;
      i++;
 }
return 0;
}

Comments

0

A 2D variable size Array in C++

 #include <bits/stdc++.h>
 using namespace std;

 int main(){

int row,col,i,j;
cin>>row;                    //no. of rows
string col_size;             //mapping index to no.of columns     

vector<vector<int> >Arr(row);

for(i=0; i<row ; i++){
  cin>>col;
  col_size.push_back(col);    // no. of columns at ith row 
  Arr[i]=vector<int>(col);
       for(j=0 ; j < col_size[i] ; j++)
             cin>>Arr[i][j];
}

                      //printing the 2D Array

for(i=0; i<row ; i++){
       for(j=0 ; j<col_size[i] ;j++){
              cout<<Arr[i][j]<<" ";
   }
    cout<<"\n";
   }
  return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.