Join Peggy Fisher for an in-depth discussion in this video Algorithms, part of Learning C++.
An algorithm is a list of steps needed to solve a problem. It is written in our native tongue and not in any particular programming language. It helps the programmer to think through the problem and describe a possible solution. Let's start by using an example that we might be familiar with. When we want to make cookies, we follow a recipe. The recipe can be considered an algorithm. The recipe starts by identifying all the ingredients we need, then it provides a list of steps that need to be executed in sequence. For example, if the recipe is for chocolate chip cookies, we have to measure the ingredients, pre-heat the oven, mix the ingredients, then scoop the dough onto a cookie sheet.
After the oven is pre-heated, we can place the raw cookie dough in the oven for a specified amount of time, then remove the cookies and let them cool. Finally, the best part, eat the cookies, with milk of course. What would happen if we didn't provide any measurements for the ingredients, or maybe we didn't pre-heat the oven? Or what if we tried to place the ingredients in the oven without mixing them first? Just like a recipe, we need to follow a series of steps to complete our program. Let's try this approach on another problem. We want to write a program that helps us determine how much gas we need to go from Philadelphia to Boston and how much it will cost.
We need to know the miles for the trip, the miles per gallon for the vehicle, and the current price of gasoline. Now that we've described our problem, let's figure out our algorithm for the solution. Step one, ask the user for the miles to be traveled. In our case, how many miles from Philadelphia to Boston? Step two, ask the user for the MPG rating of their vehicle. Step three, ask the user for the current cost of one gallon of gas. Step four, calculate the amount of gas needed for the trip using the equation total miles divided by miles per gallon.
That will tell us how many gallons we need. Step five, using the total gallons from step four, find out the expected cost by multiplying the number of gallons times the price of one gallon of gas. And step six is print the results to the user. In this new example, instead of ingredients, our variables are distance, MPG, or miles per gallon, current cost of one gallon of gasoline, total gallons needed, and finally, a variable to hold the total gas cost for the trip.
When our algorithm is finished, we can use this information to write our program. We start with the variables and define them with a specific data type. Similar to the measurement units in our recipe example, such as teaspoon or cup, when programming, we need to use data types such as integer or double. From here, we can take our algorithm and write code in any programming language
- Downloading and exploring the C++ IDE
- Working with loops
- Using predefined functions
- Creating custom functions
- Creating and instantiating classes
- Working with external files
Skill Level Beginner
Q: How do I upgrade the C++ compiler on Mac OS X and Linux?
A: Refer to C/C++ Essential Training for a detailed look at installing or upgrading the C++ compiler on various platforms.
Q: The link to download the Eclipse IDE in the "Download a C++ IDE" movie doesn't work. Where can I find the IDE?
A: Short URLs are case sensitive and need to be typed in exactly as they appear. Type in or simply click http://goo.gl/CzckWp to visit the Eclipse IDE for C/C++ Developers page.