it 1033: fundamentals of programming data types & variables · 2015. 2. 3. · data types •...

48
IT 1033: Fundamentals of Programming Data types & variables Budditha Hettige Department of Computer Science

Upload: others

Post on 13-Sep-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

IT 1033: Fundamentals of Programming

Data types & variables

Budditha Hettige

Department of Computer Science

Page 2: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Exercise 3.1

• Write a C++ program to display the following output.

[email protected]

Page 3: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Exercise 3.2

• Write a C++ program to calculate and display total

amount of the given unit price and quantity of an

item.

[email protected]

Page 4: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Data types

Page 5: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Data Types

• Use various variables to store various information

• Variables are reserved memory locations to store

values

• When you create a variable you reserve some

space in memory

• Based on the data type of a variable, the operating

system allocates memory and decides what can be

stored in the reserved memory

[email protected]

Page 6: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

C++ Data Types

• Seven basic C++ data types

[email protected]

Page 7: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Modifiers

• Several of the basic types can be modified using

one or more of these type modifiers

– signed

– unsigned

– short

– long

[email protected]

Page 8: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Character types

char

• They can represent a single character, such as 'A' or

'$'. The most basic type is char, which is a one-byte

character.

[email protected]

Page 9: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

ASCII Code for the characters

7/11/2015 Budditha Hettige ([email protected]) 9

Page 10: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Numerical integer types

int

• They can store a whole number value, such as 7 or 1024.

[email protected]

Page 11: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Floating-point types

float

• They can represent real values, such as 3.14 or

0.01

[email protected]

Page 12: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Size of a data type

• Use sizeof() function to check the size

[email protected]

Page 13: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Answer

• Result which can vary from machine to machine/

Compilers

[email protected]

Page 14: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Data type selection

[email protected]

Page 15: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Memory allocation

[email protected]

Page 16: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Define Variables in C++

[email protected]

• Tell the compiler where and how much to create the

storage for the variable

• Data Type <space> Variable Name

– int number;

– char letter;

– float emp_salary

Page 17: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Variable name( rules)

[email protected]

• Can use only letters, digits and underscore

• The first character must be a letter or underscore

• Case sensitive

• Cannot use keywords

• No limits on length

Page 18: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

C++ Variables

• Better not to begin a variable name with underscore.

• To form a name from two or more words, separate

them with underscore

• Example

– int student_age;

– float employe_salary;

– char grade ;

[email protected]

Page 19: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Local & Global Variables

[email protected]

data_size

age

Local variable

Global variable

Page 20: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Variables in a program

[email protected]

Page 21: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Exercise 3.3

• Write a C++ program to calculate and display total

amount of the given unit price and quantity of an

item.

[email protected]

Page 22: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Answer

[email protected]

Page 23: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Constant variables

• Constants are declared like variables with

the addition of the const keyword

const double PI = 3.14159;

Once declared and initialized, a constant

can be used like a variable

• A constant may not be reassigned

7/11/2015 Budditha Hettige ([email protected]) 23

Page 24: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Type Casting

• A way to convert a variable from one data type to

another data type

• Use cast operator

– (type_name) expression

– (int) float_value

[email protected]

Page 25: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

C++ Memory concept

• Variable names correspond to location in the

computer’s memory

• Every variable has a name, a type, a size and a

value

• A memory cell is never empty. But its initial contents

may be meaningless to your program.

• The current contents of a memory cell are destroyed

whenever new information is placed in that cell.

[email protected]

Page 26: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Garbage values

7/11/2015 Budditha Hettige ([email protected]) 26

Page 27: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Input / Output

Page 28: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Standard Streams

• cin is the standard input, normally the keyboard.

• To input data from keyboard use the word ‘cin’,

followed by the ‘Extraction’ operator (>>)

– cin >> x;

• Wait for a value to be entered at the keyboard and

(when enter is pressed) will put that value into

variable ‘x’.

[email protected]

Page 29: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Exercise 3.4

• Write a C++ program to read two numbers from

keyboard and display the total.

[email protected]

Page 30: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Answer

[email protected]

Page 31: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Exercise 3.5

Create a C++ program to calculate and display total

amount of given unit price and quantity of the some

item.

[email protected]

Page 32: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Answer

[email protected]

Page 33: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Input

7/11/2015 Budditha Hettige ([email protected]) 33

Page 34: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Wait until key press

7/11/2015 Budditha Hettige ([email protected]) 34

Page 35: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Read value of the key press

7/11/2015 Budditha Hettige ([email protected]) 35

Page 36: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Change the courser position

7/11/2015 Budditha Hettige ([email protected]) 36

Page 37: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Output

• To output data onto the screen, use the word‘cout’, followed by the ‘insertion’ operator (<<).

[email protected]

Page 38: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Change the output Format

• Adjusting field widths

• Use the width() member function to set the field

width to display a value.

– cout.width(10)

[email protected]

Page 39: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Example

[email protected]

Page 40: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Floating-point notation

[email protected]

Page 41: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Example

[email protected]

Page 42: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Floating-point Precision

• Sets the decimal precision to be used to format

floating-point values on output operations.

– precision(number);

[email protected]

Page 43: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Example

[email protected]

Page 44: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Exercise 3.6

Write a C++ program to read price of the 3 items and

print the total

[email protected]

Page 45: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Answer

[email protected]

Page 46: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Examples

1. Write a C++ program to read 3 integer numbers

and find the total and average

2. Write a C++ program which will convert a weight in

KG to pounds and ounces.

( 1 Kg = 2.2046 pounds)

[email protected]

Page 47: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Example

• Write a C++ program which will convert a Celsius

temperature into Fahrenheit

[email protected]

Page 48: IT 1033: Fundamentals of Programming Data types & variables · 2015. 2. 3. · Data Types • Use various variables to store various information • Variables are reserved memory

Summary

• Data types (char, int, float, long, double, bool)

• Variables (local , global and constant)

• Input fro keyboard (cin >>)

• Output (cout <<)

• Format your output

– setwidth(x)

– cout.setf()

– cout.precision(x)

7/11/2015 Budditha Hettige ([email protected]) 48