Skip to content

Instantly share code, notes, and snippets.

@hckim16
Created February 24, 2018 02:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hckim16/b37987d5b7ff66501cb9dd508ae1bf37 to your computer and use it in GitHub Desktop.
Save hckim16/b37987d5b7ff66501cb9dd508ae1bf37 to your computer and use it in GitHub Desktop.
Hackerrank C++ Variable Sized Arrays challenge
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n;
int q;
cin >> n >> q;
vector<int> a[n];
for(int i = 0; i < n; i++){
int m;
cin >> m;
int o;
for(int j = 0; j < m; j++){
cin >> o;
a[i].push_back(o);
}
}
int r, s;
for(int k = 1; k <= q; k++){
cin >> r >> s;
cout << a[r][s] << endl;
}
return 0;
}
@iamsrj99
Copy link

#include
#include
#include
#include
#include
using namespace std;

int main() {int n,q;
cin>>n>>q;
vector< vector > v;
for(int i=0;i<n;i++){
int r;cin>>r;
for(int j=0;j<r;j++){
cin>>v[i][j];
}
}
for(int t=0;t<q;t++){int i1,j1;cin>>i1>>j1;
cout<<v[i1][j1];}

/* Enter your code here. Read input from STDIN. Print output to STDOUT */   
return 0;}

@iamsrj99
Copy link

this code shows segmentation fault error,whats wrong with it???

@xtcPanda
Copy link

#include 
#include 
#include 
#include 
#include 
using namespace std;

int main() {int n,q;
cin>>n>>q;
vector< vector > v;
for(int i=0;i<n;i++){
int r;cin>>r;
for(int j=0;j<r;j++){
cin>>v[i][j];
}
}
for(int t=0;t<q;t++){int i1,j1;cin>>i1>>j1;
cout<<v[i1][j1];}

this isn't a code for a programmer at all !
I can't know what is your mistakes if you just throw the code letter by letter in my face you need to work on your styling and your formatting
give me the correct code you tried because this snippet of code has a lot of things that ain't clear at all.

@xtcPanda
Copy link

but maybe this (#include)'s and you need to put a type instead of < vector > you typed here in the 9th line vector< vector > v; maybe you should make this line of code int i1,j1; on its own before the for loop on the 16th line for(int t=0;t<q;t++) and you are missing alot of (endl)'s focus on your formatting and the looks of your output and of your code so ppl can read it at least

@subhoshreep
Copy link

#include
#include
#include
#include
#include
using namespace std;

int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n,q,i,j,l,k,a[k],b,c;
cin>> n>>q;
for(j=0;j<n;j++){
cin>>k;
for(i=0;i<k;i++){
cin>>a[i];}
}
for(l=0;l<q;l++){
cin>>b>>c;
cout<<a[b][c]<<endl;
}

return 0;

}
can anyone say whats wrong?

@mewebbie
Copy link

can you please tell how is it taking form of a 2D vector/array while the declaration is still the same as that for 1D array??

@Muhsin-42
Copy link

Muhsin-42 commented Feb 3, 2021

Try this one

#include
#include
#include
#include
#include
using namespace std;

int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
// accept enlemnt to -no. of rows
int n;
cin>>n;

//accept another variable - no. of queries
int q;
cin>>q;

int arr[n+1][1000];
for(int i=0; i<n ;i++){
    cin>>arr[i][0];
    for(int j=1 ; j<=arr[i][0];j++){
        cin>>arr[i][j];
    }
}
int r,c;
for(int i=0; i<q;i++){
    cin>>r>>c;
    cout<<arr[r][c+1]<<endl;
}
return 0;

}

I am getting the answer for some cases but not all

its showing "Segmentation Fault!"

@nikhilkotiya
Copy link

#include
#include
#include
#include
#include
using namespace std;

int main() {
int k;
int n;
int q;
cin>>n;
cin>>q;
int g = q;
int i;
int j;
int c;
int a[i][c];
for(i=0;i<q;i++){
cin>>k;
for(c=0;c<k;c++){
cin>>a[i][c];
}
}
for(int f=0;f<=g;f++){
cin>>j;
cout<<a[f][j]<<"\n";
}
return 0;
}
/*can anyone say whats wrong? */

@budagabrielchristian
Copy link

`
#include
#include

using namespace std;

int main() {
int numOfArrays, numOfQueries;

cin >> numOfArrays >> numOfQueries;

int arrayIndexes[100000];
int elementIndexes[100000];
int board[numOfArrays][300000];

for(int i = 0; i < numOfArrays; i++){
    int limit;
    cin >> limit;
    for(int j = 0; j < limit; j++){
        cin >> board[i][j];
    }
}

for(int l = 0; l < numOfQueries; l++){
    int arrayIn, elementIn;
    cin >> arrayIn >> elementIn;
    arrayIndexes[l] = arrayIn;
    elementIndexes[l] = elementIn;
}

for(int j = 0; j < numOfQueries; j++){
    int currentArr = arrayIndexes[j], currentElement = elementIndexes[j];
    cout << board[currentArr][currentElement] << endl;
}
return 0;

}
`
I get segmentation fault myself, does anyone know what exactly is wrong with my code?

@budagabrielchristian
Copy link

`
#include
#include

using namespace std;

int main() {
int numOfArrays, numOfQueries;

cin >> numOfArrays >> numOfQueries;

int arrayIndexes[100000];
int elementIndexes[100000];
int board[numOfArrays][300000];

for(int i = 0; i < numOfArrays; i++){
    int limit;
    cin >> limit;
    for(int j = 0; j < limit; j++){
        cin >> board[i][j];
    }
}

for(int l = 0; l < numOfQueries; l++){
    int arrayIn, elementIn;
    cin >> arrayIn >> elementIn;
    arrayIndexes[l] = arrayIn;
    elementIndexes[l] = elementIn;
}

for(int j = 0; j < numOfQueries; j++){
    int currentArr = arrayIndexes[j], currentElement = elementIndexes[j];
    cout << board[currentArr][currentElement] << endl;
}
return 0;

}
`
I get segmentation fault myself, does anyone know what exactly is wrong with my code?

@dabir-supremacy
Copy link

dabir-supremacy commented Dec 13, 2022

You are getting a segmentation fault.
Segmentation fault occurs when you are trying to access memory which you dont own, consequently to prevent memory from getting corrupted and unusable C++ has a built-in mechanism to prevent this, which raises the SegFault/Segmentation Fault/Core dump.

to prevent this from occurring try breaking you problem into parts and converting your code in different 'functions'.

this may seem primitive but if anyone has an expert solution, im all ears.

EDIT: A vector can also resolve this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment