LEC 1 Introduction
LEC 1 Introduction
Course Information
Outlines
Course Objectives
❖Combine fundamental data structures and algorithmic techniques in
building a complete algorithmic solution to a given problem.
Course Outcomes (COs)
Course Learning Outcomes
CLO1 Describe the different implementations of data structures and algorithms for
appropriate design decisions based on application data requirements.
CLO3 Build a Mini-project using OOP coding for different data structures and
algorithms applications.
Course Information
Additional Course Notes - Lecture Notes.
References:
- Armstrong Subero, "CODELESS DATA STRUCTURES AND
ALGORITHMS", ISBN-13 (electronic): 978-1-4842-5725-8,
2020.
Essential Books (Textbooks): S.Mohanty and P.Tripathy, "Data Structure and Algorithms -
Using C++, A Practical Implementation", Scrivener Publishing,
John Wiley & Sons,2021.
Grading:
Assessment Method Week Weighting of Asses.
Experimental 10%
15
Oral Test & Reports 10%
Final Exam
16 60%
Total 100%
Outlines
1- Introduction to Programming
2- Introduction to Data Structure and Algorithm
3- A Brief Revision to C++
Programming
a program is a set of step by step instructions that directs the computer to do the
tasks you want it to algorithm and produce the results you want.
The Evolution of Programming Languages
Understand problem
Step 3: Maintenance
requirements
Use and modify the program if there is a problem Does program require user
If the problem is complex, divide it into sub-problems interaction?
Analyze each sub-problem as above Does program manipulate
data? Algorithm:
The data structure is a way that specifies how to organize and manipulate
the data. It also defines the relationship between them. Data Structures
are the central part of many computer science algorithms as they enable
the programmers to handle the data in an efficient way
◼Mobile Applications
Primitive DS Non-Primitive DS
A primitive data structure is generally a basic structure that is usually built into the
language, such as an Integer, Floating-point number, Character constants, string constants,
pointers etc, fall in this category.
There are more sophisticated DS Non-Primitive DS
The non-primitive data
structures emphasize on
structuring of a group of
homogeneous (same type) or Linear List Non-Linear List
heterogeneous (different
type) data items.
A non-primitive data
structure is built out of Queue
primitive data structures Array Link List Graph Trees
linked together in meaningful
ways. Stack
A linear data-strucutre has sequentially arranged Non-Linear Data Structure: The Non-linear data
data items. The next time can be located in structure does not form a sequence i.e. each item or
the next memory address. It is stored and element is connected with two or more other items
accessed in a sequential manner. Array and in a non-linear arrangement. The data elements are
list are example of linear data structure. not arranged in the sequential structure.
Array
Linked List
Queue Stack
Tree
Algorithms
How to implement?
A normal calculator program. Direct translation for requirements can do it!
Remember to respect math: 2+1*5 = 7 NOT 15
Given 1000 integers, find how many 3 numbers with sum 400?
We can do 3 loops and try every 3 numbers!
1000000000 operations! Too much Computations!
Given airport locations and cost of every direct flight, find flight
from cairo to newyork with the minimum cost?
..how to represent airports relations!
Facebook site has 1.4 Billion users, please suggest to mostafa
all new friends from them, where there is exactly 2 friends
between mostafa and each of a new friend?
Examples :
++K , K++ → k= K+1
---K , K-- → K= K-1
36
When ++ (or – –) is used before the variable name, the
computer first increments (or decrements) the value of the
variable and then uses its new value to evaluate the expression.
x = 1; x = 1;
Cout << x++; Cout << ++x;
37
special assignment statements
X +=2 ; means x = x + 2;
x *=y; means x = x * y;
x /=y; means x = x / y;
Int v=0;
Cout<<v++;
V++;
Cout<< v--;
--v;
Cout<< v++;
38
Functions
Experience has shown that the best way to develop and maintain a large program is
to construct it from smaller pieces or modules, each of which is more manageable
than the original program.
Task 4
Task 1
Function ( )
Function ( )
Task 3
Task 2
Function ( ) Function ( )
40
Function Definition
Parameter
Data_type function-name( parameter ) It is input to the function. It
is Optional
{
Lines of code to be
In return function
executed It Is the data type for the returned value
… from the function to the calling program. if
no returned value expected , we use Void
…… function.
(Body)
RETURN ;
} Float main(float v1, float v2) Void sw(int& x1,int & x2)
{ {
Float c= v1+v2; Int x3 =x1;
Return c; X1=x2;
41
} X2=x3;}
Arrays
Array : is a collection of fixed number of elements, where in all of elements
have same data type.
Array Basics:
Consecutive group of memory locations that all have the same type.
The collection of data is indexed, or numbered, and at starts at 0 and
The highest element index is one less than the total number of elements
in the array.
One-dimensional array:
Multi-dimensional array:
• Example:
int arr[5];
43
Recall that arrays go from 0 to size– 1
Array Initialization
Consider the declaration
After declaring the array you can use the For .. Loop to initialize it with values
submmited by the user.
Example:
for (int i = 0; i < 10; i++)
cin >> array[i];
Student 1
Student 2
Student 3
Student 4
Student 5
Student 6
declaration
Datatype ArrayName [ Rows] [Columns] ;
Float array [6] [4] ;
declaration
After declaring the array you can use the For .. Loop to initialize it
with values submmited by the user.
46
Int main() Int main()
Void add ( int a[3][4] )
{ {
Const int b=5; {
Const int row =3, col=4;
Int cv[b]; Int sum =0;
Int x[row][col];
Int sum =0;
For (int i=0; i<b; i++) For (int i=0; i<3; i++)
{ For (int i=0; i<row; i++)
For (int j=0; j<4; j++)
Cout<< “enter the values”<< i<<endl; For (int j=0; j<col; j++)
Cin>>cv[i]; {
{
} Sum+= a[i][j];
Cin>> x[i][j];
}
For (int i=0; i< b; i++) }
{
Sum = sum + cv[i]; Add(x);
} Cout<< “the sum is
Return 0;
“<<sum;
}
Cout<< sum << endl; Cout<< sum/ 12;
Return 0; }
}
Structure struct Employee
{
A Structure is a collection of related data items, possibly of int emp_no ;
different types. string fname ;
string lname;
A structure type in C++ is called struct. float salary;
float bonus ;
In contrast, array is homogeneous since it can contain only data of
float total_salary;
the same type. };
int main ()
For Example : To store an Employee data like {
Employee emp1;
( emp_no , fname , lname , salary , bonus , total_salary ); emp1.emp_no = 12;
emp1.fname="Ahmed";
emp1.lname="Ali";
To Access the Memebers of the struct , use the . Operator emp1.salary=3000;
emp1.bonus=500;
emp1.total_salary=
emp1.salary +
emp1.bonus;
}
48
Classes in C++
A class definition begins with
the keyword class. class class_name
The body of the class is
{ private members or
private: methods
contained within a set of braces, { }; …
(notice the semi-colon). …
…
public: Public members or methods
Within the body, the keywords private: …
and public: specify the access level …
…
of the members of the class. };
the default is private.
Void show()
{
Cout<<x1<< “ “<<x2<<endl;
}
};
Pointers
One of the simplest ideas in C++, but one which most students have a problem
with is a pointer
int m = 5;
int *ptr;
ptr = &m;
cout << *ptr << endl; // prints 5
Similarly, we can modify values stored at an address:
int m = 5;
int *ptr;
ptr = &m;
▪ Design Issue:
▪ select and design appropriate data types
(This is the main motivation to learn and understand
data structures)