0% found this document useful (0 votes)
12 views35 pages

3-Lecture-FoP-number Systems and Variables

Number system and variables

Uploaded by

bilalasim1020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views35 pages

3-Lecture-FoP-number Systems and Variables

Number system and variables

Uploaded by

bilalasim1020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

CS-114 Fundamentals of Programming

Number systems & Variables

Dr Ayesha Zeb
Email: [email protected]
Lecture Contents

• Number systems
• Variables and memory
• Types of variables
Review – A Basic Program
//EC 100 Algorithms and Computing
//A first program
//Hello World – displays “Hello
World!”
//on the screen

#include <iostream>

using namespace std;

int main()
{
cout << "Hello, World!";
return 0;
}
Review – A Basic Program with Input/Output

//EC 100 Algorithms and string name;


Computing cout << "Please
//Simple Input Output enter your name" <<
//Modifying our program endl;
cin >> name;
#include <iostream> cout << "\n Hello
#include <string> "<<name<<endl;
return 0;
using namespace std; }

int main()
{
How are things stored in a computer?

• Remember: Computers – lots of small on, off switches


• These on off switches are called transistors
• Transistors are electronic devices
• On (1) or Off (0) , High and Low Voltages

Source: Mark Redekopp’s slides


Number Systems

• Humans use the decimal number system:


– Based on number 10
– 10 digits: [0-9]
• Computers use the binary number system:
– Based on number 2
– 2 digits: [0,1]
• Other number systems:
– Octal (base 8)
– Hexadecimal (base 16)
Bits and Bytes

• Computers use the binary number system


• A 1 or 0 is called a bit
• 8 bits make a byte

0 0 0 0 0 0 1 0
8 bits = 1 byte
Representing information in computers
string name;
cout << "Please enter your name" <<
endl;
cin >> name;
cout << "\n Hello "<<name<<endl;

Primary
Memory
Loader
Loader puts program
in memory.
Disk ..
[name here]
..
..

Primary
Memory CPU takes each
CPU instruction and
executes it, possibly
storing new data
..

john ..
..
values as the program
executes.
Representing Text

• Text characters are usually represented with some kind


of binary code mapping
• ‘a’ = 01100001 = 0x61
• ASCII (American Standard Code for Information
Interchange)
– an 8-bit code to represent text
• Unicode – a 16-bit code
• String – sequence of characters
Dec Hex Char Dec Hex Char Dec Hex Char Dec Hex Char Dec Hex Char Dec Hex Char Dec Hex Char Dec Hex Char
0 00 32 20 64 40 @ 96 60 ` 128 80 € 160 A0 192 C0 À 224 E0 à
1 01 33 21 ! 65 41 A 97 61 a 129 81 161 A1 ¡ 193 C1 Á 225 E1 á
2 02 34 22 " 66 42 B 98 62 b 130 82 ‚ 162 A2 ¢ 194 C2 Â 226 E2 â
3 03 35 23 # 67 43 C 99 63 c 131 83 ƒ 163 A3 £ 195 C3 Ã 227 E3 ã
4 04 36 24 $ 68 44 D 100 64 d 132 84 „ 164 A4 ¤ 196 C4 Ä 228 E4 ä
5 05 37 25 % 69 45 E 101 65 e 133 85 … 165 A5 ¥ 197 C5 Å 229 E5 å
6 06 38 26 & 70 46 F 102 66 f 134 86 † 166 A6 ¦ 198 C6 Æ 230 E6 æ
7 07 39 27 ' 71 47 G 103 67 g 135 87 ‡ 167 A7 § 199 C7 Ç 231 E7 ç
8 08 40 28 ( 72 48 H 104 68 h 136 88 ˆ 168 A8 ¨ 200 C8 È 232 E8 è
9 09 41 29 ) 73 49 I 105 69 i 137 89 ‰ 169 A9 © 201 C9 É 233 E9 é
10 0A 42 2A * 74 4A J 106 6A j 138 8A Š 170 AA ª 202 CA Ê 234 EA ê
11 0B 43 2B + 75 4B K 107 6B k 139 8B ‹ 171 AB « 203 CB Ë 235 EB ë
12 0C 44 2C , 76 4C L 108 6C l 140 8C Œ 172 AC ¬ 204 CC Ì 236 EC ì
13 0D 45 2D - 77 4D M 109 6D m 141 8D 173 AD - 205 CD Í 237 ED í
14 0E 46 2E . 78 4E N 110 6E n 142 8E Ž 174 AE ® 206 CE Î 238 EE î
15 0F 47 2F / 79 4F O 111 6F o 143 8F 175 AF ¯ 207 CF Ï 239 EF ï
16 10 48 30 0 80 50 P 112 70 p 144 90 176 B0 ° 208 D0 Ð 240 F0 ð
17 11 49 31 1 81 51 Q 113 71 q 145 91 ‘ 177 B1 ± 209 D1 Ñ 241 F1 ñ
18 12 50 32 2 82 52 R 114 72 r 146 92 ’ 178 B2 ² 210 D2 Ò 242 F2 ò
19 13 51 33 3 83 53 S 115 73 s 147 93 “ 179 B3 ³ 211 D3 Ó 243 F3 ó
20 14 52 34 4 84 54 T 116 74 t 148 94 ” 180 B4 ´ 212 D4 Ô 244 F4 ô
21 15 53 35 5 85 55 U 117 75 u 149 95 • 181 B5 µ 213 D5 Õ 245 F5 õ
22 16 54 36 6 86 56 V 118 76 v 150 96 – 182 B6 ¶ 214 D6 Ö 246 F6 ö
23 17 55 37 7 87 57 W 119 77 w 151 97 — 183 B7 · 215 D7 × 247 F7 ÷
24 18 56 38 8 88 58 X 120 78 x 152 98 ˜ 184 B8 ¸ 216 D8 Ø 248 F8 ø
25 19 57 39 9 89 59 Y 121 79 y 153 99 ™ 185 B9 ¹ 217 D9 Ù 249 F9 ù
26 1A 58 3A : 90 5A Z 122 7A z 154 9A š 186 BA º 218 DA Ú 250 FA ú
27 1B 59 3B ; 91 5B [ 123 7B { 155 9B › 187 BB » 219 DB Û 251 FB û
28 1C 60 3C < 92 5C \ 124 7C | 156 9C œ 188 BC ¼ 220 DC Ü 252 FC ü
29 1D 61 3D = 93 5D ] 125 7D } 157 9D 189 BD ½ 221 DD Ý 253 FD ý
30 1E 62 3E > 94 5E ^ 126 7E ~ 158 9E ž 190 BE ¾ 222 DE Þ 254 FE þ
31 1F 63 3F ? 95 5F _ 127 7F • 159 9F Ÿ 191 BF ¿ 223 DF ß 255 FF ÿ
Representing Different Types of information
Type Name Bytes Other Names Range of Values
int 4 signed –2,147,483,648 to 2,147,483,647
unsigned int 4 unsigned 0 to 4,294,967,295
bool 1 none false or true
char 1 none –128 to 127 by default
signed char 1 none –128 to 127
unsigned char 1 none 0 to 255
short 2 short int, signed short int –32,768 to 32,767
unsigned short 2 unsigned short int 0 to 65,535
long 4 long int, signed long int –2,147,483,648 to 2,147,483,647
unsigned long 4 unsigned long int 0 to 4,294,967,295
long long 8 none –9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
unsigned long long 8 none 0 to 18,446,744,073,709,551,615
38 38
float 4 none -1.0x10 to 1.0x10 (7 digits)
308 308
double 8 none -1.0x10 to 1.0x10 (15 digits)
Representing Different Types of Information

• Information – Data
• Two ways to represent numerical data
– signed (default)
– unsigned
• Signed data will have its sign (positive or negative) for its
MSB (most significant bit)
– Will this effect its range?
Representing Different Types of Information

• Information – Data
• Two ways to represent numerical data
– signed (default)
– unsigned
• Signed data will have its sign (positive or negative) for its
MSB (most significant bit)
– Its range will be reduced by half!
Representing Different Types of Information

• Information – Data
• Two ways to represent numerical data
– signed (default)
– unsigned
• Signed data will have its sign (positive or negative) for its
MSB (most significant bit)
– Its range will be reduced by half!
• Similarly unsigned data will not have a sign (it will always
be positive)
– Its range will be twice as much of signed!
int main()
Variables {
int x = 1564983;
char c;
string name;
• Programs operate on cout << "Please enter
your name" << endl;
and produce data values ………
• Data values are stored }
in memory/RAM char c;
0 11010010
• ‘Variables’ are memory A single byte
variable 1 01001011
locations reserved to
2 10010000
store a piece of data of
3 11101010
specific size and type
int x = 4 01101000
1564983; 5 11010001
A four-byte 6 01101000
variable 7 11010001
8 00001110
… …..
1024 10101010
Variables

• Used to store information/data


• Data can be taken from the user
– cin>> name;
• Data can be decided by the programmer
– name = “Hello World!”;
Variables

• Variables have:
– Location – address in memory where they are located/stored
– Type
– Name/identifier – what the programmer uses to access that
memory location
• C++ variables are ‘typed’
– You must declare (tell the compiler that the variable exists)
before you use it
– Indicated what kind of information you will be storing
• int x = 5;
• float y = 0.5;
• int z = 2.3;
Variable Declaration

• A variable declaration
– Defines the name of the variable
– Defines the type of the variable (int, float, etc.)
– Gives the programmer a description of the variable
• Declaration format:
– type name; //comment

– Examples:
– int sum; //stores ‘sum’ in int type variable
– float answer; //stores ‘answer’ in float type variable
– char something;
– string name
Naming Variables

• A name/identifier in a C++ program


– Starts with a letter, contains letters, digits, and underscores
(only)
• x, number_of_elements, Fourier_transform, z2
• Not names:
– 12x
– time$to$market
– main line
• Do not start names with underscores: _foo
– those are reserved for implementation and systems entities
– Users can't define names that are taken as keywords
• E.g.:
– int
– if
– while
– double
Naming Variables

• Choose meaningful names


– Abbreviations and acronyms can confuse people
• mtbf, TLA, myw, nbv
– Short names can be meaningful
• (only) when used conventionally:
– x is a local variable
– i is a loop index
– Don't use overly long names
• Ok:
– partial_sum
element_count
staple_partition
• Too long:
– the_number_of_elements
remaining_free_slots_in_the_symbol_table
Variable Declaration

• Once the variable has been declared, it can be used in


assignment statements
• Sum = 2457 + 5363;
• Answer = 2.456+3.2;

– Note: Variables are case sensitive


int main()
Variable Initialization {
int x = 1564983;
char c;
• Initialization: string name;
assigning/giving a value cout << "Please enter
your name" << endl;
to the variable for the ………
first time }

• Good to initialize char c; 0 11010010


variables A single byte variable, 1 01001011
not initialized, could be
• Garbage otherwise anything
2 10010000
3 11101010
4 01101000
int x = 1564983;
5 11010001
A four-byte variable
6 01101000
7 11010001
8 00001110
… …..
1024 10101010
Declaration and initialization
int a = 7;

int b = 9;
a: 7
char c = 'a';
b: 9
char a;
c: 'a'
double x = 1.2;
a: ### garbage
string s1 = "Hello, world";
x: 1.2

string s2 = "1.2";
s1 12 | "Hello, world"
:
s2: 3 | "1.2"
A Simple Program Using Variables
1 // Fig. 1.6: fig01_06.cpp Enter first integer
2 // Addition program. 45
3 #include <iostream> Enter second
4 integer
5 // function main begins program execution 72
6 int main() Sum is 117
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 std::cout << "Enter first integer\n"; // prompt
13 std::cin >> integer1; // read an integer
14
15 std::cout << "Enter second integer\n"; // prompt
16 std::cin >> integer2; // read an integer
17
18 sum = integer1 + integer2; // assign result to sum
19
20 std::cout << "Sum is " << sum << std::endl; // print sum
21
22 return 0; // indicate that program ended successfully
23
24 } // end function main © 2003 Prentice Hall, Inc.
All rights reserved.
Variables and Memory

std::cin >> integer1;


integer1 45
– Assume user entered 45

std::cin >> integer2; integer1 45


integer2 72
– Assume user entered 72

integer1 45
sum = integer1 + integer2; integer2 72
sum 117

2/10/2024
Variables and Memory

• Variable names such as a and sum actually correspond


to locations in the computer's memory.
• When a variable is declared, the compiler allocates a
memory location to that variable. Any further operation
on that variable takes place in that memory location.
• Every variable has a name, a type, a size and a value.
For example
int num1;
cin >> num1; // read first integer from user into num1
the characters typed by the user are converted to an integer that will
be placed into a memory location to which the variable number1 has
been assigned by the C++ compiler.
Memory

• Destructive Process
– Whenever a value is placed in a memory location it overwrites
the previous value in that location placing a new value into that
memory location.

• Nondestructive Process
– Whenever a value is read from a memory location, the value of
that variable in that memory location remains unchanged.
Memory

• For example
sum = num1 + num2;
// add the nums and store result in sum
– The values of ‘num1’ and ‘num2’ will be read out and used in the
calculation of sum. So it will be a nondestructive process.
– However, the result of the addition process will be stored in the
variable ‘sum’. So it will be a destructive process.
• Thus, it is always best to declare a variable as null or
zero.
When to use variables

• Saving values
• Reusing values
• Getting values from the user/at runtime
• Will the values change ?
• Making code more readable
Which Type?

Constant Type Right/Wrong


4.0 int
5 int
‘a’ string
“abc” string
5. double
5 char
“5.0” double
‘5’ int

Source: Mark Redekopp’s slides


Which Type?

Constant Type Right/Wrong


4.0 int double (.0)
5 int int
‘a’ string char
“abc” string string (char* or char[])
5. double float/double
5 char int but if you store 5 in
a char its okay
“5.0” double string
‘5’ int char
Summary

• By the end of this lecture, you should know


– how to write a basic program, declare variable and input/output
statements.
– the different number systems and their inter-conversions.
– The basic concept of what memory is and how it is used.
Acknowledgement/References
• Slides contents taken from Ma’am Fatima Faruq

• Deital and Deital, “C++ How to Program”, Latest Edition

• Stroustrup, “Programming – Principles and Practice


Using C++”, Latest Edition

You might also like