Programming with Visual C++: Concepts and Projects Chapter 2A: Reading, Processing and Displaying Data (Concepts) Objectives In this chapter, you will: • Develop algorithms to solve a problems • Learn about the standard C++ data types • Declare and initialize variables • Read data using the TryParse() method • Use the standard C++ arithmetic operators Programming with Visual C++: Concepts and Projects Objectives (continued) • Process data using arithmetic expressions • Abbreviate lines of code using shorthand assignment operators • Use the Math::Pow() and Math::Sqrt() methods • Display output using the ToString() method Programming with Visual C++: Concepts and Projects Solving Problems • An algorithm is an ordered list of steps required to solve a problem • Algorithms are written in pseudocode • Algorithms are programming language independent – Written in English pseudocode – Not written in a programming language Programming with Visual C++: Concepts and Projects Solving Problems (continued) • High-level algorithms provide general list of tasks (as shown in Algorithm 2-1) • Low-level algorithms provide specific details Programming with Visual C++: Concepts and Projects Data and Data Types • Data includes everything entered into your program (numbers and characters for example) • Data is represented using binary digits called bits • A grouping of eight bits is called a byte • Different kinds of data are called data types Programming with Visual C++: Concepts and Projects Data and Data Types (continued) Programming with Visual C++: Concepts and Projects Data and Data Types (continued) Programming with Visual C++: Concepts and Projects Data and Data Types (continued) • Primitive data types – Boolean (bool) used to represent true or false – Character (char) represents a single character • ASCII codes represent char data in one byte – Integer (int) includes positive and negative whole numbers and zero – Real numbers (float and double) includes all values that may have decimal places • float – Represents single precision numbers • double – Represents double-precision numbers Programming with Visual C++: Concepts and Projects Data and Data Types (continued) Programming with Visual C++: Concepts and Projects Data and Data Types (continued) • Derived data types • Built on primitive types • The String data type is used to represent text strings • Text strings • Example: “Hello world!” Programming with Visual C++: Concepts and Projects Variables • A variable is a named location in memory that stores data • Variables are created by a process called declaration • When a variable is declared: • One or more memory cells are allocated in memory • The allocated space is assigned a name • The allocated space is associated with a data type Programming with Visual C++: Concepts and Projects Variables (continued) • Variables may be declared on separate lines • Or all on one line (if they are the same data type) Programming with Visual C++: Concepts and Projects Variables (continued) Programming with Visual C++: Concepts and Projects Variables (continued) • Examples of valid variable declarations Programming with Visual C++: Concepts and Projects Variables (continued) • Variable naming conventions • Names must begin with letter or underscore (_) and can only use letters, digits or underscores in the body of the name • Names are case sensitive (height is not the same name as Height) • Spaces cannot be used as separators Programming with Visual C++: Concepts and Projects Variables (continued) • Invalid variable names Programming with Visual C++: Concepts and Projects Variables (continued) • Valid variable names Programming with Visual C++: Concepts and Projects Initializing Variables • Initialization refers to the act of assigning a value to a variable before it is used • By default, Visual C++ initializes all numeric variables to 0 when the variable is declared • The programmer can initialize variables using assignment statements Programming with Visual C++: Concepts and Projects Initializing Variables (continued) • Variables can also be initialized when they are declared • In Example 2-5, num1 and num2 are declared as integers. – num1 is initialized to 0 by default – num2 is initialized to 5 Programming with Visual C++: Concepts and Projects Initializing Variables (continued) Programming with Visual C++: Concepts and Projects Data Input • Data input refers to the process of reading data from an object on the interface (like a Textbox) into a variable • Remember that variables exist in memory, they are not the same as visible objects like textboxes Programming with Visual C++: Concepts and Projects Data Input (continued) Programming with Visual C++: Concepts and Projects The TryParse() Method • Used to get data from a TextBox on the interface into a variable in the program’s memory • Parses the Text contained in a TextBox • Parsing is the process of extracting data from a string of text • Every data type class has a built-in TryParse() method Programming with Visual C++: Concepts and Projects The TryParse() Method (continued) • The scope resolution operator (::) identifies the data type class that TryParse() belongs to (for example: Int32) • In Example 2-6 TryParse() is used to parse the contents of textBox1->Text as a 32-bit integer and assign the integer result to variable age Programming with Visual C++: Concepts and Projects The TryParse() Method (continued) • The TryParse() method makes use of two parameters – textBox1->Text – age • Parameters are items of information that a method requires • A parameter list is a groups of parameters separated by commas Programming with Visual C++: Concepts and Projects The TryParse() Method (continued) Programming with Visual C++: Concepts and Projects Data Processing • Computations often involve arithmetic expressions • An arithmetic operator is a symbol that stands for an arithmetic operation • Arithmetic operations are – – – – – Multiplication (*) Division (/) Mod (%) Addition (+) Subtraction (-) Programming with Visual C++: Concepts and Projects Arithmetic Operators • Operands are variables or values that arithmetic operations are performed on • Arithmetic operators are binary (require two operands, one to the left and another to the right of the operator) • Arithmetic operations are performed from left to right (left to right associative) • Examples: – num = num1 + num2; – num = num + 1; Programming with Visual C++: Concepts and Projects Arithmetic Operators (continued) Programming with Visual C++: Concepts and Projects Arithmetic Operators (continued) Programming with Visual C++: Concepts and Projects Operator Precedence in Arithmetic Expressions • Complex arithmetic expressions have more than one arithmetic operator • Operator precedence rules dictate which operations are performed first • Multiplication (*), division (/) and mod (%) have higher precedence than addition (+) and subtraction (-) • When two or more operators of the same precedence are in an expression they are performed from left to right Programming with Visual C++: Concepts and Projects Operator Precedence in Arithmetic Expressions (continued) Programming with Visual C++: Concepts and Projects Operator Precedence in Arithmetic Expressions (continued) Programming with Visual C++: Concepts and Projects Operator Precedence in Arithmetic Expressions (continued) • Parentheses have higher precedence than all arithmetic operators • Parentheses can be used to perform lower precedence operations before higher ones • Expression trees are used to illustrate the order in which operations are completed when a complex expression is evaluated Programming with Visual C++: Concepts and Projects Operator Precedence in Arithmetic Expressions (continued) Programming with Visual C++: Concepts and Projects Operator Precedence in Arithmetic Expressions (continued) Programming with Visual C++: Concepts and Projects Arithmetic Operators and Strings • Addition operator (+) is used to add numeric data • When the operator (+) is used with strings it does not perform addition, instead, it joins the strings together into one (this is called concatenation) • Example: – textBox3->Text = textBox1->Text + textBox2->Text; Programming with Visual C++: Concepts and Projects Arithmetic Operators and Strings (continued) Programming with Visual C++: Concepts and Projects Shorthand Assignment • Shorthand operators are abbreviations for assignment statements involving arithmetic operations • Shorthand operators: +=, /=, %=, +=, -= • Example: – The shorthand version of num1 = num1 + num2; – is num1 += num2; Programming with Visual C++: Concepts and Projects The Math Library • The system-defined Math class contains methods that perform commonly-used tasks • Example: – To perform exponentiation (23) • Math::Pow(2,3) – To find the square root of 45 • Math::Sqrt(45) – To find the length of the hypotenuse of a right triangle • c=Math::Sqrt(Math::Pow(a,2)+Math:Pow(b,2)); Programming with Visual C++: Concepts and Projects The Math Library (continued) Programming with Visual C++: Concepts and Projects The Math Library (continued) • Common Math methods – – – – – Exponentiation, Math::Pow() Square Root, Math::Sqrt() Cosine, Math::Cos() Sine, Math::Sin() Tangent, Math::Tan() • There are many others Programming with Visual C++: Concepts and Projects Data Output • To display a value in the Text property of a TextBox it must be converted to a String • The ToString() method creates a string of text from a non-text variable • Every primitive data type in Visual C++ has a built-in ToString() method • Example: – textBox3->Text = sum.ToString(); Programming with Visual C++: Concepts and Projects Data Output (continued) Programming with Visual C++: Concepts and Projects Summary • Solving problems requires algorithms – Step-by-step lists of instructions • Commonly used data types are – Bool, char, int, float, and double – The String data type stores character strings • Data is stored in variables • Variable declarations allocate, name and assign a data type to memory cells • Assignment statements are a common way to initialize variables Programming with Visual C++: Concepts and Projects Summary (continued) • Data Input – Captures the contents of a TextBox and stores the result in a variable – TryParse() • Data Processing – Common arithmetic operations (*,/,%,+,-) – Precedence rules (*,/ and % before +,-) – Expression trees are used to evaluate arithmetic expressions Programming with Visual C++: Concepts and Projects Summary (continued) • Data Output – Transferring data from a variable to a TextBox – Use the ToString() method Programming with Visual C++: Concepts and Projects