-
-
Save hckim16/b37987d5b7ff66501cb9dd508ae1bf37 to your computer and use it in GitHub Desktop.
#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; | |
} |
#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?
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??
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!"
#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? */
`
#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?
`
#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?
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.
but maybe this (
#include
)'s and you need to put a type instead of< vector >
you typed here in the 9th linevector< vector > v;
maybe you should make this line of codeint i1,j1;
on its own before the for loop on the 16th linefor(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