Vector Data Project


Course Overview
Arrays and Vectors
Built-in Arrays
PREVIEW18m 31s

The course is part of this learning path

Start course
1h 25m

This course explores arrays and vectors which are types of data structures in C++. We'll focus on built-in arrays, which C++ inherits from the C programming language, and a couple of sequence containers including the array template class and the vector template class, which are part of the standard template library or STL.

Intended Audience

  • Beginner coders, new to C++
  • Developers looking to upskill by adding C++ to their CV
  • College students and anyone studying C++


To get the most out of this course, you should have a basic understanding of the fundamentals of C++.



For this project, you will create a Visual Studio project named VectorData. And you will do essentially what we did in the last project, read in integers and print out twice the values that were entered. The difference is that we are going to take advantage of the fact that vectors are not fixed size, we can hold an arbitrary number of integers. So, for this project, you will only accept non-negative integers to store in the vector that will have twice their value printed. If the user enters a negative integer, the loop will stop. As a hint, consider using a wire loop this time for reading the input and doing a priming read which we discussed earlier in the course, meaning you ask the user for input once before even entering the loop. In the last project, you practiced with counter controlled repetition. Well in this project, your code must wait for a negative number to exit the loop. Otherwise, it can read in an arbitrary number of vintages. Therefore, you're going to be working with sentinel-controlled repetition, which we discussed earlier in the course as well. So, here's a quick demonstration of the VectorData project. We will go to debug, start without debugging, and it says enter a positive integer to add to the vector or a negative integer to quit. So, let's put 100, 44, 20, 7, 70, 77, 65, 14. And you'll notice that it's taking more values than the other version did with the array class. So, I can keep going if I wanted to. But when I want to stop, I just type in a negative number like -1, hit 'Enter' and then you'll notice it will go through and it will double all of the numbers that I entered, starting with the 100 which becomes 200, 44 is 88 etc. all the way to the 22 which becomes a 44. So, hopefully that demonstration helps you solve this problem. So, pause the video and see if you can accomplish this project VectorData. How did that work out for you? Were you able to complete the project? That's really awesome if you were able to finish it or even if you got close, if not don't worry just keep practicing review material if you need to and you will get better and better all the time. But let's do the project together and see what we can come up with. So, VectorData is the name of our project. C++, empty C++ project Vector Data. Give it a minute. We will create the source file which is main.cpp. And then we will start typing some of the code. So, I know I need iostream and I know I need vector using namespace standard and then we'll fill in the skeleton code. What we need now is we need a vector of integers. We'll call it myIntVector and a temporary variable will hold input in as the user enters it. Now let's do a priming read right here. So, we're going to say enter a positive integer to add to the vector or negative, we'll say or negative integer to quit. Excellent. Now, we take in input, so this is the priming read. This is before we even enter the loop. Now you could do other things like do stuff with a while loop or do while loop that doesn't use a priming read. But I like priming reads. I think they're useful. A lot of people don't because they see it as replicating code. All right, so we've got myIntVector.push_back the value input, and this is as long as the input that was entered is greater than or equal to zero meaning non negative. So, we're going to print out, again actually I can just copy and paste this. Now, normally copy and pasting code, again this is one of the reasons some people don't like priming reads if they can avoid them because it really is a replication of code, but it happens once and this is to get the loop started. So, let's see what we can do here if we get all of the data going in here and then when the user finally enters a value at the end of this loop, that causes the loop to break, what should we do with it? So, now we have to print out double the amount. So, now here are double the amounts. So, these are twice the amounts. We can use a range-based for loop, right here. Just print out num * 2, and there we go. Let's run this and see what happens. So, enter a positive integer, let's say 100. Now we'll do 50, 25, 22, 76, 43, 29. You'll notice this is way more than the five that we did in the last project. As soon as I hit a negative number, like -1, it will break out of the loop and echo to me the input that I gave multiplied by two, and you can follow this along and make sure that they're correct, but they are correct. So, 50 becomes 100. The original 100 becomes 200, 25 becomes 50, 22 44, 76, 152 etc. Excellent. So, awesome work everyone. In the next lecture, we're going to do something a little more creative. We're going to use something called parallel arrays, or parallel vectors, would be more accurate. So, we'll use more than one vector but the element at each index in these vectors will represent the same entity. So, there's very exciting content coming up next. I'll see you there.


About the Author
Learning Paths

John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.

Covered Topics