Professional Documents
Culture Documents
LECTURE
2 Programming Basics
CSC 113
Computer Programming
Spring 2023
Abrar Ahmed
abrarahmed.buic@bahria.edu.pk
Outline
1
2/28/2023
Decimal-to-Binary
Conversion
Binary-to-Decimal
Conversion
0 1 2 3 4
5 6 7 8 9
CSC 113 – Computer Programming
Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 4
2
2/28/2023
100
101
19
18
17
16
15
99
98
97
96
95
14
13
12
11
10
24
23
22
21
20
9
8
7
6
5
4
3
2
1
0
Ran out of symbols (0-9), so increment the digit on the left by one unit.
01
CSC 113 – Computer Programming
Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 6
3
2/28/2023
10000
1111
1110
1101
1100
1011
1010
1001
1000
111
101
100
110
11
10
1
0
CSC 113 – Computer Programming
Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 7
4
2/28/2023
Binary Numbers
• Bit Numbering 2 7 6
2 5
2 2 4 3
2 2 2
2 1
20
Binary Numbers
10
5
2/28/2023
Decimal Binary
conversion
11
Convert 75 to Binary
2 75 remainder
2 37 1
2 18 1
2 9 0
2 4 1
2 2 0
1 0
12
6
2/28/2023
13
Check
= 1 + 2 + 0 + 8 + 0 + 0 + 64
= 75
14
7
2/28/2023
2 100 remainder
2 50 0
2 25 0
2 12 1
2 6 0
2 3 0
1 1
15
a) 1310 = ?
b) 2210 = ?
c) 4310 = ?
d) 15810 = ?
16
8
2/28/2023
a) 1310 = ? 11012
b) 2210 = ? 101102
c) 4310 = ? 1010112
d) 15810 = ? 100111102
17
Binary Decimal
conversion
18
9
2/28/2023
7 6 5 4 3 2 1 0
1 0 0 1 1 1 0 1
7 6 5 4 3 2 1
2 2 2 2 2 2 2 20
19
1 0 1 0 1 1 0 0
128 64 32 16 8 4 2 1
128 + 0 + 32 + 0 + 8 + 4 + 0 + 0
128 + 32 + 8 + 4 = 172
CSC 113 – Computer Programming
Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 20
20
10
2/28/2023
0 1 0 1 0 0 0 1
128 64 32 16 8 4 2 1
0 + 64 + 0 + 16 + 0 + 0 + 0 + 1
64 + 16 + 1 = 81
CSC 113 – Computer Programming
Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 21
21
- - - ✓ - ✓ ✓ ✓
128 64 32 16 8 4 2 1
0 + 0 + 0 + 16 + 0 + 4 + 2 + 1
16 + 4 + 2 + 1 = 23
CSC 113 – Computer Programming
Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 22
22
11
2/28/2023
☺ ☺ ☺ ☺ ☺ ☺
128 64 32 16 8 4 2 1
128 + 0 + 32 + 16 + 0 + 4 + 2 + 1
128 + 32 + 16 + 4 + 2 + 1 = 183
CSC 113 – Computer Programming
Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 23
23
a) 0110 2 = ?
b) 11010 2 = ?
c) 0110101 2 = ?
d) 11010011 2 = ?
24
12
2/28/2023
a) 0110 2 = ? 6 10
b) 11010 2 = ? 26 10
c) 0110101 2 = ? 53 10
d) 11010011 2 = ? 211 10
25
Representation of
Binary Numbers
in Memory
CSC 113 – Computer Programming
Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 26
26
13
2/28/2023
Nibble
• 4 bits form a Nibble
3 2 1 0
• “1011” is One Nibble of Information 1 1 0 1 = 13
23 22 21 20
• Nibble Values: 3 2 1 0
• 0000 0 0 0 0 =0
3 2 1
2 2 2 20
3 2 1 0
• 1111 1 1 1 1 = 15
23 22 21 20
27
• Byte Values: 7 6 5 4 3 2 1 0
• 00000000 0 0 0 0 0 0 0 0 =0
27 26 25 24 23 22 21 20
7 6 5 4 3 2 1 0
• 11111111 1 1 1 1 1 1 1 1 = 255
27 26 25 24 23 22 21 20
28
14
2/28/2023
• 2 Bytes Value: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
• 0000000000000000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 =0
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 20
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
• 1111111111111111 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 = 65,535
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 20
29
3 Bytes
• 24 bits form 3 bytes
• “000000000001110110011101” is Three Byte of Information
23 22 . . . . . 8 7 6 5 4 3 2 1 0
0 0 0 1 1 1 0 1 1 0 0 1 1 1 0 1 = 7,581
23 22 . . . . . 8 7 6 5 4 3 2 1
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 20
• 3 Byte Values:
• 000000000000000000000000
23 22 . . . . . 8 7 6 5 4 3 2 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 =0
223 222 2. 2. 2. 2. 2. 28 27 26 25 24 23 22 21 20
• 111111111111111111111111
23 22 . . . . . 8 7 6 5 4 3 2 1 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 = 16,777,216
223 222 2. 2. 2. 2. 2. 28 27 26 25 24 23 22 21 20
= 16.77 Millions
CSC 113 – Computer Programming
Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 30
30
15
2/28/2023
• 11111111111111111111111111111111
31 30 . . . . . 8 7 6 5 4 3 2 1 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 = 4,294,967,296
231 230 2. 2. 2. 2. 2. 28 27 26 25 24 23 22 21 20
= 4.29 Billions
CSC 113 – Computer Programming
Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 31
31
• 11111111111111…………111111111111111111
63 62 . . . . . 8 7 6 5 4 3 2 1 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 = 18,446,744,073,709,551,616
263 262 2. 2. 2. 2. 2. 28 27 26 25 24 23 22 21 20
=?
CSC 113 – Computer Programming
Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 32
32
16
2/28/2023
Binary
Representation of
Signed Numbers
CSC 113 – Computer Programming
Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 33
33
• Minimum value
6 5 4 3 2 1 0
1 1 1 1 1 1 1 1 = -128
26 25 24 23 22 21 20
• Maximum value
6 5 4 3 2 1 0
0 1 1 1 1 1 1 1 = +127
6 5 4 3 2 1
2 2 2 2 2 2 20
34
17
2/28/2023
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* Wirte a program that takes a value
from user as his/her
Fixed Part of
confusion level out of 100 the Program
and prints his/her confidence level*/
int confusionLevel;
cout << "Enter your confusion level out of 100% : ";
cin >> confusionLevel;
cout << "Dont worry, you are " << 100 - confusionLevel << "% confident";
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
_getch();
return 0;
}
CSC 113 – Computer Programming
Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 35
35
Back to C++
//Preprocessor Directives
#include<iostream>
#include<conio.h>
using namespace std;
//-----------------------------------
// Main Function
int main()
{
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* Wirte a program that takes a value
from user as his/her
Data Type
confusion level out of 100
and prints his/her confidence level*/
int confusionLevel;
Memory is
cout << "Enter your confusion level out of 100% : "; Requested from
cin >> confusionLevel;
cout << "Dont worry, you are " << 100 - confusionLevel << "% confident";
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
_getch();
return 0;
}
CSC 113 – Computer Programming
Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 36
36
18
2/28/2023
Variables
37
What is a Variable?
• A variable is a memory address where data can be stored and changed.
• Declaring a variable means specifying both its name and its data type.
38
19
2/28/2023
Declaration
• Declarations
• Variable Declarations
• Constant Declarations
• Variable Declarations
• Syntax
<type> <identifier> = <expression>;
• Example
int confidenceLevel = 100;
39
Declaration
• Constant Declarations
• Syntax
const <type> <identifier> = <expression>;
• Example
• const double PI = 3.1459;
40
20
2/28/2023
Declaration
• Variable Declarations
• Variables are used to store values that can be changed during the program execution
• Syntax:
< type > < identifier >;
< type > < identifier > = < expression>;
• Examples:
int sum;
int total = 3445;
char answer = 'y';
double temperature = -3.14
41
Declarations
• A variable has a type and it can contain only values of that type. For example, a variable of the type int can
only hold integer values
• Thus, it is good practice to initialize variables when they are declared. Once a value has been placed in a
variable it stays there until the program deliberately alters it.
42
21
2/28/2023
Declarations
43
Variable Declaration
• All variables must declared before use.
• At the top of the program
• Just before use.
• Commas are used to separate identifiers of the same type.
int count, age;
or
int count;
int age;
• Variables can be initialized to a starting value when they are declared
int count = 0;
int age;
44
22
2/28/2023
45
Assignment Operator
• An operator to give (assign) a value to a variable.
• Denote as ‘=‘
• Only variable can be on the left side.
• An expression is on the right side.
• Variables keep their assigned values until changed by another assignment statement or by reading in a new
value.
46
23
2/28/2023
NOTE: An automatic type coercion occurs after evaluation but before the value is stored if the types differ for
Expression and Variable
47
Variables in C++
48
24
2/28/2023
49
50
25
2/28/2023
Back to C++
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
cout << "Size of char : \t\t\t" << sizeof(char)
<< " byte" << endl;
cout << "Size of int : \t\t\t" << sizeof(int)
<< " bytes" << endl;
cout << "Size of short int : \t\t" << sizeof(short int)
<< " bytes" << endl;
cout << "Size of long int : \t\t" << sizeof(long int)
<< " bytes" << endl;
cout << "Size of signed long int : \t" << sizeof(signed long int)
<< " bytes" << endl;
cout << "Size of unsigned long int:\t " << sizeof(unsigned long int)
<< " bytes" << endl;
cout << "Size of float : \t\t" << sizeof(float)
<< " bytes" << endl;
cout << "Size of double : \t\t" << sizeof(double)
<< " bytes" << endl;
cout << "Size of long double : \t\t" << sizeof(long double)
<< " bytes" << endl;
cout << "Size of bool : \t\t\t" << sizeof(bool)
<< " bytes" << endl;
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
51
52
26
2/28/2023
53
54
27
2/28/2023
7 6 5 4 3 2 1 0
• Byte Values:
0 0 0 0 0 0 0 0 =0
• 00000000 27 26 25 24 23 22 21 20
7 6 5 4 3 2 1 0
1 1 1 1 1 1 1 1 = 255
• 11111111
27 26 25 24 23 22 21 20
55
56
28
2/28/2023
Identifiers
• Series of Characters (letters, digits, underscores)
• Must NOT start with a digit (0 – 9)
• Must not be a C++ keyword
• Case Sensitive
• Exercise
• Which of these are valid identifiers
• floating
• int
• Int
• main3
• 4yi
57
• Lvalues
• Expressions that can appear on left side of equation
• Can be changed (i.e., variables)
• x = 4;
• Rvalues
• Only appear on right side of equation
• Constants, such as numbers
• Cannot write 4 = x;
58
29
2/28/2023
Input value
• Input Source?
• Console
• Data File
• Console Input
• cin >>
• In the header file “iostream”
59
1 // Example
2 // Addition program.
3 #include <iostream>
4 using namespace std;
5 // function main begins program execution
6 void main()
7 {
8 int integer1; // first number to be input by user
9 int integer2; // second number to be input by user
10 int sum; // variable in which sum will be stored
11
12 cout << "Enter first integer:\n"; // prompt
13 cin >> integer1; // read an integer
14
15 cout << "Enter second integer:\n"; // prompt
16 cin >> integer2; // read an integer
17
18 sum = integer1 + integer2; // assign result to sum
19
20 cout << "Sum is " << sum << endl; // print sum
21
22
23
24 } // end function main
CSC 113 – Computer Programming
Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 60
60
30
2/28/2023
61
62
31
2/28/2023
Portability Tip
63
Arithmetic operators
• Arithmetic calculations
•*
Multiplication
•/
Division
Integer division truncates remainder
7 / 5 evaluates to 1
•%
Modulus operator returns remainder
7 % 5 evaluates to 2
• + and –
Addition and Subtraction
64
32
2/28/2023
Arithmetic operators
65
Arithmetic operators
• Priority of operators
• a = 5 + 7 % 2;
• we may doubt if it really means:
• a = 5 + (7 % 2) with result 6 or
• a = (5 + 7) % 2 with result 0
• Parentheses are included when one is not sure
66
33
2/28/2023
Arithmetic operators
a + b - c + d
a * b / c
1 + a * b % c
a + d % b - c
e = b = d + c / b - a
67
Arithmetic operators
• Arithmetic Assignment Operators
• a = a + b;
• a += b;
int main()
{
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
int number = 15;
number += 10;
cout << number << endl;
number -= 7; 25
cout << number << endl;
number *= 2; 18
cout << number << endl;
number %= 2; 36
cout << number << endl;
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0
_getch();
return 0;
}
68
34
2/28/2023
Arithmetic operators
• Increment Operators (Unary Operator)
◼ count = count + 1;
◼ count +=1;
◼ count++; OR ++count;
int a = 5; int a = 5;
int b = 10; int b = 10;
int c = a * b++; int c = a * ++b;
50 55
69
Arithmetic operators
• Decrement Operators (Unary Operator)
◼ count = count - 1;
◼ count -=1;
◼ count--; OR --count;
postfix prefix
70
35
2/28/2023
Arithmetic operators
void main()
{
int count = 10;
71
int a = 1;
int b = a++ + ++a;
cout << a;
cout << endl;
cout << b;
CSC 113 – Computer Programming
Department of Computer Sciences
2. Programming Basics
Bahria University, Islamabad
Slide: 72
72
36
2/28/2023
Relational operators
• To evaluate comparison between two expressions
• Result : True / False
Equality operators
= == x == y x is equal to y
!= x != y x is not equal to y
73
Relational operators
• Examples
▪ (7 == 5) would return false
▪ (3 != 2) would return true
▪ (6 >= 6) would return true
• If a=2, b=3 and c=6
▪ (a*b >= c) would return true since it is (2*3 >= 6)
▪ (b+4 > a*c) would return false since it is (3+4 > 2*6)
▪ ((b=2) == a) would return true
74
37
2/28/2023
• During comparison alphabetical order is followed. (ASCII: American Standard Code for Information
Interchange ).
• Examples
• ‘a’ < ‘e’ // True
75
76
38
2/28/2023
Logical operators
• Logical expressions - expressions that use conditional statements and logical operators.
• && (And)
• A && B is true if and only if both A and B are true
• || (Or)
• A || B is true if either A or B are true
• ! (Not)
• !(condition) is true if condition is false, and false if condition is true
• This is called the logical complement or negation
• Examples
• (salary < 10000) || (dependants > 5)
• (temperature > 40.0) && (humidity > 90)
• !(temperature > 90.0)
77
78
39
2/28/2023
79
80
40
2/28/2023
81
Remember
• && operator yields a true result only when both its operands are true.
• || operator yields a false result only when both its operands are false.
82
41
2/28/2023
Conditional operators
• condition ? result1 : result2
▪ if condition is true the expression will return result1, if not it will return result2
• Examples
▪ 7==5 ? 4 : 3 returns 3 since 7 is not equal to 5.
▪ 7==5+2 ? 4 : 3 returns 4 since 7 is equal to 5+2
▪ a>b ? a : b returns the greater one, a or b
83
Bitwise Operators
• Bitwise Operators ( &, |, ^, ~, <<, >> )
84
42
2/28/2023
0 0 1 0 1 0
1 0 1 0 0 0
85
XOR
86
43
2/28/2023
Type conversion
void main(void)
{
int number = 2;
float factor = 1.5;
double result = number * factor;
cout << "Result is : " << result;
}
void main(void)
{
short x = 2;
int y = x;
}
87
Type conversion
Casting
void main(void)
{
short number = 30000;//-32768 to 32767
short result = (number * 10) / 10;
cout << "Result is : " << result;//Result Incorrect
number = 30000;
result = (long(number) * 10) / 10; //Casting
cout << "Result is : " << result;
}
88
44
2/28/2023
Operator precedence
89
Example
int x = 1, y = 2;
int result = y++ + ++x;
cout << result << endl;
cout << x << endl;
cout << y << endl;
90
45
2/28/2023
Escape Sequences
1. \n (New line) – We use it to shift the cursor control to the new line
2. \t (Horizontal tab) – We use it to shift the cursor to a couple of spaces to the right in the same line.
3. \a (Audible bell) – A beep is generated indicating the execution of the program to alert the user.
4. \r (Carriage Return) – We use it to position the cursor to the beginning of the current line.
5. \\ (Backslash) – We use it to display the backslash character.
6. \’ (Apostrophe or single quotation mark) – We use it to display the single-quotation mark.
7. \” (Double quotation mark)- We use it to display the double-quotation mark.
8. \0 (Null character) – We use it to represent the termination of the string.
9. \? (Question mark) – We use it to display the question mark. (?)
91
• Evaluate
• Y = 2*5*5+3*5+7;
92
46
2/28/2023
93
Activity 2
94
47
2/28/2023
Activity 3
95
Activity 4
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;
96
48
2/28/2023
Activity 5
• Write a program that calculates the square of a number entered by the user and displays output as below.
Value Square
4 16
97
Activity 6
• What would be the output
int i=6, j=2;
cout<<i<<endl;
cout<<i++<<endl;
cout<<++i<<endl;
cout<<j++<<endl;
cout<<++i-j<<endl;
cout<<i-j--<<endl;
98
49
2/28/2023
Output
6
6
8
2
6
6
99
100
50