Parallel Vectors Project

Contents

keyboard_tab
Introduction
1
Course Overview
PREVIEW1m 25s
Arrays and Vectors
2
Built-in Arrays
PREVIEW18m 31s
Conclusion

The course is part of this learning path

Start course
Overview
Difficulty
Intermediate
Duration
1h 25m
Students
18
Ratings
5/5
starstarstarstarstar
Description

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++

Prerequisites

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

 

Transcript

In this lecture, we will explore an important design technique in which we use more than one vector to represent entities. You will work on a project called Weight Tracking. I want you to create two vectors, one for full names and one for weights. The names should obviously be strings and the weights could be integers or doubles. You can choose. You should prompt the user five times for five different people and their corresponding weights. Store the person's name in one vector and at the same location in the corresponding vector, store that person's weight. If you're consistent, you shouldn't have any problems. After you've done this, print out each of the people's names followed by what they weigh in a sentence such as Sally weighs 120 Ibs or Sam weighs 145 Ibs . And don't forget to consume the new line character after obtaining the weight of the person. As a hint, try cin.get. So, let's do a demonstration of the weight tracking program. So, we'll start without debugging and it asks us to enter a full person's name. So, John and then we'll say 200 and then another person's name, Sally Smith. We'll say that Sally Smith is 120. We will say that Ali Oxenfree is 250. We will say that Silas Gray is 300 and we'll say that Jackson Alanson is 130 and it will say that each of these people have a certain weight. The first one, I didn't enter a full name with last name included but it still works. But you should notice that it does work with last names as well and it will say that person's name then weighs the number of pounds or kilograms or whatever you choose and then that particular amount and then Sally Smith weighs 120 Ibs, Ali Oxenfree weighs 250 Ibs. Silas Gray weighs 300 and Jackson Allinson weighs 130 Ibs. So, I hope that helps you with your attempt at this project. So, pause the video and create your weight tracking project and try to solve this problem. Come back when you're done or if you need some help. So, how did you do? I hope you were able to complete this project. If so, great job. Even if you couldn't get it all done, we'll do it together so you can see how you might have gone about solving it. So, let's create a project called WeightTracking. So, create a new project. Make sure it's an empty project with C++. And we will call this WeightTracking. Create. So as usual, we will create a source file here and let's put that main.cpp. include < iostream >. We also need the string class because we'll be using it for names and of course, we'll need vectors. So, using namespace std;, int main() and there is our skeleton program. So, the number of people were going to be asking about, we can make it a constant because we know immediately how many people. Even though it's a vector, we could use an arbitrary number of people. For this one, we're just asking for five vector < string > names; and vector<int> < int > weights;. Now, you could make both of these vectors size five. You could give them initial sizes and that would be okay and then you could set their values using the index operator. That's totally fine. But I'm going to use a slightly different technique that we learned before. And just to make things a little easy on us, we have a temporary name and also temporary weight. And now, I'm going to use a traditional for loop. So, we're going to say for int i = 0. i < NUM _PEOPLE; we are enquiring about, i++) and then that's the end of the for loop. I will say << "Please enter a person's full name" << endl; and then we can say this one will only get line because it's a string. So, we'll say take it from (cin, temp name);. Then, I'm going to say, "Please enter" and to make a little bit nice, I'm going to say << tempName  << and then " 's weight."  << endl;. Now, if you didn't do that, that's totally fine but this is just more of a formatting thing. So, it will say "Please enter Bob's weight" or "Please enter John's weight" or Sally's weight. So, it makes it look of nice and you can see how the variable will be interspersed since we just grabbed their full name, John, Baugh or John or whatever we want to put. And then for the integer, we'll grab the template and remember what we need to do here though. We need to consume the new line character because with the integer it will still be in the stream and then the next time through the loop, get line will try to grab it so we definitely need to consume the new line character, // consume newline character. We learned about that when we learned about user input. Now, here's the problem. If I just leave it as is, it's just going to go through the loop over and over again and keep overriding tempName and tempWeight will never have it stored. That's why we use data structures. Is because there are collections of data that we want to store and sometimes longer term. So, I'm going to say names.push_back(tempName); and then weights.push_back(tempWeight);. Now, since these are both empty vectors and they have started with nothing and then when I call push_back on the first time through, it will add them both to the vector at index zero or the corresponding vectors at index zero. So, that's exactly what we want. So, we might also create some new line characters here to do some separation between all the prompts and the input. And now, I'm going to do this. I'll use a traditional for loop just to keep things very simple and to show some alternatives to what we use sometimes. So, see our names at i that will give me at indices 0, 1, 2, 3, 4 because it goes up to five but this is strictly less than five, so each time through the loop it will keep going up. So, when it reaches the termination by making the loop continuation statement false, we will then break out of the loop. So, right now though, we will say this. Whoever the person is, notice there's a space on either side inside the double quotes. I have spaces and then we put "<< weights[i] <<" and then  << "lbs" <<. Now, if you want to use kilograms or something like that, that's totally fine. So, I'm in the United States so remember Americans use anything but the metric system. So, that should be it. Let's see what we've got here and there we go. So, this is the input. So, person's full name will say John Baugh and I'm going to say 180, I wish. Person's full name, we'll say Rob Percival and we'll say that he's a dapper 150. We'll say Sally Snuggles. Make up some names and Sally Snuggles is 120. We'll say Sandy Shores weighs 110 and then think, I think this is our last person. We'll make it Randy Taylor, one of my friends. So, we'll say he weighs, I think he's 200. Pretty tall,. So, we've got John Baugh weighs 180, Rob Percival weighs 150, Sally Snuggles weighs 120. Sandy shores weighs 110 and Randy Taylor weighs 200 lbs. Isn't that awesome? Hopefully, you found this to be an interesting project. It was a bit of a new use of vectors and although I explained what to do initially, I didn't give you all the step by step instructions before we went through it together. This is to expose you to more applications of concepts and skills that we work with. In industry and real life applications, you aren't given all the instructions and have to use your own creativity and pull on your knowledge to come up with a solution. And it's okay if your solution was different than mine as long as you accomplish the goal. Being a software developer is as much about being a problem solver as it is knowing specific languages. So, practicing with both problem solving and languages is important. Next up is the section wrap up. I'll see you there.

 

About the Author
Students
297
Courses
20
Learning Paths
4

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