Top Banner
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
49

VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

Jan 21, 2016

Download

Documents

Amie Holmes
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS

Chapter 2AReading, Processing and Displaying Data (Concepts)

Page 2: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, 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

Page 3: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

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

Page 4: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

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

Page 5: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

Solving Problems (continued)

Visual C++ Programming

5

Page 6: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

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

Page 7: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

Visual C++ Programming

7

Page 8: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

Visual C++ Programming

8

Page 9: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

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

Page 10: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

Data and Data Types (continued)

Visual C++ Programming

10

Page 11: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

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!”

Page 12: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

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

Page 13: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

Variables (continued)

Visual C++ Programming

13

Page 14: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

Variables (continued)

Visual C++ Programming

14

Page 15: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

Visual C++ Programming

15

Page 16: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

Variables (Continued)

Visual C++ Programming

16

Page 17: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

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

Page 18: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

Variables (continued)

Visual C++ Programming

18

Page 19: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

Variables (continued)

Visual C++ Programming

19

Page 20: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

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

Page 21: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

Initializing Variables (continued)

Visual C++ Programming

21

Page 22: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

Initializing Variables (continued)

Visual C++ Programming

22

Page 23: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

Initializing Variables (continued)

Visual C++ Programming

23

Page 24: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

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

Page 25: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

Data Input (continued)

Visual C++ Programming

25

Page 26: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

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

Page 27: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

The TryParse() Method (continued)

Visual C++ Programming

27

Page 28: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

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

Page 29: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

The TryParse() Method (continued)

Visual C++ Programming

29

Page 30: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

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 (-)

Page 31: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

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;

Page 32: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

Visual C++ Programming

32

Page 33: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

Visual C++ Programming

33

Page 34: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

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

Page 35: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

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

Page 36: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

Visual C++ Programming

36

Page 37: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

Visual C++ Programming

37

Page 38: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

Visual C++ Programming

38

Page 39: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

Visual C++ Programming

39

Page 40: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

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;

Page 41: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

Visual C++ Programming

41

Page 42: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

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;

Page 43: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

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()

Page 44: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

Visual C++ Programming

44

Page 45: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

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();

Page 46: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

Visual C++ Programming

46

Page 47: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

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

Page 48: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

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

Page 49: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)

Summary (continued)

Visual C++ Programming

49

Data Output• Transferring data from a variable to a

TextBox• Use the ToString() method