Fundamentals of C++
The course is part of this learning path
This course provides you with a solid understanding of the fundamentals of C++. We will take a look at the components of the programming language and then put these into practice through a couple of projects that we will run through at the end of the course.
- Learn how to store different types of data in main memory
- Understand how to manipulate and perform operations on that data, including performing arithmetic on numbers
- Understand how programs make decisions
- Learn how you can write your programs to communicate with users
- Beginner coders, new to C++
- Developers looking to upskill by adding C++ to their CV
- Experienced C++ programmers who want to stay sharp!
- College students and anyone studying C++
This is a beginner-level course and so no prior knowledge of C++ is necessary.
This lecture serves as a place to challenge you with a comparable or slightly more complex project than what we've done before in the simple challenges throughout the section. It will continue to challenge what you learned and bring together several components of things that we have learned throughout the section. Specifically for this project, you will create a project called Average of Three but with no spaces of course. In this project you will prompt the user to enter three doubles, which you might consider naming num1, num2 and num3, but that's really up to you. Then you should calculate the mean value which is also called the average. You will print the mean value after the calculation is complete. The mean as you may recall is a measure of central tendency in statistics. It gives an indication of what an average element picked from the sequence of elements might look like. The others would be the median and the mode of course. The mean is calculated by taking the sum of all the numbers then by dividing that sum by the count of the numbers. So, for example, if I have five numbers and I add them all together and then divide by five, that will give me the statistical mean of those numbers. So, I have the requirements right in front of you here in this Power Point presentation and it lists what you're supposed to do. We can also take a look at what the project looks like in Visual Studio. So, over here I have not exposed the code but we will run the program and you will see what a typical interaction might look like. So, if I go to debug start without debugging, you will see that it says please enter a real number. So, I could put regular integer type number or use decimal places. So, 45.5, 50.25 and then 100. And then when I hit 'Enter' there, it says average is 65.25. So, you could ask for all of the numbers at once, meaning you could prompt the user and say enter three doubles and you could obtain those doubles by using the cin object. So, what I want you to do is pause the video and take your best crack at this project. When you think you're done or if you need to see how I do it, come back. But to get better, I strongly recommend you try to do it on your own. Take your time, review lectures if you want to and then when you have completed it or if you just are totally stumped, come back here and we'll do it together. I'll be waiting here, pause the video now. Were you able to create the solution to this project? Let's do it together and see if you've got something similar to me. So, I'm going to hit the X there and I'm going to close out the project that is currently here, and we'll create one together. So, create new project, empty project. Next, I'm going to call this proj-02-01 and then just put AverageOfThree and then we're going to click 'Create' as usual. I will add a new item and we will rename this CPP file, main.cpp, include iostream using namespace std, int main and then return 0 as always. So, I'm going to prompt the user for three doubles. In order to get those doubles though I have to of course declare them. So, I'm going to say double num1, double num1; double num2; and double num3; You could actually put the declarations all on one line like double num1, num2, num3, but I don't really like that style so I don't use it myself. Now, you also need something to hold the average and if we divide a bunch of real numbers, we're definitely going to get a real number in return, so I need to make that a double as well. Let's call this double average and I'm going to prompt the user and obtain the user input for each of these three doubles. So, I'm going to say, "Please enter a real number" and then we're going to do cin >> num1; Then I might say, "Please enter a second real number." Or something to that effect. It doesn't have to be identical to what I've asked for but it will be similar probably to how you've prompted your user, and then I'm going to say, "Please enter the final real number". And then we're going to say cin >> num3. Now we've got all our numbers, now we have to decide what do we do with this. So, we say average = (num1 + num2 + num3). That's done in parentheses because you need the sum first. If you created a separate variable called sum and you got that first before we do the division, that's totally fine too. But just because of the order of operations, just like in regular math, if I didn't put these in parentheses and I said divided by 3 right here because there were three items to divide by, it would actually add this plus this and then it would add this divided by this. So, it takes the last number divide by 3 and then that would not give us what we wanted. So, we have to group these together using parentheses just like math. So, once we've done that we're then going to say, print out, "The average is" and then we're going to say average, right there. So, it should print it out. Notice, I did put a space there right after is and let's run this. So, start without debugging, if we don't have any errors. Good, good, good, please enter a real number. So, we'll say 100, 250 and then 175.75 and then it says the average is 175.25. So, I hope that you were able to solve that problem and if not, don't get too discouraged just keep practicing. Awesome work. The project combined user input, user output, variable declarations, assignment operators, and arithmetic operators. So, used quite a few things that we've learned in this section. In the next project, we will work a little more with strings by creating a Mad Libs Clone. So, I'll see you there.
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.