Home > Enterprise >  How to implement a variable multidimensional array using int** array = new int*[n];?
How to implement a variable multidimensional array using int** array = new int*[n];?

Time:10-31

I tried this approach but I am getting error when I cout <<vararr[i][j]<<endl; The problem I am trying to solve- hackerrank

my code-

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int* variablesizedarr(int size){
    int* arr= new int [size];
    for(int i=1;i<size;i  ){
        cin >>arr[i];
    }
    /*I believe the problem is when I return the arr array*/
    return arr;
    
}

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int n,q,size;
    cin >>n >>q;
    int** vararr= new int* [n];
    for(int i=0;i<n;i  ){
        cin >>size;
        vararr[i]=variablesizedarr(size);
    }
    /*now printing the values to the console*/
    
    for(int k=0;k<q;k  ){
        int i,j;
        cin >>i>>j;
        cout <<vararr[i][j]<<endl; /* difinetly an error in this line because that's what the compiler say*/
        
    } 
    // deallocate the array
    for(int i=0;i<n;i  ){
        delete [] vararr[i];
        
    }
    delete [] vararr;
    
    return 0;
}

edit1: error I am getting-

Compiler Message
Segmentation Fault
Error (stderr)
Reading symbols from Solution...done.
[New LWP 2655054]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `./Solution'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00000000004011f2 in main () at Solution.cpp:32
32          cout <<vararr[i][j]<<endl;
To enable execution of this file add
    add-auto-load-safe-path /usr/local/lib64/libstdc  .so.6.0.25-gdb.py
line to your configuration file "//.gdbinit".
To completely disable this security protection add
    set auto-load safe-path /
line to your configuration file "//.gdbinit".
For more information about this security protection see the
"Auto-loading safe path" section in the GDB manual.  E.g., run from the shell:
    info "(gdb)Auto-loading safe path"

I know I could do this the way other people have done it, but I was not going to learn like that. Any help is appreciated.

CodePudding user response:

int** p = new int*[n];
for (int i = 0; i < n; i  ) {
    p[i] = new int[m];
}

CodePudding user response:

Thanks to @nathanpearson I was able to find the error in

int* variablesizedarr(int size){
    int* arr= new int [size];
    for(int i=1;i<size;i  ){
        cin >>arr[i];
    }
    /*I believe the problem is when I return the arr array*/
    return arr;
    
}

Here the initialization of i should be 0, not 1. I know it's a stupid mistake, but Hackerrank debugger is very bad in explaining the error so I was doubting my understanding. Anyway, this is another way to make a variable multidimensional array without using a vector.

Again, Thanks to Nathan for taking the time to understand my question.I really appreciate it.

  • Related