Jump to content

Programming Fundamentals/Practice: Data and Operators

From Wikibooks, open books for an open world

Chapter Summary

[edit | edit source]
  • Constants and Variables: In a program, a constant is a value that cannot be changed during the execution of the program. With the addition of an identifier, it can become a "named" constant. "Constant" and "Named constant" can often be used interchangeably. Example: const float PI = 3.1415927 in the program PI will always be that value no matter what the user inputs. On the other hand, you have a variable, which is a value that can be changed during the execution of the program. Example: yard = mile * 1760 in this program the output for "yard" will change depending on what the user inputs for "mile" which makes it a variable.
  • Identifier Names: When an item is declared or defined, it is identified by a name. Some examples of items that can be named are constants, variables, type definitions, and functions. These names help identify the function of the item.
  • Data Types: A data type is a classification of data that tells the compiler or interpreter how the programmer intends to use the data. There are five types:
    • Integer Data Type - Whole numbers with no fractional parts
    • Floating-Point Data - Formulaic representation of real numbers (scientific notation)
    • String Data Type - a string of characters being either a literal constant or a variable
    • Boolean Data Type - has one of two possible values (true or false)
    • Nothing Data Type - a feature of some programming languages which allows the setting of special values to indicate a missing or uninitialized value rather than using the value 0
  • Order of Operations: Mathematical rules that govern the order in which procedures (addition, multiplication, etc.) are carried out in a term.
  • Assignment: The assignment operator, which is typically an equals symbol (=), sets or changes the value of a modifiable data object, usually a variable. The operand on the left (Lvalue) of the assignment operator is the modifiable object and the operand on the right (Rvalue) is typically the value that is assigned to the modifiable object. For novice programmers, the assignment operator (=) is often confused with the relational operator (==) which is used for comparison or as a test expression. [1]

Arithmetic Operators: Arithmetic operations are functions that represent basic arithmetic. The basic arithmetic operations are addition, subtraction, multiplication, and division. All operations also follow the order of operations as well.

Integer Division and Modulus: Integer division and Modulus are when the dividend is divided by the divisor and turned into a quotient. The modulus is the remainder of the integer operation. This is because, in programs, integer values are always handled in whole units.

Unary Operations: Unary operations are operations with only one operand. The most common values of the operand are negative and positive, also known as Unary positive and Unary Negative. Unary Negative is a value that can change a sign and flip it, while Unary positive isn't able to change any values and considered worthless.

Lvalue and Rvalue: Lvalue and Rvalue are the left and right side of the assignment operator, where the Lvalue is considered to be the expression that can be modified, and the right side is a temporary expression value that can change.

Data Type Conversions: Data type conversions are when you change the value of a data type, also referred to as "type conversion". There are two types of type conversions, Implicit and Explicit. Implicit is when the change is simply implied, whereas Explicit is when a change is done with an operator or function. The value can have a Promotion when the smaller domain is changed to a bigger domain, or Demotion, where the larger domain is changed to a smaller domain.

Input-Process-Output Model: IPO(Input-Process-Output) model is a widely used approach for describing the structure of multiple programs in system analysis and software engineering. The model is designed to detect inputs and outputs and specific processing tasks that are required to turn inputs into outputs in these programs.

Review Questions

[edit | edit source]

True or false:

  1. A data type defines a set of values and the set of operations that can be applied to those values.
  2. Reserved or keywords can be used as identifier names.
  3. The concept of precedence says that some operators (like multiplication and division) are to be executed before other operators (like addition and subtraction).
  4. An operator that needs two operands will promote one of the operands as needed to make both operands be of the same data type.
  5. Parentheses change the precedence of operators.
  6. Integer data types are stored with a mantissa and an exponent.
  7. Strings are identified by single quote marks in most programming languages.
  8. An operand is a value that receives the operator’s action.
  9. Arithmetic assignment is a shorter way to write some expressions.
  10. Integer division is rarely used in computer programming.
  11. The Nothing data type is the same as the value 0 (zero).
  12. A boolean data type has two or more possible values. One possibility can be a null data type.
  13. A constant can change its value.
  14. The Pascal case standard uses all lowercase letters with underscores separating words.

Answers:

  1. true
  2. false
  3. true
  4. true
  5. false – Parentheses change the order of evaluation in an expression.
  6. false
  7. false - Strings can be identified by double quotation marks as well.
  8. true
  9. true
  10. false
  11. false
  12. false - Boolean is a binary variable, only having two possible values, such as true/false.
  13. false
  14. false

Variables:
In each of the following, determine appropriate identifier names and data types:

  1. You are buying paint for a mural project in your neighborhood, so you must calculate how many gallons of paint you'll need.
  2. You want to open a savings account at a bank, but you are not sure which bank is best for you. You decide to compare each bank's interest rate to see where you'll get the most money.
  3. There is a sale at your local supermarket, and you want to know how much you saved on your purchase.
  4. You are taking a poll to see which flavor of ice cream people like most at your school.
  5. A condominium complex decides to open a pool and wants to know how many cubic feet of space they need to dig out.

Short Answer:

  1. A men’s clothing store that caters to the very rich wants to create a database for its customers that records clothing measurements. They need to record information for shoes, socks, pants, dress shirts and casual shirts. Explain how you would create a program that records this information using your new knowledge of assigning values and data types. List the steps you would take and why you would take them. HINT: You may need more than 5 data items.
  2. The sequence operator can be used when declaring multiple identifier names for variables or constants of the same data type. Is this a good or bad programming habit and why?
  3. Explain how you would correctly display something that includes two different types of data. For example, how would you display something that says "John is" + (integer variable with Johns age) " years old"?
  4. You are creating a program that converts inches to centimeters. Using the input-process-output model, list the steps required to carry out the operation.
  5. What is the correct order of operations using these 6 terms: Addition, Subtraction, Multiplication, Division, Parentheses, and Exponents?

Activities

[edit | edit source]

Complete the following activities using pseudocode, a flowcharting tool, or your selected programming language. Use appropriate data types for each variable, and include separate statements for input, processing, and output. Create test data to validate the accuracy of each program. Add comments at the top of the program and include references to any resources used.

  1. Create a program to prompt the user for hours and rate per hour and then calculate and display their weekly, monthly, and annual gross pay (hours * rate).[2]
  2. Create a program that asks the user how old they are in years, and then calculate and display their approximate age in months, days, hours, and seconds. For example, a person 1 year old is 12 months old, 365 days old, etc.
  3. Review MathsIsFun: US Standard Lengths. Create a program that asks the user for a distance in miles, and then calculate and display the distance in yards, feet, and inches, or ask the user for a distance in miles, and then calculate and display the distance in kilometers, meters, and centimeters.
  4. Review MathsIsFun: Area of Plane Shapes. Create a program that asks the user for the dimensions of different shapes and then calculate and display the area of the shapes. Do not include shape choices. That will come later. For now, just include multiple shape calculations in sequence.
  5. Create a program that calculates the area of a room to determine the amount of floor covering required. The room is rectangular with the dimensions measured in feet with decimal fractions. The output needs to be in square yards. There are 3 linear feet (9 square feet) to a yard.
  6. Create a program that helps the user determine how much paint is required to paint a room and how much it will cost. Ask the user for the length, width, and height of a room, the price of a gallon of paint, and the number of square feet that a gallon of paint will cover. Calculate the total area of the four walls as 2 * length * height + 2 * width * height Calculate the number of gallons as: total area / square feet per gallon Note: You must round up to the next full gallon. To round up, add 0.9999 and then convert the resulting value to an integer. Calculate the total cost of the paint as: gallons * price per gallon.
  7. Review Wikipedia: Aging in dogs. Create a program to prompt the user for the name of their dog and its age in human years. Calculate and display the age of their dog in dog years, based on the popular myth that one human year equals seven dog years. Be sure to include the dog's name in the output, such as:
        Spike is 14 years old in dog years.

References

[edit | edit source]

See Also

[edit | edit source]
  1. http://flowgorithm.org/documentation/operators.htm
  2. http://flowgorithm.org/documentation/declare.htm
  3. http://flowgorithm.org/documentation/output.htm
  4. http://flowgorithm.org/documentation/types.htm
  5. http://flowgorithm.org/documentation/input.htm