0% found this document useful (0 votes)
3 views49 pages

Lecture 2

Uploaded by

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

Lecture 2

Uploaded by

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

Glory be to you, we have no knowledge except what you have taught us.

Verily, it is You, the All Knower, the All-Wise.


Q[2:32]
Programing
Fundamentals
Three steps before writing Code
• Write algorithm
• Design the flowchart
• Write the program/code
What is a computer Program
What is a Algorithm and Pseudo Code
What is a Flowchart
Symbols of Flowchart
• There are four types of symbols in a flowchart:
– Ovals (terminal symbols)
The Start and End terminal symbols mark the program’s
starting and ending points respectively
– Parallelograms
Input and output symbols
– Rectangles (processing symbols)
Represent a step in the program.
– Diamonds (decision symbols)
We will discuss them later…
– The symbols are connected by arrows
Arrows represent the “flow” of the program. To step through
the symbols in the proper order, you begin at the Start terminal
and follow the arrows until you reach the End terminal.
Flowchart 1
Flowchart 2
• Problem: How to calculate and
display the gross pay for an hourly
paid employee?

• Note that the flowchart represents the


logic of the pseudocode.
08/10/2024 Programming Fundamentals Week #1 9
Flowchart 3
Bugs and Debugging
• Programming errors are called bugs
• The process of tracking bugs and correcting
them is called debugging.
• Three kinds of errors:
– Syntax errors
– Logical errors
– Runtime errors

• It is useful to distinguish between them in order to


track them down more quickly
Syntax Error
Logical Error
Runtime Error
• All variables are declared before they are used
in a program.
• The declared variables need to know the type
of the memory that they point to, in order to
allow any operation on the data.
– Integer, Real, String, Character, etc.
Data Type
Variables
How to declare variables?

You can store a value in a variable with an assignment statement


The value can be the result of a calculation, which is created with math
operators
Simplistic View of a Computer

21
08/10/2024
Variable
To store a value inside a computer a ‘variable’ is
used.
A variable is a space in the memory to store a value. This
space is reserved until the variable is required.

The Programming language C has two main variable types


• Local Variables
• Global Variables

08/10/2024 22
An Example of a Variable

08/10/2024 23
Memory (RAM)
Program Memory. 1 Byte
.
• Each block in memory represents 204
1 byte 205
206
207
208
209
210
211
.
.
Memory (RAM)
Program Memory. 1 Byte
.
• Each block in memory represents 204
1 byte 205
206
• Each byte in memory has an 207
address 208
209
210
• Whenever a variable is declared,
211
the computer reserves some .
memory for it. .
Memory (RAM)
Program Memory. 1 Byte
.
Integers are stored in 4 bytes 204
Characters stored in 1 byte 205 $
206
int num = 100; 207
char letter = ‘$’; 208
209
210 10
211
.
0
.
Memory (RAM)
Program Memory. 1 Byte
.
Integers are stored in 4 bytes 204
Characters stored in 1 byte 205 $
206
int num = 100; 207
char letter = ‘$’; 208
209
210 10
Can we find the memory address of
these variables??
211
.
0
.
Memory (RAM)
Program Memory. 1 Byte
.
Integers are stored in 4 bytes 204
205 $
int num = 100; 206
char letter = ‘$’; 207
208
Can we find the memory address of 209
210 10
these variables??
211
.
0
cout<< &num; .
cout<< &letter;
Memory (RAM)
Program Memory. 1 Byte
.
int num = 100; 204
char letter = ‘$’; 205 $
206
cout<< &num; 207
cout<< &letter; 208
209
210 10
Variable Name Data Type

num int
Starting Address

208
211
.
0
letter char 205 .
Memory (RAM)
Program Memory. 1 Byte
.
int num = 100; 204
char letter = ‘$’; 205 $
206
cout<< &num; //prints 208 207
cout<< &letter; //prints 205 208
209
210 10
Variable Name Data Type

num int
Starting Address

208
211
.
0
letter char 205 .
Example of a Variable
(Memory View)
int temperature = 35
00000000 Location 0
Locations 0 - 3 are collectively 00000000 Location 1
called as ‘temperature’ 00000000 Location 2
00100011 Location 3
Location 4

100011 is the binary equivalent of 35 Location 5

08/10/2024 31
Changing the Value of Variable

Let's change the value of ‘temperature’


temperature = 45902
00000000 Location 0
Locations 0 - 3 are collectively 00000000 Location 1
called as ‘temperature’ 10110011 Location 2
01001110 Location 3
Location 4
Location 5
10110011 01001110 is the binary equivalent of 45902

08/10/2024 32
Variable types
Variable type Keyword used in Size in bits Range
declaration
integer int 32 bits -2147483648 to
2147483647
Short integer short int 16 bits -32768 to 32767

Long integer long int 32 bits -2147483648 to


2147483647
Floating point float 32 bits -1.0x1038 to 1.0x1038
data
Floating point double 64 bits -1.0x10308 to
data (with large 1.0x10308
fraction)
Text type data char 8 bits -128 to 127

Boolean data bool 1 bit 1 or 0


(True or False)
08/10/2024 33
Assignment Statement

x = 5671

5671 is written at the memory location


reserved for x

08/10/2024 Programming Fundamentals Week #2 35


Relative Comparison of int and
double
int numPeople = 2;
Reserves 32 bits (4 bytes) and sets
the value stored in that space to 2.
The name ‘numPeople’ is associated
with this space.

double bill = 32.45;


Reserves 64 bits (8 bytes) and
sets the value stored in that
space to 32.45.
The name ‘bill’ is associated
with this space.

08/10/2024 36
Variable Types
• To use a variable in our code
– First, we must have to declare it, variable are known with its
keywords. “Use the name of keyword” for declaration of a
variable”.
– Example: int number1 = 10;
– Example: float floatData = 20.93;
– Example: long int myData = -10;
– Example: char textData = ‘A’;
– Example: bool boolData = true;
#include <iostream> #include <iostream>
using namespace std; using namespace std;
void main () void main ()
{ {
int number1 = 10; int number1 = 20,
float floatData = number2 = 10;
20.93; }
}
08/10/2024
Points to remember in naming the
variables
• The names given to variables (and other
program features) are called identifiers.
• What are the rules for writing identifiers?
• You can use upper and lowercase letters, and the
digits from 1 to 9.
• You can also use the underscore (_).
• The first character must be a letter or
underscore. (it cannot be a digit)
• No spaces allowed in a variable name

08/10/2024 38
Program 1
Program 2
Escape Sequence

Examples:
cout << "Hello\t" << "I\’m Ali\n";
cout << "123\nabc
Example
#include<iostream>
#include<cstring>
using namespace std;
int main()
{

cout << "\t\t\t\t\t\t\t Main Menu\n";

cout << "\n\n\t\t\t\t1: Enter Data";


cout << "\n\n\t\t\t\t2: Print All Data";
cout << "\n\n\t\t\t\t3: Search Data";
cout << "\n\n\t\t\t\t4: Edit Data";
cout << "\n\n\t\t\t\t5: Go to main menu";
cout << "\n\n\t\t\t\t\tEnter option: ";
return 0;
}

08/10/2024 Presented by Dr. AKHTAR JAMIL 42


#include<iostream>
#include<cstring>
using namespace std;
int main()
{
cout << "\t\t\t\t\t\t\t Main Menu\n";
cout << "\n\n\t\t\t\t1: Enter Data";
cout << "\n\n\t\t\t\t2: Print All Data";
cout << "\n\n\t\t\t\t3: Search Data";
cout << "\n\n\t\t\t\t4: Edit Data";
cout << "\n\n\t\t\t\t5: Go to main menu";
cout << "\n\n\t\t\t\t\tEnter option: ";

cout << "\n\t\t\t\t\t====================================\n";


cout << "\t\t\t\t\t| *********** |\n";
cout << "\t\t\t\t\t|* BANK WALLET MENU *|\n";
cout << "\t\t\t\t\t| *********** |\n";
cout << "\t\t\t\t\t|* *|\n";
cout << "\t\t\t\t\t|* 1. Display User Information *|\n";
cout << "\t\t\t\t\t|* 2. Reset Username & Password *|\n";
cout << "\t\t\t\t\t|* 5. View details *|\n";
cout << "\t\t\t\t\t|* 6. Add Assets *|\n";
cout << "\t\t\t\t\t|* 7. View Assets *|\n";
cout << "\t\t\t\t\t|* 8. Use Currency Calculator *|\n";
cout << "\t\t\t\t\t|* 9. Logout *|\n";
cout << "\t\t\t\t\t|* 10. Login Again *|\n";
cout << "\t\t\t\t\t|* 11. Exit *|\n";
cout << "\t\t\t\t\t|* *|\n";
cout << "\t\t\t\t\t| *********** |";
cout << "\n\t\t\t\t\t====================================\n";
cout << "\t\t\t\t\tEnter your choice : ";
return 0;
}

08/10/2024 Presented by Dr. AKHTAR JAMIL 43


Program 3(a)
Two METHODS to print * in the pattern given
in picture
1. endl
2. \n
Program 3 (a)
Program 4
Program 5
Program and Flowchart
Comments in the Code
• All programs are documented in order to allow other
programmers to extend it.
• There are two types of comments:
– Line Comment
• // A line comment should explain the logic of the current
line in the program.

– Block Comment
• /* This is a Block Comment.*/

08/10/2024 Programming Fundamentals Week #1 49

You might also like