Created
February 24, 2018 02:57
-
-
Save hckim16/b37987d5b7ff66501cb9dd508ae1bf37 to your computer and use it in GitHub Desktop.
Hackerrank C++ Variable Sized Arrays challenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.