#include <iostream> #include <stdexcept> #include <vector> using namespace std; // your code here: define getValue(const vector<int>& arr, int idx) int getValue(const vector<int>& arr, int idx) { int len = arr.size()-1; if(idx<0 || idx>len) { throw out_of_range("Index out of range"); }else{ return arr[idx]; } } int main() { int n; cin >> n; vector<int> arr(n); for (int i = 0; i < n; i++) cin >> arr[i]; int idx; cin >> idx; try { int value = getValue(arr, idx); cout << "Value: " << value << "\n"; } catch (const out_of_range& e) { cout << "Error: " << e.what() << "\n"; } return 0; }
Test Cases
Test Results
Input
5 10 20 30 40 50 2
Expected Output
Value: 30