VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Jan 21, 2016
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS
Chapter 2AReading, Processing and Displaying Data (Concepts)
Objectives
Visual C++ Programming
2
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
Objectives (continued)
Visual C++ Programming
3
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
Solving Problems
Visual C++ Programming
4
An algorithm is an ordered list of steps required to solve a problem
Algorithms are written in pseudocode Algorithms are language independent High-level algorithms provide general list
of tasks Low-level algorithms provide specific
details
Solving Problems (continued)
Visual C++ Programming
5
Data and Data Types
Visual C++ Programming
6
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
Visual C++ Programming
7
Visual C++ Programming
8
Data and Data Types (continued)
Visual C++ Programming
9
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 – used to represent single precision
numbers double – represents double-precision numbers
Data and Data Types (continued)
Visual C++ Programming
10
Data and Data Types (continued)
Visual C++ Programming
11
Derived data types• Built on primitive types• The String data type is used to represent
text strings• Text strings• Example: “Hello world!”
Variables
Visual C++ Programming
12
• 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
Variables (continued)
Visual C++ Programming
13
Variables (continued)
Visual C++ Programming
14
Visual C++ Programming
15
Variables (Continued)
Visual C++ Programming
16
Variables (Continued)
Visual C++ Programming
17
Variable naming conventions• Names must begin with a letter or an
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
Variables (continued)
Visual C++ Programming
18
Variables (continued)
Visual C++ Programming
19
Initializing Variables
Visual C++ Programming
20
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
The programmer can initialize variables using assignment statements
Initializing Variables (continued)
Visual C++ Programming
21
Initializing Variables (continued)
Visual C++ Programming
22
Initializing Variables (continued)
Visual C++ Programming
23
Data Input
Visual C++ Programming
24
Data input refers to the process of reading data into a variable from the program interface
Data is often read in from a TextBox
Data Input (continued)
Visual C++ Programming
25
The TryParse() Method
Visual C++ Programming
26
Parsing is the process of extracting data from a string of text
The TryParse() method parses the Text contained in a TextBox
Every data type class has a built-in TryParse() method
Scope refers to the class or program segment to which something belongs
The scope resolution operator (::) identifies the data type class that TryParse() belongs to
The TryParse() Method (continued)
Visual C++ Programming
27
The TryParse() Method (continued)
Visual C++ Programming
28
Parameters are items of information that a method requires
A parameter list is a groups of parameters separated by commas
The TryParse() Method (continued)
Visual C++ Programming
29
Data Processing
Visual C++ Programming
30
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 (-)
Arithmetic Operators
Visual C++ Programming
31
Operands are the variables or values that arithmetic operations are performed on
Arithmetic operators are binary (require two operands)
Arithmetic operations are performed from left to right (left to right associative)
Examples: num = num1 + num2; num = num + 1;
Visual C++ Programming
32
Visual C++ Programming
33
Operator Precedence in Arithmetic Expressions
Visual C++ Programming
34
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
Operator Precedence in Arithmetic Expressions (continued)
Visual C++ Programming
35
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
Visual C++ Programming
36
Visual C++ Programming
37
Visual C++ Programming
38
Visual C++ Programming
39
Arithmetic Operators and Strings
Visual C++ Programming
40
The addition operator (+) is used to add numeric data
When the operator (+) is used with strings it joins them together (concatenation)
Example: textBox3->Text = textBox1->Text + textBox2->Text;
Visual C++ Programming
41
Shorthand Assignment
Visual C++ Programming
42
Shorthand operators are abbreviations for assignment statements involving arithmetic operations
Shorthand operators: +=, /=, %=, +=, -=
Example: num1 = num1 + num2; is num1 += num2;
The Math Library
Visual C++ Programming
43
The System Math Class contains methods that perform commonly-used tasks
Example: Math::Pow(2,3) Math::Sqrt(45) c = Math::Sqrt(Math::Pow(a,2) + Math:Pow(b,2));
Common Math methods: Exponentiation, Math::Pow() Square Root, Math::Sqrt() Cosine, Math::Cos() Sine, Math::Sin() Tangent, Math::Tan()
Visual C++ Programming
44
Data Output
Visual C++ Programming
45
To display a value in the Text property of a TextBox it must be converted to a String
The ToString() method Example:
textBox3->Text = sum.ToString();
Visual C++ Programming
46
Summary
Visual C++ Programming
47
• 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
Summary (continued)
Visual C++ Programming
48
• 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
Summary (continued)
Visual C++ Programming
49
Data Output• Transferring data from a variable to a
TextBox• Use the ToString() method