FAQ: Vectors - Index

This community-built FAQ covers the “Index” exercise from the lesson “Vectors”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C++

FAQs on the exercise Index

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

#include <iostream>

#include <vector>

int main() {

   

  std::vector<double> subway_adult = {800, 1200, 1500};

  

  std::vector<double> subway_child = {400, 600, 750};

  

  // What number at index 2 of subway_child?

  

std::vector<double> some_vector = {0.25, 0.50, 0.75, 1.00};

  

}

Why is it not working?? need help

Hello, @design7796009471, and welcome to the forums.

What isn’t working? What are you expecting that doesn’t happen? Also, remember to format your code when you post it. I edited your post to format the code so it will display properly.

I dont know how to code the thing’

I just looked at the lesson. It seems to explain accessing vector elements by their index fairly well. What in particular do you not know how to do? Do you know how to use std::cout? If so, you want to use that to output the element of the subway_child vector that is at index 2. To access an element of a vector at a particular index, you put the index value inside of square brackets following the name of the vector. For example if I have a vector named names, and it has 6 elements: {"Greg", "Marcia", "Peter", "Jan", "Bobby", "Cindy"} I could print the first element using this syntax: std::cout << names[0] or if I wanted to print the element at index 4, I could use: std::cout << names[4].

#include <iostream> #include <vector> int main() { std::vector<std::string> names = {"Greg", "Marcia", "Peter", "Jan", "Bobby", "Cindy"}; //print first element std::cout << names[0] << std::endl; //print element at index 4 std::cout << names[4]; return 0; }

I dont get it? for some reason!

oh now i do ohdjhdffj

1 Like

The exercise doesn’t really indicate that one has to add a skip line at the end of the std::cout statement. Spent 15 minutes trying to figure out what was wrong with the code even though the statement was correct :smiley:

Don’t forget to check solution if you’re stuck at the same

I’m not sure if the hint ’ Remember, you can initialize a double vector like: …’ is that relevant