Computer Programming: Lab 3 and 4 Lab Instructor: Junaid Rashid
Computer Programming: Lab 3 and 4 Lab Instructor: Junaid Rashid
Lab 3 and 4
Lab Instructor: Junaid Rashid
Bits & Bytes
A bit is a single numeric value either ‘1’or ‘0. that
encodes a single unit of digital information.
A byte is a sequence of bits.
8 bits=1 byte.
Data Types
A computer program operates on data and produces an
output.
In C++, each data must be of specific data type.
The data type determines how the data is represented in the
computer and kind of processing the computer can perform
on it.
Types of data
integer (int)
Float (float)
Boolean
Characters (char)
3
Integers (int)
integers are whole numbers with a range of values.
used to store numbers
Example: 5, 6, 100, 2500
4
Integers (int)……… continued
If int is unsigned then the range will be 0 to 4,294,967,295
Positive integers do not need a + sign in front of them
No commas are used within an integer. Recall that in C++,
commas are used to separate items in a list. So 36, 782
would be interpreted as two integers: 36 and 782.
Float
used to represent floating point numbers.
6
Character
Character types can hold a single character.
7
ASCII Coding System
American Standard Code for Information Interchange
(ASCII)
Originally designed as a 7-bit code
purpose was to standardize a binary code to give the
computer user the capability of using several
machines to process data regardless of the
manufacturer for transmitting and processing data
8-bit version of ASCII
boolean
Boolean or Flag type is a type which can represent only
two values: 0 and 1, usually identified with false and true
respectively.
main()
{
bool x=true; //or x=false
}
10
Variables
A storage box, its type defines the kind of stuff you store
in it
11
Variable
Variable is a box
In computer, a storage box is a memory location on RAM
12
Memory in Computer
First you ask computer to give you a Each little
box and you also have to specify what box is a
type of box you want memory
location
Occupied Free
Memory Memory
Spaces Spaces
13
Rules for Variable Naming
Identifier can be as long as you like but only the first 250
character are recognizable in C++ Compiler.
Syntax Rule to declare a variable is
Datatype identifier;
For example int result;
Integer Variables Example
#include<iostream>
using namespace std;
int main ( )
{
int var1; //define var1
int var2; //define var2
var1 = 20; //assign value to var1
var2 = var1 + 10; //assign value to var2
cout<<“Result =”;
cout<<var2<< endl; //displaying the sum of var1 + 10
return 0;
} Output:
Result=30
Character Variable Example
#include<iostream>
using namespace std;
int main ( )
{
char charvar1 = ‘A’; //define char variable
char charvar2 = ‘\t’;
cout << charvar1; // display character
cout << charvar2;
charvar1 = ‘B’; //reassigning charvar1 to B
cout << charvar1;
return 0;
} Output:
A B
Float Variable Example
#include<iostream>
using namespace std;
int main ( )
{
float radius=2;
float Pi =3.14;
Area is 12.56
Constant
values used in a program that are not changed during the
course of program.
e.g.
Circumference=2*pi*r.
area=pi*r*r
18
Defining constant
main()
{
const int i = 5;
i = 10; //error, can not modify constant
i++; // error, can not modify constant
}
Basic Input in c++
cin with insertion operators >>.
21
Activity 1
Shorthand:
a=a+b a+=b
a=a-b a-=b
a=a*b a*=b
a=a/b a/=b
24
a=a%b a%=b
Operator precedence
()
* / %
+ -
25
Activity 3
What prints when each of the following c++ statement
is executed? Assume x=2 and y=3.
1. cout<<x;
2. cout<<x+x;
3. cout<<“x=“;
4. cout<<“x=“<<x;
5. cout<<x+y<<“=“<<y+x;
Activity 4
Write a program that calculates the square of a number
entered by the user and displays output as below.
Value Square
4 16
27
Evaluate the following expressions
Integer and Float Conversion
• rules that are used for implicit conversion of floating point
and integer values in C++ are;