0% found this document useful (0 votes)
9 views

03-data-types-and-variables

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

03-data-types-and-variables

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 48

CS-1101: Fundamentals of Programming

Data types & variables

Department of Computer Science &IT


Exercise 3.1
• Write a C++ program to display the following output.
Exercise 3.2
• Write a C++ program to calculate and display total
amount of the given unit price and quantity of
an item.
Data types
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
C++ Data Types
• Seven basic C++ data types
Modifiers
• Several of the basic types can be modified
using one or more of these type modifiers
– signed
– unsigned
– short
– long
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.
ASCII Code for the characters

9
Numerical integer types
int
• They can store a whole number value, such as 7 or 1024.
Floating-point types
float
• They can represent real values, such as 3.14
or 0.01
Size of a data type
• Use sizeof() function to check the size
Answer
• Result which can vary from machine to machine/
Compilers
Data type selection
Memory allocation
Define Variables in C++
• 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
Variable name( rules)
• 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
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 ;
Local & Global Variables
Global variable

data_size

age

Local variable
Variables in a program
Exercise 3.3
• Write a C++ program to calculate and display total
amount of the given unit price and quantity of
an item.
Answer
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

23
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
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.
Garbage values
Input / Output
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’.
Exercise 3.4
• Write a C++ program to read two numbers from
keyboard and display the total.
Answer
Exercise 3.5
Create a C++ program to calculate and display total
amount of given unit price and quantity of the some
item.
Answer
Input

33
Wait until key press

34
Read value of the key press

35
Change the courser position

36
Output
• To output data onto the screen, use the word
‘cout’, followed by the ‘insertion’ operator
(<<).
Change the output Format
• Adjusting field widths
• Use the width() member function to set the field
width to display a value.
– cout.width(10)
Example
Floating-point notation
Example
Floating-point Precision
• Sets the decimal precision to be used to format
floating-point values on output operations.
– precision(number);
Example
Exercise 3.6
Write a C++ program to read price of the 3 items and
print the total
Answer
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)
Example
• Write a C++ program which will convert a Celsius
temperature into Fahrenheit
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)

You might also like