0

is my code ok?

Hello everyone, One of the questions in the exercises from the first lesson of the C++ arrays chapter in Sololearn was the following: **PRACTICE EXERCISE** **Alphabet** The given code declares an array that holds the letters of the English alphabet. **Task** Take a number as input and output the letter from the array that corresponds to that index. To solve this problem, I wrote the following code: ```cpp #include <iostream> using namespace std; int main() { char letters[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; int num; cin >> num; cout << letters[num]; } ``` My code was accepted and it worked correctly! It was surprising to me that my code was accepted and worked as expected! I would appreciate it if you could let me know whether I solved the problem exactly as intended by the question, or if it needed to be solved in a different way. Thank you all!

11th Dec 2024, 11:39 AM
mohammadhoseinmohebian
mohammadhoseinmohebian - avatar
3 Antworten
+ 3
As a beginner level mohammadhoseinmohebian's solution is perfect no change need to be done here. Just keep going and continue learning. Happy coding!
11th Dec 2024, 2:52 PM
Alhaaz
Alhaaz - avatar
0
would work in constraint 0<= num < 26 think what if i entered 35 ... a more c++ way of doing it would be using std::array and you can either bound check using .at() or assert 🥲 ( just cuz you asked )
11th Dec 2024, 1:55 PM
Prashanth Kumar
Prashanth Kumar - avatar
0
mohammadhoseinmohebian , this kind of question is not meant to be posted in the `q&a` section. `q&a` is for coding related issues. post and present your code in the ` community section` feed instead. see also the Content Creation and Community Guidelines: https://www.sololearn.com/Content-Creation-Guidelines/
11th Dec 2024, 8:50 PM
Lothar
Lothar - avatar