Data Structures and Algorithms




     Constants and
     Variables

Constants
 @ a value, that is, a specific alphabetical and/or
       numeric value, that never changes during the
       processing of all the instructions in a solution
 @ can be any type of data - numeric, alphabetical,
       or special symbols


Variable
 @ may change during processing

 @ the computer sets up a specific memory location
       to hold the value of each variable name found in a
       program
 @ can be any data type, just as constant



Beginning Problem-Solving Concepts for the Computer                     Page 1 of 17
Data Structures and Algorithms




     Constants and
     Variables

 Rules for Naming and Using Variables
 @ Name a variable according to what it reperesents,
   that is, HOURS for hours worked, PAYRATE for rate
   of pay, and so on.

 @ Do not use spaces in a variable name. If a space is
   needed, use the underline character.

 @ Do not use a dash (or any symbol that is used as a
   mathematical operator) in a variable name. The
   computer will recognize these symbols as
   mathematical operators, turn your variable into
   two or more variables, and treat your variable as
   a mathematical expression.




Beginning Problem-Solving Concepts for the Computer                     Page 2 of 17
Data Structures and Algorithms




     Constants and
     Variables


 Rules for Naming and Using Variables

 @ After you have introduced a variable name that
   represents a specific data item, this exact variable
   name must be used in all places where the data
   item is used.

 @ Be consistent when using upper- and lowercase
   characters.




Beginning Problem-Solving Concepts for the Computer                     Page 3 of 17
Data Structures and Algorithms




       Data Types


Data
  @ unorganized facts

Information
  @ what is returned to the user as output, or
       processed data



                            Input                Data       Output
        DATA                               Processed into                      REPORT
                                             Information



      Checks
                                             Calaculates
     Deposits                                                              Balance Sheet
                                             the balance
     Bk. Chgs.




Beginning Problem-Solving Concepts for the Computer                                    Page 4 of 17
Data Structures and Algorithms




       Data Types



       Data Type                           Data Set              Examples
      Numeric: Integer                 All whole numbers            3570 , -54
                                  All real numbers (whole +
       Numeric: Real                        decimal)           3792.91, 474526.23
        CHARACTER
       (surrounded by              All letters, numbers, and    "A","a", "M", "1",
      quotation marks)                  special symbols           "88","&", "#"

   STRING (surrounded               Combinations of more
   by quotation marks)               than one character        "waahhh", "876860"
        LOGICAL                       TRUE FALSE                 TRUE FALSE




Beginning Problem-Solving Concepts for the Computer                                Page 5 of 17
Data Structures and Algorithms




       Data Types



                              Data                         Data Type
           The price of an item: 12.50, 34.00              Numeric: real

               An account number: "A8693"                 Character String
                    A quantity: 10,345                    Numeric: integer

               The name of a school: "STI"                Character String

              A credit check: TRUE, FALSE                      Logical

              A date: 06/21/03 or "01/11/81           Date or Character String




Beginning Problem-Solving Concepts for the Computer                            Page 6 of 17
Data Structures and Algorithms




       Data Types



Rules for Data Types
@ The data that define the value of a variable or a
  constant will most commonly be one of three data
  types: numeric, character (including character
  string), or logical.

@ The programmer designates the data type during
  the programming process. The computer then
  associates the variable name with the designated
  data type.

@ Data types cannot be mixed.




Beginning Problem-Solving Concepts for the Computer                     Page 7 of 17
Data Structures and Algorithms




       Data Types


Rules for Data Types

@ Each of the data types uses what is called a data
  set. The numeric data uses the set of all base 10
  numbers, the plus sign (+), and the negative sign
  (-); the character type uses the set of data
  consisting of the words TRUE and FALSE. The use
  of any data outside the data set results in an error.

@ Any numeric item that must be used in calculations
  resulting in a numeric results must be designated
  as numeric data type. All other numbers should be
  designated as character or character-string data
  types, even if data are all numbers.




Beginning Problem-Solving Concepts for the Computer                     Page 8 of 17
Data Structures and Algorithms




         Functions

         @ small sets of instructions that perform
               specific tasks and return values

         @ usually built into a computer language or
               application

         @ used as parts of istructions in a solution


Classes of Functions:
          a     Mathematical
          a     String
          a     Conversion
          a     Statistical
          a     Utility




Beginning Problem-Solving Concepts for the Computer                     Page 9 of 17
Data Structures and Algorithms




         Functions



                                           Example            Result
                                            SQRT(4)             2
     Mathematical                           ABS (-3)            3
                                         ROUND (5.678)          6
                                        INTEGER (5.789)         5




                                           Example            Result
                String               MID(S, 3, 2) where S =
                                          "THOMAS"            "OM"
                                     LEFT (S,3) where S =
                                          "THOMAS"            "THO"
                                     RIGHT (S, 3) where S=
                                          "THOMAS"            "MAS"
                                     LENGTH(S) where S =
                                          "THOMAS"              6




Beginning Problem-Solving Concepts for the Computer                            Page 10 of 17
Data Structures and Algorithms




         Functions


                                            Example        Result
     Conversion                           VALUE ("57")      +57
                                          STRING (+57)      "57"



    Statistical                           Example             Result
                                      AVERAGE (1,2,3,4)        2.5
                                        MAX (1,2,3,4)           4
                                        MIN (1,2,3,4)           1
                                        SUM (1,2,3,4)          10


            Utility                            Example       Result
                                                DATE         6/22/03
                                                TIME         2:55:03




Beginning Problem-Solving Concepts for the Computer                        Page 11 of 17
Data Structures and Algorithms




         Operators


  @ the data connectors within expressions and
       equations
  @ tell the computer how to process the data

  @ tell the computer what type of processing
       (mathematical,logical or whatever) needs to be
       done

Types of Operator
  @ Mathematical - include addition, subtraction,
       multiplication, division, integer division, modulo
       division, powers, and functions

  @ Relational - = , <, >, >=, <=, <>

  @ Logical - AND, NOT, and OR



Beginning Problem-Solving Concepts for the Computer                    Page 12 of 17
Data Structures and Algorithms




         Operators

     Mathematical

               Operation                       Resultant
               3.0 + 5.2                         8.2
                7.5 - 4.0                        3.5
                8.0 * 5.0                         40
                 9.0/4.0                          2
                   94                            2
               9 MOD 4                            1
                  3^2                             9


     Relational
               Operation                      Resultant
                 3=2                           FALSE
                  5>1                          TRUE
                 5=1                           FALSE
                7 <= 8                         TRUE
                9>=12                          FALSE
                10 < 2                         TRUE
                 8<>8                          FALSE




Beginning Problem-Solving Concepts for the Computer                         Page 13 of 17
Data Structures and Algorithms




         Operators

   Logical

                     A                         Not A
                     T                          F
                     F                          T




                      A                               B       A AND B
                      T                               T          T
                      T                               F          F
                      F                               T          F
                      F                               F          F




                       A                                  B     A OR B
                       T                                  T        T
                       T                                  F        T
                       F                                  T        T
                       F                                  F        F


Beginning Problem-Solving Concepts for the Computer                               Page 14 of 17
Data Structures and Algorithms




         Operators

Hierarchy of Operations
 ( ) Reorders the hierarchy; all operations are
 completed within the parentheses using the same
 hierarchy.

      1. Functions

      Mathematical:
      2. Power
      3. , MOD
      4. *, /
      5. +, -

      Relational:
      6. =, <, >, <=, >=, <>

      Logical:
      7. NOT
      8. AND
      9. OR
Beginning Problem-Solving Concepts for the Computer                    Page 15 of 17
Data Structures and Algorithms




     Expressions and
     Equations

Expression
  @ processes data, the operands, through the use of
       operators
  @ example: LENGTH * WIDTH


Equation
  @ stores the resultant of an expression in a memory
       location in the computer through the equal (=) sign
  @ example: AREA = LENGTH * WIDTH

  @ often called assignment statements because the
       variable on the left-hand side of the equal sign is
       assigned the value of the expression on the right-
       hand side
  @ the equal sign does not mean equals; instead it
       means replaced by or is assigned the value of

Beginning Problem-Solving Concepts for the Computer                    Page 16 of 17
Data Structures and Algorithms




              Expressions and
              Equations




Beginning Problem-Solving Concepts for the Computer                 Page 17 of 17

2 beginning problem solving concepts for the computer

  • 1.
    Data Structures andAlgorithms Constants and Variables Constants @ a value, that is, a specific alphabetical and/or numeric value, that never changes during the processing of all the instructions in a solution @ can be any type of data - numeric, alphabetical, or special symbols Variable @ may change during processing @ the computer sets up a specific memory location to hold the value of each variable name found in a program @ can be any data type, just as constant Beginning Problem-Solving Concepts for the Computer Page 1 of 17
  • 2.
    Data Structures andAlgorithms Constants and Variables Rules for Naming and Using Variables @ Name a variable according to what it reperesents, that is, HOURS for hours worked, PAYRATE for rate of pay, and so on. @ Do not use spaces in a variable name. If a space is needed, use the underline character. @ Do not use a dash (or any symbol that is used as a mathematical operator) in a variable name. The computer will recognize these symbols as mathematical operators, turn your variable into two or more variables, and treat your variable as a mathematical expression. Beginning Problem-Solving Concepts for the Computer Page 2 of 17
  • 3.
    Data Structures andAlgorithms Constants and Variables Rules for Naming and Using Variables @ After you have introduced a variable name that represents a specific data item, this exact variable name must be used in all places where the data item is used. @ Be consistent when using upper- and lowercase characters. Beginning Problem-Solving Concepts for the Computer Page 3 of 17
  • 4.
    Data Structures andAlgorithms Data Types Data @ unorganized facts Information @ what is returned to the user as output, or processed data Input Data Output DATA Processed into REPORT Information Checks Calaculates Deposits Balance Sheet the balance Bk. Chgs. Beginning Problem-Solving Concepts for the Computer Page 4 of 17
  • 5.
    Data Structures andAlgorithms Data Types Data Type Data Set Examples Numeric: Integer All whole numbers 3570 , -54 All real numbers (whole + Numeric: Real decimal) 3792.91, 474526.23 CHARACTER (surrounded by All letters, numbers, and "A","a", "M", "1", quotation marks) special symbols "88","&", "#" STRING (surrounded Combinations of more by quotation marks) than one character "waahhh", "876860" LOGICAL TRUE FALSE TRUE FALSE Beginning Problem-Solving Concepts for the Computer Page 5 of 17
  • 6.
    Data Structures andAlgorithms Data Types Data Data Type The price of an item: 12.50, 34.00 Numeric: real An account number: "A8693" Character String A quantity: 10,345 Numeric: integer The name of a school: "STI" Character String A credit check: TRUE, FALSE Logical A date: 06/21/03 or "01/11/81 Date or Character String Beginning Problem-Solving Concepts for the Computer Page 6 of 17
  • 7.
    Data Structures andAlgorithms Data Types Rules for Data Types @ The data that define the value of a variable or a constant will most commonly be one of three data types: numeric, character (including character string), or logical. @ The programmer designates the data type during the programming process. The computer then associates the variable name with the designated data type. @ Data types cannot be mixed. Beginning Problem-Solving Concepts for the Computer Page 7 of 17
  • 8.
    Data Structures andAlgorithms Data Types Rules for Data Types @ Each of the data types uses what is called a data set. The numeric data uses the set of all base 10 numbers, the plus sign (+), and the negative sign (-); the character type uses the set of data consisting of the words TRUE and FALSE. The use of any data outside the data set results in an error. @ Any numeric item that must be used in calculations resulting in a numeric results must be designated as numeric data type. All other numbers should be designated as character or character-string data types, even if data are all numbers. Beginning Problem-Solving Concepts for the Computer Page 8 of 17
  • 9.
    Data Structures andAlgorithms Functions @ small sets of instructions that perform specific tasks and return values @ usually built into a computer language or application @ used as parts of istructions in a solution Classes of Functions: a Mathematical a String a Conversion a Statistical a Utility Beginning Problem-Solving Concepts for the Computer Page 9 of 17
  • 10.
    Data Structures andAlgorithms Functions Example Result SQRT(4) 2 Mathematical ABS (-3) 3 ROUND (5.678) 6 INTEGER (5.789) 5 Example Result String MID(S, 3, 2) where S = "THOMAS" "OM" LEFT (S,3) where S = "THOMAS" "THO" RIGHT (S, 3) where S= "THOMAS" "MAS" LENGTH(S) where S = "THOMAS" 6 Beginning Problem-Solving Concepts for the Computer Page 10 of 17
  • 11.
    Data Structures andAlgorithms Functions Example Result Conversion VALUE ("57") +57 STRING (+57) "57" Statistical Example Result AVERAGE (1,2,3,4) 2.5 MAX (1,2,3,4) 4 MIN (1,2,3,4) 1 SUM (1,2,3,4) 10 Utility Example Result DATE 6/22/03 TIME 2:55:03 Beginning Problem-Solving Concepts for the Computer Page 11 of 17
  • 12.
    Data Structures andAlgorithms Operators @ the data connectors within expressions and equations @ tell the computer how to process the data @ tell the computer what type of processing (mathematical,logical or whatever) needs to be done Types of Operator @ Mathematical - include addition, subtraction, multiplication, division, integer division, modulo division, powers, and functions @ Relational - = , <, >, >=, <=, <> @ Logical - AND, NOT, and OR Beginning Problem-Solving Concepts for the Computer Page 12 of 17
  • 13.
    Data Structures andAlgorithms Operators Mathematical Operation Resultant 3.0 + 5.2 8.2 7.5 - 4.0 3.5 8.0 * 5.0 40 9.0/4.0 2 94 2 9 MOD 4 1 3^2 9 Relational Operation Resultant 3=2 FALSE 5>1 TRUE 5=1 FALSE 7 <= 8 TRUE 9>=12 FALSE 10 < 2 TRUE 8<>8 FALSE Beginning Problem-Solving Concepts for the Computer Page 13 of 17
  • 14.
    Data Structures andAlgorithms Operators Logical A Not A T F F T A B A AND B T T T T F F F T F F F F A B A OR B T T T T F T F T T F F F Beginning Problem-Solving Concepts for the Computer Page 14 of 17
  • 15.
    Data Structures andAlgorithms Operators Hierarchy of Operations ( ) Reorders the hierarchy; all operations are completed within the parentheses using the same hierarchy. 1. Functions Mathematical: 2. Power 3. , MOD 4. *, / 5. +, - Relational: 6. =, <, >, <=, >=, <> Logical: 7. NOT 8. AND 9. OR Beginning Problem-Solving Concepts for the Computer Page 15 of 17
  • 16.
    Data Structures andAlgorithms Expressions and Equations Expression @ processes data, the operands, through the use of operators @ example: LENGTH * WIDTH Equation @ stores the resultant of an expression in a memory location in the computer through the equal (=) sign @ example: AREA = LENGTH * WIDTH @ often called assignment statements because the variable on the left-hand side of the equal sign is assigned the value of the expression on the right- hand side @ the equal sign does not mean equals; instead it means replaced by or is assigned the value of Beginning Problem-Solving Concepts for the Computer Page 16 of 17
  • 17.
    Data Structures andAlgorithms Expressions and Equations Beginning Problem-Solving Concepts for the Computer Page 17 of 17