Data Types - Computing in Engineering and Science - Lecture Slides, Slides of Computer Science

Larry Caretto delivered this lecture in Computing in Engineering and Science course. Key points in this lecture are: Data Types, Memory, Computer Memory, Integer Data Types, Floating Point Data Types, Approximate Representation, Constant Types, Declaring Variables

Typology: Slides

2013/2014

Uploaded on 02/01/2014

savitri_122
savitri_122 🇮🇳

4.6

(14)

184 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Types February 7, 2006
1
Data Types
Data Types
Declarations
Declarations
and Initializations
and Initializations
Larry Caretto
Computer Science 106
Computing in Engineering
and Science
February 7, 2005
2
Outline
Review last week
Meaning of data types
Integer data types have no decimal
point and integer division truncates
Floating point data types: approximate
representation of decimal numbers
Character, string and boolean (logical)
data types
3
Review of Last Week
Basic elements of C++ programs
Template #include, using namespace std;
int main(), return EXIT_SUCCESS;
C++ is case sensitive
White space does not matter except in
string constants
End statements with a semicolon (;)
Braces { and } frame logical code blocks
Will use in lab this week
4
Review of Last Week II
Screen output using cout and <<
Keyboard input using cin and >>
Variables refer to memory locations
Use letters, numbers and _
Start with letter or _
31 characters maximum
Use meaningful names
Variables are case sensitive (x2 is not X2)
5
Review Variables and Memory
Program variables refer to computer
memory (RAM) locations
When we use a variable (say x) we get
the value in the memory location the
compiler associated with this variable
x = 3; // assigns the value 3 to x
cout << x; // writes value of x to the screen
y = x; // assigns value of x to y
x = x + 3; // replaces x by x + 3
6
Computer Memory
Cells show memory address, associated
variable name and value stored (if any)
What is effect of y2 = PI * rad * rad?
What happens to cell 106? to cells 102 and 104?
Cell 106 gets the new value of π(3.5)2
Cells 102 and 104 are not changed
107 _data
13
106 y2105 nam
“CSUN”
104 rad
3.5
103 i
0
102 PI
3.1415926
101 x1
15
100 Var
12.2
Address
Variable
name
Value
pf3
pf4
pf5

Partial preview of the text

Download Data Types - Computing in Engineering and Science - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Data TypesData Types –– DeclarationsDeclarations

and Initializationsand Initializations

Larry Caretto Computer Science 106

Computing in Engineering

and Science

February 7, 2005

2

Outline

  • Review last week
  • Meaning of data types
  • Integer data types have no decimal

point and integer division truncates

  • Floating point data types: approximate

representation of decimal numbers

  • Character, string and boolean (logical)

data types

3

Review of Last Week

  • Basic elements of C++ programs
    • Template #include, using namespace std; int main(), return EXIT_SUCCESS;
    • C++ is case sensitive
    • White space does not matter except in string constants
    • End statements with a semicolon (;)
    • Braces { and } frame logical code blocks
    • Will use in lab this week 4

Review of Last Week II

  • Screen output using cout and <<
  • Keyboard input using cin and >>
  • Variables refer to memory locations
    • Use letters, numbers and _
    • Start with letter or _
    • 31 characters maximum
    • Use meaningful names
    • Variables are case sensitive (x2 is not X2)

5

Review Variables and Memory

  • Program variables refer to computer

memory (RAM) locations

  • When we use a variable (say x) we get

the value in the memory location the

compiler associated with this variable

  • x = 3; // assigns the value 3 to x
  • cout << x; // writes value of x to the screen
  • y = x; // assigns value of x to y
  • x = x + 3; // replaces x by x + 3 6

Computer Memory

  • Cells show memory address, associated variable name and value stored (if any)
  • What is effect of y2 = PI * rad * rad?
    • What happens to cell 106? to cells 102 and 104?
    • Cell 106 gets the new value of π(3.5) 2
    • Cells 102 and 104 are not changed

107 _ data

105 nam 106 y

“CSUN”

104 rad

103 i

102 PI

101 x

100 Var

Address

Variable name

Value

7

Computer Memory

  • What is effect of _data = x1?
    • The value of 15 in cell 101 is stored in cell 107
    • What happens to the value of 13 in cell 107?
    • It is lost forever
    • What happens to the value of 15 in cell 101?
    • Nothing

107 _ data

105 nam 106 y

“CSUN”

104 rad

103 i

102 PI

101 x

100 Var

8

Computer Memory

  • What is effect of var = var + 3.5?
    • What happens to cell 100?
    • The value of 12.2 is replaced by 12.2 + 3.5 = 15.
    • Are any other cells affected?
    • No

107 _ data

105 nam 106 y

“CSUN”

104 rad

103 i

102 PI

101 x

100 Var

9

Computer Memory

  • What is effect of var = var + rad on cell 104? on cell 100? - The value of 3.5 in cell 104 does not change - The value of 12.2 in cell 100 is replaced by the result 12.2 + 3.5 = 15.

107 _ data

105 nam 106 y

“CSUN”

104 rad

103 i

102 PI

101 x

100 Var

10

Computer Memory

  • Can you do y2 = nam?
    • This is a trick question. You have no basis for answering it yet. It depends on the type for y2.
    • If y2 can hold strings the operation is okay
    • If y2 cannot hold strings this is a syntax error

107 _ data

105 nam 106 y

“CSUN”

104 rad

103 i

102 PI

101 x

100 Var

11

Data Types

  • Computer memory stores binary

information – a string of ones and zeros

  • How are these ones and zeros

interpreted? Depends on data type

  • Analogy – How do you interpret the

following string of characters?

chair

  • Depends on language 12

Integer Data Types

  • There are several of these including int ,

the only one we will use in this course

  • Integer data types have no decimal part
  • When we divide two integers the

decimal part is truncated (lost)

  • Not rounded, but truncated
  • What is 12/7?
  • What is 4/5?

19

Floating Point Data Types III

  • Type double has about 15–

significant figures of accuracy

  • Range is -1.7976931348623158x10 308 to -2.2250738585072014x10 -308^ , 0, and 2.2250738585072014x10 -308^ to 1.7976931348623158x10 308
  • For double, ε = 2.2x10 -
  • Generally use type double for modern

engineering/science/mathematics codes

20

Other Data Types

  • char data type represents single

characters (constant example: ‘a’)

  • string data type represents a string of

characters (constant example: “string”)

  • Requires #include declaration
  • bool data type used in logical

operations

  • Only two possible values for type bool variables: true and false

21

Constant Types

  • Program constants have types
    • integer 37 is type int
    • decimal 123.4 or 1.234e2 is type double
    • Character ( char ) ‘c’,
    • string “This is a string constant”
      • The only place where spaces matter
    • logical ( bool ean) true false
  • Can force constants to other types by

adding a letter (e.g. 37L is a long int)

22

Declaring Variables

  • All variables must be declared as

belonging to a specific data type the first

time they appear in the code

  • Failure to do so is a syntax error
  • Can initialize a variable with declaration

(recommended practice) or subsequent

to declaration

  • Can declare several variables in a

single statement

23

Examples

double maxAllowedError = 1e-13;

bool converged = false; int maxIterations = 100;

double radius = 10; char c = ’9’; string name = “CSUN”;

double x, y; char YES = ‘Y’, NO = ‘N’;

int x = 2, // length y = 4, // width z = 7; // height

int x; // length int y; // width int z; // height

24

Summary

  • Variables represent memory locations
  • Must declare variables before use
  • Data types for variables
    • Integer types (e.g., int) for counting
    • Floating point types (e.g., double) for calculations - Do not represent numbers exactly
    • Other types are char (individual characters), string, and bool (true and false)

25

True-False Review Quiz

  • You do not have to declare a variable

before using it in a program

  • Declaring a variable is the same as

assigning it an initial value

  • You can declare a variable and assign it

a value at the same time

  • Subtracting one from the minimum short

integer gives the maximum short

False

True

False

True

26

Assignments

  • Reading pages in text
    • Today: 45-61, 100-
    • Thursday: 64-72, 93-
    • Next Tuesday, February 14: 117-137 117-
  • Recommended homework for this week: pages 75, problems 7 and 8; page 78, problem 27
  • Exercise one may be submitted late
  • Exercise two due Thursday (9 Feb)