Return the Sum of Array Object Elements Project

Start course
Overview
Difficulty
Intermediate
Duration
2h 27m
Students
16
Ratings
5/5
starstarstarstarstar
Description

Functions in C++ are reusable named pieces of code that we can call or invoke when we need them to do something. They help us to take large problems and break them down so that they are more manageable. This course explores functions and puts them to use in a range of projects.

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

To help you work with a variety of data structures as you use functions, this time, I'd like you to do the same thing we did before, essentially, some of the elements of an array. However, the array should be an array object and your function only needs one parameter since array objects know their own size. However, don't forget that the array object itself takes two type parameters: the data type and the size. Let's make this array size 10 this time. Also you can use a range-based for-loop if you'd like. Make sure to test it out using an array you create and don't forget the array library. Let's take a quick look at how this program will run before you pause to work on it. So, without showing you the code, I'm going to go up to Debug, Start Without Debugging. And very simply, it does almost what the other one did. It just says, "The result is" instead of "The sum is", but it doesn't really matter, you could put, "The sum is". So, The result is 110. Now, I'd like you to pause the video and create the project named SumArrayObject, and give this one a shot. Meet me back here when you're done. Were you able to solve this problem? Let's take a look at it together. I'm going to close this project or the solution, and we're going to create a new project. Empty Project. And we will call this SumArrayObject. Hit 'Create'. And, of course, create our main.cpp file. Fill up the skeleton code. We know that we need the array library this time. So, we need to put that. All right. And we need sumArray this time. We need an array object being passed in. And this one we have to specify the type parameters. So, let's put the function prototype as the header near the bottom. And like before, we need an accumulator; this thing that we put the sum in. And we'll use the range-based for. So, this time we'll use a range-based for. sum += item. And then, of course, at the end we return the sum. We need to test it out, so we need to create an array inside of main, with 10 elements. So, I'll call it primaryArray just to show we don't need the same name. This one's going to have 2, 4, 6, 8, 10, 12, 14, 16, 18, 20. And you could, of course, have used a loop to populate this or whatever you'd really like to do. We're going to put theResult = sumArray, pass in primaryArray, and then print out the result. So, The result is, theResult, end now. And, of course, we're going to run it and test it out to make sure that it works. Debug, Start Without Debugging. And it does, in fact, work. The result is 110. So, it goes into this function right here. Initializes the sum to zero. And for each item, adds it to this sum here. Again, this is the addition assignment compound operator. So, it basically is combined. It's a shortcut for sum= sum+ item as we've used it before. And then we return the sum, and that's all we need to do. Awesome. Awesome. So, we have the expected result. You can verify it with a calculator or another technique if you'd like. You're getting really good at this whole working with functions thing. So, keep it up. The next project is going to, yet again, involve summing the values in an array object. But this time, we will make the return type void. So, how are we going to get the sum back if we don't have a return? Well, meet me in the next lecture and let's check it out. See you there.

 

About the Author
Students
316
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