Badshah
Badshah

Reputation: 431

2D variable sized array c++

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?

Upvotes: 1

Views: 166

Answers (4)

Kumar Vijyant
Kumar Vijyant

Reputation: 1

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;
}

Upvotes: 0

Monster
Monster

Reputation: 7

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;
}

Upvotes: 0

Daniel Fischer
Daniel Fischer

Reputation: 183978

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.

Upvotes: 2

Qaz
Qaz

Reputation: 61970

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.

Upvotes: 3

Related Questions