Professional Documents
Culture Documents
Fall 2018
Introduction
Lecture 1
M. Tahir Awan
(mtahir@cust.edu.pk)
Capital University of Science & Technology (CUST),
Islamabad
Course Information
Email mtahir@cust.edu.pk
Lecture Slides
• Course Folder
– \\fs\lectures$\M.Tahir\CSEE2123
• Bill Gates
• Founder: Microsoft Corporation
• Bill Gates started Microsoft while he was
a University student at Harvard
Quizzes 20%
Assignments 15%
Class Participation 05%
Midterm Exam 20%
Final Exam 40%
Quizzes
Quizzes will be announced
There will be no make-up quizzes
Mid-Term
–Tentative Date : Nov 10 – 17, 2018
Final Exam
–Tentative Date : Jan 15 – 26, 2019
All the Exams will be close-book /
close-notes
Loader
Loader puts program in
memory.
Disk ..
..
..
Primary Memory
CPU takes each
CPU instruction and
executes it, possibly
storing new data
..
.. values as the program
9/18/2018 CSEE2123: OOP and DS ©.. M. Tahir Awan, CUST 30
executes.
Software Tools
• Compiler
• Compiler converts the code written in high-level language
into object code.
• Assembler
• Assembler converts the low level assembly language into
machine code of underlying processor.
• Linker
• Linker uses the object files created by the compiler and
predefined library objects to create an executable.
• Interpreter
• Interpreter interprets the user written code line by line,
every time program is executed. Interpreters are time
consuming and not used in real-time applications.
9/18/2018
27CSEE2123: 15 =32768, 231 = 2147483648
= 128, 2OOP and DS © M. Tahir Awan, CUST 34
C\C++ Operators
• Arithmetic/Numeric Operators
(+, -, *, /, %)
• Comparison Operators (Relational Operators)
(>, <, <=, >=, ==, !=)
• Boolean Operators (Logical Operators)
(!, &&, ||)
• Shorthand/Compound Assignment Operators
(+=, -=, *=, /=, %=)
• Increment and Decrement Operators(Unary
Operators)
(++, --)
• Bitwise operators
(&, |, ~, >>, <<, <<<, &=, |=, >>=, <<=)
9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 35
C\C++ : Operator Precedence
• Precedence of Operators
// function main
void main()
{
const float PI = 3.14159;
float radius, area;
printf("Entre Radius of Circle = ");
scanf("%f", &radius);
area = PI * radius * radius;
» IF-ELSE
» SWITCH
– Repetition Structures
no
» WHILE
» DO-WHILE yes
Loop
Body
» FOR
#include <iostream>
• Tells preprocessor to include the input/output
stream header file <iostream>
void main(){
int num1, num2;
num1 = 4;
cout << "num1 = " << num1 << endl;
// function main
void main()
{
const float PI = 3.14159;
float radius, area;
printf("Entre Radius of Circle = ");
scanf("%f", &radius);
area = PI * radius * radius;
// function main
void main()
{
const float PI = 3.14159;
float radius;
cout<<"Entre Radius of Circle = ";
cin>>radius;
float area = PI * radius * radius;
Function name
Return Type
int factorial(int n)
{ Function
Local Variables int factres = 1; Arguments
while(n>1)
{
factres = factres*n;
n--;
}
Return Value
return factres;
}
9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 53
Functions Example
• Write a function to compute average of two
numbers
#include <iostream> /* User defined Function */
using namespace std; float avg (float val1, float
val2)
float avg(float num1, float {
num2); float result;
result = (val1+val2)/2;
void main()
{ return result;
float data1, data2; }
data1 = 5;data2 = 7;
cout<<“Average =”<<
avg(data1, data2)<<endl;
data1 = 15;data2 = 9;
cout<<“Average =”<<
avg(data1, data2)<<endl;
}
9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 54
Function Argument/Parameter
Passing
• Call by value
– formal parameter receives the value of the
actual parameter
– function can NOT change the value of the
actual parameter
• Call by reference
– actual parameters are passed by reference/
pointers
– function can change the value of the actual
parameter
– Function can return multiple values when using
call by reference
9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 55
Passing Arguments by Reference :
Example
#include <iostream>
• Swap values of using namespace std;
two variables
void swap(int *a, int *b);
void main() {
int q = 3;
int r = 5;
swap(&q, &r);
cout<<"q = “<<q<<endl;
cout<<“r = “<<r<<endl);
}
/* function definition */
void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}
• Syntax:
dataType& reference-name = variable-name
• Example:
float total = 100;
float& sum = total;
9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 60
Recursion : Recursive Function
• A function that calls itself is called a recursive
function
• Recursive function only solves simplest case
(base case)
• Recursive function divides problem into two :
– If base case, Solve the problem
– If not base case, simplify the problem and
recursively call the function again
• Recursively calling itself function will ultimately
reach base case and solve it
ans = fact(n);
cout<<"Factorial= “<<ans;
return(0);
}
Row 0 a[ 0 ][ 0 ] a[ 0 ][ 1 ] a[ 0 ][ 2 ] a[ 0 ][ 3 ]
Row 1 a[ 1 ][ 0 ] a[ 1 ][ 1 ] a[ 1 ][ 2 ] a[ 1 ][ 3 ]
Row 2
a[ 2 ][ 0 ] a[ 2 ][ 1 ] a[ 2 ][ 2 ] a[ 2 ][ 3 ]
Column subscript
Array name
Row subscript
66
9/18/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST
2D Arrays in Memory
• 2D Arrays in C can be used to define matrices
• 2D Arrays are stored row-wise (row-major order)
in computer memory
4
int matrix[3][4];
1
0
Row 0 4 1 0 2 2D array in
memory 2
-1
Row 1 -1 2 4 3 2
4
Row 2 0 -1 3 1 3
0
-1
Column 3 3
Column 0 Column 1 Column 2
1
H e l l o \0
0 1 2 3 4 5 6 7 8 9
• https://www.coursera.org/learn/c-plus-plus-a
9/18/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 71
Hour of Code : Programming Tutorials
• http://hourofcode.com/us
• http://www.cplusplus.com/doc/tutorial/