California State University, Sacramento College of Engineering and Computer Science Computer Science 10: Introduction to Programming Logic Activity C Expressions Computers were originally designed for their fantastic ability to perform complex math. That hasn t changed much over the many, many decades since the first mainframes. Even today, most of our favorite applications make use of complex math (behind the scenes). For example, the video games that you play (rather than study) are heavily reliant on computations from determining the path of a projectile, the gravity under your avatar s feet, and even things like making realistic shadows, shading, and mist. Part 1 Arithmetic Operator Precedence Before doing anything on the computer, think about the following expression: 4 + 6 / 2 * 3 What is the result? It could be 15 or it could be 13 or, perhaps, 5. Which is it? Hopefully, you recognize that the right answer is 13. This is because of the "precedence" of operators. The multiplication operator (an asterisk in almost all programming languages) has higher precedence than the addition operator. Both multiplication and division have the same level and are calculated from left-to-right. There is more discussion of this issue in the lecture textbook. Part 2 Simple Mathematics Let's look at a very simple mathematical equation from geometry the diameter of a circle vs. its radius. Of course, back in high school, you learned that the diameter is equal to twice the radius. (Yes, you probably guessed by now, it's time to dust off your geometry boots) 1. Start a new flowchart 2. Add an assignment that computers the radius from given diameter. The following is the pseudocode from the textbook: Set radius = diameter / 2 3. Uh oh, that needs two variables. So, add declarations to the start of your program. Make them real numbers. 4. Let's set the diameter to a value. Add an assignment that sets diameter to 25. 5. Add an output statement to the end of the flowchart. Output the radius.
2 6. Execute your flowchart. Did you get the correct result? If not, see what you did wrong. Let's make the results look better. Just printing a number doesn't help the user. 7. Use an Output statement to make your output look like this. (Remember the concatenation operator?) The radius is 12.5 Hard-coding the diameter to 25 is good for testing, but doesn't make a very useful program. 8. Replace the Diameter assignment statement with an input statement. 9. Add a helpful prompt an output statement asking the user to enter the diameter. In this part, you did the calculations separately from the output statement, saving the result (and then printing that saved result in the output statement). You could have alternatively just output diameter / 2. Both approaches would work. But there is a big difference. By saving the result in a variable, we can reuse it later in the program. Part 3 Mathematical Expressions In your future programs especially if you are doing something with graphics you will use geometry. So, let's do something with circles and shapes. The following is the equation used to calculate the area of a circle. How can we implement this formula in Flowgorithm? The formula uses π right next to the variable r. We know that this means that they will be multiplied. However, computers aren't that smart. So, while we know there is a multiplication operator there, the computer doesn't Also, how are we going to get π? Naturally, you can write out 3.1415, but that can get a bit tiresome and makes your program hard to read. Fortunately, since pi is a common constant, many programming languages have a built-in constant for it. In Flowgorithm, it is simply pi. You can use pi as a constant anywhere a variable will work. Finally, what about the exponent? Some languages provide an exponentiation operator. Many languages (sadly) don't. When the operator is supported (as it is in Flowgorithm), the symbol "^" is used. 1. Add a new declaration to store the area. The formula uses A, but that's very short and not very descriptive. In computer science, it is good to have readable variable names. Use the name: circle. 2. Add a new assignment statement for the area according to the formula above. You created a variable name radius, so make sure you use the right name in your expression instead of r. 3. Output the area of the circle to the screen. 4. Execute and check the results. 5. If you didn't do so already, add a nice caption to the output statement. Let the user know it s the area of a circle.
Take moment to think about how the calculation would look if you didn't use the value stored in radius. Without it, you would have to use diameter/2 instead. Would you need parenthesis around diameter/2?. Think about operator precedence. The answer is "yes", you would need them. The exponent operator is performed before multiplication and division. 3 Part 4 More mathematics Now that you can compute the area of a circle, it would be nice to also compute the area for a few more geometric shapes. Below is the formula to compute the surface area of a sphere. Notice that the mathematical formula again uses A to represent the area. Mathematical books do this all the time. However, for your flowchart, this is a different value than the area of a circle. NOTE: Basic Structure of Programs You might have guessed already that programs generally have a very simple structure. First, variables are declared. Second, input values are read (with prompts). Third, calculations are performed. Finally, the results are output. This basic approach makes programs easier to read, write, and modify. Declare variables Input Values Calculations Output results 1. Add a new variable (a real) to store the surface area of a sphere. Choose a good name. 2. Implement the formula above. 3. Output the calculated result with a nice caption. 4. Execute your flowchart and check the results. Excellent work! So far your flowchart: inputs a diameter (with a nice prompt) and outputs the area of a circle and the surface area of a sphere. Let's add one more geometric shape to your flowchart. This time, you will calculate the surface area of a tube such as a pipe, straw, etc. The formula is below. Notice that you will need another variable to store the surface area of the tube as well as the height.
4 5. Add variable declarations for both height and the area of a tube (which you are calculating). Make both reals (real). 6. Add a new input shape for the height. Put it right after you input the diameter. 7. Add a nice prompt for the height. 8. Add an assignment shape for the volume of a tube. 9. Add an output shape with a nice caption. 10. Execute your flowchart and check the results. Part 5 Putting It All Together Wow! That is a lot of math! You calculated the area of a circle, sphere, tube, and displayed all these calculations nicely to the screen. Your flowchart is actually useful! So, let's use your skills to calculation something truly important the surface area of a Minion. Minions are helpful (and mischievous) little creatures that help Gru with his evil plans. Well, at least when they aren't looking for bananas. But, why does Gru need the surface area of a minion? Perhaps, he is planning to paint them in camouflage. Is he planning to invade Sacramento Hall to take over Sac State? (gasp!) Or, perhaps, he is getting enough sun tan lotion to take them all to the Bahamas! Either way it's probably better we don't know. Minions are actually very mathematically shaped creatures and Gru can use basic geometry to figure out their surface area:
5 So, a minion's surface area is half the area of a sphere (their "forehead"), the surface area of a tube (their "head/body"), and the area of a circle (their "bum"). We can use the formulas above to figure out the result. At this point, it is important to look at what your flowchart already does. You already calculated the volume of sphere, tube, and circle. You could create a very fancy expression by combining all the above formulas. There is nothing wrong with that approach. However, programmers often like to break a large problem into smaller parts. NOTE: Divide and Conquer Sometimes it is useful to break complicated arithmetic expressions down in this way to help you (the human) understand them. Other times, you'll be happy with the parenthesized, and somewhat more complicated, combined expressions. There is no real rule for which is best. Whatever makes it easier for you (and others) to understand is the best choice. Let's first try it the hard way. 1. Add another variable (a real again) to hold the surface of a minion. The name is up to you. 2. Add a new assignment shape to calculate the area of minion. Don't use your existing values for the sphere, circle or tube. Combine the formulas above. 3. Output the result of the calculation. Give it a nice caption. 4. Execute your flowchart and check the results. Once you make sure the math is correct, let's see if doing it the "easy" way gets the same result. 5. Add another variable to store the second minion calculation. Think of a good name. 6. Add an assignment shape to compute the surface area. This time, use the sphere, circle, and tube variables you calculated. 7. Output the result with a nice caption 8. Execute your flowchart. The two calculations should match. If not, you made a mistake. Fix it. Which approach do you prefer?
The End Result 6 Your program should have the following: Variable declarations: diameter, radius, area of circle, area of sphere, area of a tube, area of minion (using a complex expression), and the area of a minion (using existing variables) Input statements: diameter and height Assignments: radius, area of circle, area of sphere, area of a tube, area of minion (using a complex expression), and the area of a minion (using existing variables) Output: all the calculated variables with nice captions.