Skip to content

Instantly share code, notes, and snippets.

@omi-akif
Created November 20, 2020 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omi-akif/7621b21d1bbbdb4c73aa02ad1ac62a79 to your computer and use it in GitHub Desktop.
Save omi-akif/7621b21d1bbbdb4c73aa02ad1ac62a79 to your computer and use it in GitHub Desktop.
A cpp program to create variable arrays and query them.
#include <iostream>
/*
1. Compile the program with using g++ compiler -> variable_array.cpp -o variable
2. Run the compiled file in Terminal(Ubuntu) or Command Promt(Windows)
3. First Line/Step -> Enter the number of variable length arrays and the number of queries.
Second Line/Step -> Enter the number of data entries within the variable sized arrays and then enter the numberes.
Third Line/Step -> Enter the index of variable array and the index of the array within the array.
Example:
Input:
2 2
2 1 3
3 1 4 5
0 1
1 0
Output:
3
1
*/
using namespace std;
int main(){
int n, q; // Entries for number of variable length of arrays and the number of queries respectively
int k;
int i, j;
cin >> n; //enter number of variable-length arrays
cin >> q; //enter the number of queries
//Created my first two dimensional array
int **a = new int *[n]; //point to an array of arrays
for(int x = 0; x < n; x++){ // Initialize 'x' for x is the numer of entries for a[]
cin >> k; // Enter the number of entries
a[x] = new int [k];
for(int l = 0; l < k; l++){ //Iniatialize 'l' for l is the number of a[x]
cin >> a[x][l];
}
}
int *temp = new int [q]; //temporary array
for(int x = 0; x < q; x++){ // Inserting number of queries
cin >> i;
cin >> j;
temp[x] = a[i][j];
}
for(int x = 0; x < q; x++){
cout << temp[x] << endl; //Outputting number of queries.
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment