FAQ: Vectors - Introduction to Vectors

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

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

Learn C++

FAQs on the exercise Introduction to Vectors

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!

help! Vector simply explained

I’m missing something with vectors. I’ve basically just memorized by rote the code to use vectors in situations like the UFO exercise or the Palindrome excercise, but the fact that I know the WHAT and don’t understand THE WHY fully is getting in the way of comprehending these lessons.

Can some one break down in the simplest of terms what is happening here.

std::vector<char> incorrect;

for (int i = 0; i < incorrect.size(); i++)

I know what this will do, but I don’t understand how the ■■■■ it identifies whether a string of characters are the same or not.

what is the integer i exactly? is it like each character is assigned an integer value like a = 0, b =1 etc?

what is .size exactly, is it a function within a vector? if so what does it do.

I’m coming at this as an absolute beginner, so please use the simplest jargon possible, I’m sure whatever I’m missing, once I get more of this defined the logic will fall into place.

yes I’ve googled these things, a link to a visual description, animation, illustration, video is what I need cause the articles aren’t sticking.

Deepest thanks from a humble but determined Noob

Hello, @course2520194271, and welcome to the forums.

The code you supplied declares a vector of type char. The next line would be the first line of a for loop used to iterate through the elements of the vector. The variable i is used as the index, so we can tell the computer which element of the vector to look at. For example, if I had the following vector:

std::vector<char> name;
  name.push_back('h');
  name.push_back('e');
  name.push_back('r');
  name.push_back('c');
  name.push_back('u');
  name.push_back('l');
  name.push_back('e');
  name.push_back('s');
//I've added each letter (ie. char) as an element in the vector called name.
//The elements are separated by commas.
// The vector now could be visualized like this:
// { 'h', 'e', 'r', 'c', 'u', 'l', 'e', 's' }
//'h' is at index 0, 'e' is at index 1, 'r' is at index 2, and so on:
//{ 'h', 'e', 'r', 'c', 'u', 'l', 'e', 's' }
//   0    1    2    3    4    5    6    7 <--Index values
//To access the letter 'r' I would use: name[2];

So, you are starting with i = 0; because the first element of the vector is at index 0. The next part of the for loop is i < incorrect.size();. .size() is a method that returns the size of the vector. A vector’s size is the number of elements it contains. In my example above, the vector, name has a size of 8. There are 8 elements that we can access using the index values (0-7). That is why we use
i < incorrect.size(). The loop will continue iterating until this condition is no longer true, so when i gets to 8, 8 is not less than 7, so the loop is finished. Therefore, i will start at 0, and be incremented by 1 following each iteration of the loop until it gets to 8. That will give us the index values we need to access every element in the vector: (0, 1, 2, 3, 4, 5, 6, 7). We could use that to print the name in the console from my previous example:

#include <iostream>;
#include <vector>;

int main() {
  std::vector<char> name;
  name.push_back('h');
  name.push_back('e');
  name.push_back('r');
  name.push_back('c');
  name.push_back('u');
  name.push_back('l');
  name.push_back('e');
  name.push_back('s');

  for(int i = 0; i < name.size(); i++) {
    std::cout << name[i]; 
  }

  return 0;
}

Output:

hercules

First name[0] was printed, so ‘h’.
Then name[1], so ‘e’, and so on until i was incremented to 8 at which point the for loop was exited.

As far as how the code you supplied:

It doesn’t. Not by itself. It could be used to iterate over the elements in the vector called incorrect, then additional code could be written to make the necessary comparisons, and determine a result. I did a search, and found this video. I didn’t watch the whole thing, but perhaps it will help.

Note: We could also simply assign elements to the name vector at the time is was declared instead of repeatedly using push_back():

std::vector<char> name = { 'h', 'e', 'r', 'c', 'u', 'l', 'e', 's' };

ok, I think it’s starting to come together, thanks SO MUCH
so long as i is less than vector_name.size() the loop will repeat, meaning the amount of characters in the vector. and the i++ is so the index value goes up by one each iteration of the loop.

that’s making more sense.

std::string codeword = "codecademy";
std::string answer = "__________";
int misses = 0;
std::vector<char> incorrect;
bool guess = false;
char letter;

  for (int i = 0; i < codeword.length(); i++) {

    if(letter == codeword[i]) {

      answer[i] = letter;
      guess = true;

So if I put this in plain english, this loop is repeating and checking if “letter” (the user input) is equal to the codeword[0], the first letter, then codeword[1] the second letter, etc. and if any are equal, the bool returns true. if the loop checks this in the whole character vector and there’s no match the bool returns false.

Then an if-else statement after will run one of two sets of code based on the result of that bool.

I think i’m getting it

Part of the code is missing, but you’re pretty much on track. Individual chars in a string can be accessed by their index as well (because it is actually an array of characters), so this for loop is iterating over each letter in codecademy which is the value codeword is assigned to. If the user’s guess, letter, matches one of the chars, then using the same index, the _ in the string that answer is assigned to is replaced with the char. I would guess that the missing code includes a line that assigns incorrect guesses to the incorrect vector using something like:

if(guess == false) { //or possibly: if(!guess)
  incorrect.push_back(letter);
}

Hi can you guys help with a code and i am still new to the world of coding especially c++ which is the language i am learning
#include

// A game of pleading guilty in court

int main () {

std::cout << “In hospital emergency room a patient is sick.\n”;

std::cout << “The nurses starts by checking the boold pressure of patient”;

int choice;

std::cout << “What is the blood pressure of patient:.\n”;

std::cin >> choice;

// the normal blood pressure of a human in 120 / 80

if (choice == 120 / 80) {

std::cout << " The patient blood is normal and patient is fine\n";

} else if (choice > 120 / 80) {

std::cout << "The patient blood pressure is hight need medical attention\n";

} else if (choice < 120 / 80) {

 std::cout << "The patient is and need medical attention.\n";

} else {

std::cout << “INVALID READING ARMBAND NOT CORRECTLY POSITIONED.\n”;

}

return 0;

}
so please help with the last code of else statement and it is not executed please help

It is not executed because it wil enter either one of the ifs