Array Data 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

For this project, you will do something relatively straightforward but it will also combine different skills you've gained throughout the course. Specifically, you're going to use some of your skills with counter controlled repetition as well as your newfound skills working with arrays. I'm going to use the array class in my solution but feel free to use built-in arrays or the array class for yours. You will read in five integers from the user using standard input, that is, for our purposes input from the keyboard, into the array. And then you will print out each of the values multiplied by two. In other words, you will double each of the integers inside the array. So, don't store the doubled value in the array but double them for printing. So, I want you to create a project called array data. Again, you need to read five integers from the user using standard input. In other words, the keyboard, with each integer going into the array. Then once you're done reading those five integers into the array, you loop through that array and print out each of those values multiplied by two. Here's a quick demonstration of the array data program. So, let's go to debug. Start without debugging and we get 'Enter an integer'. Let's put 15 then 32, 77, 104, and 6. And you'll notice that when they are printed out we get 30, 64, 154, 208 and 12, which are double each of these. So, hopefully that demonstration helps. So, pause the video now and come back when you're done or if you need some help. So, how did that go for you? I'm willing to bet you were able to solve that one. But even if you had some trouble or even just got some of it done, congratulations! This one combined a lot of your previous knowledge with brand new knowledge that we just learned, and that's exactly what learning to program is all about. You keep reviewing what you already know and adding on to your skillset. It's like expanding your toolkit. So, let's work on this project together. First I will create an array data project, and then we'll fill in some code. So, empty project, we call it ArrayData. Hit 'Create'. Create a source file. main. cpp as usual. All right. We're going to use the array class in my solution. But again, you could have used the built-in arrays in yours. Here we go. All right, here. So, I need an array size, I'm just going to use a constant for this and that's five. And for the array that we're declaring, it holds integers and is of size 5. Okay, all right. And we'll call that myInts and, I guess we need to make sure that size int, right? So, now we need to loop through here int i = 0; i < ARRAY_SIZE; i++. And notice you could have actually foregone the use of using a constant here and just used i < myInt.size. So, the size method instead of this constant. But we're just working on using different varieties. So, I'm going to say enter an integer for each iteration of this loop. And then we could use an intermediate variable, an intermediate or temporary integer named like int temp or something. But you can treat any element of an array just like you would any regular variable of that type. So, I'm just going to say myInts[i] and we'll cut out the middleman. So, now what we're going to do is print out double the amounts because if you'll recall that is the key to our solution. So, now here I'll double the amounts and this time I'm going to use a range-based for loop. So, each element in the array is an integer. So, int num, each of those integers in myInts, and inside here I just want to print out num * 2, double the value. So, let's run it. Debug, start without debugging and it says enter an integer. Okay, so 15, 20, 22, 5, and 7. And then it prints out 30, 40, 44, 10, and 14 which if you'll look at each of them, they are double what their corresponding input was, 15 becomes 30, 20 is 40, 22 is 44, 5 is 10, and finally 7 is 14. Awesome, isn't it? We can use cin and the stream extraction operator to directly store data in our myInts array at a particular index. Because remember, an array of integers, for example, contains elements that are each an integer themselves just like any normal variable of type integer. Therefore, anywhere we could use a regular integer variable, you can also use an array of integers specified at that particular subscript or index. Nice job, everyone. In the next lecture, we will do another project. This time, you will get a chance to practice using vectors. Let's get going.

 

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