Data Structures &Algorithms
By Mr. Wubie A. (M.Tech) Dep’t of IT
Mizan-Tepi University
School of Computing
and Informatics
2.
2
Please bring toclass
each day
Introduction
Arrays
Structures
Functions
Pointers
Chapter ONE
3.
By Mr WubieA. (M.Tech)
3
The computer:
Nowadays is involved in our day to day lives.
Has limited memory so there is need for efficient utilization of memory
Stores and manipulates large amount of data ,the type of the data and
storage of the data are very important
Now the question: how are the data stored in the memory and how are
they processed?
The answer lies in the study of Data structure.
Introduction to Data Structure
4.
By Mr WubieA. (M.Tech)
4
A data structure may be defined as:
A data structure is a way of organizing data that considers not only the
items stored, but also their relationship to each other. i.e. is a systematic
way of organizing and accessing data.
Advance knowledge about the relationship between data items allows
designing of efficient algorithms for the manipulation of data.
The mathematical model to organize the data in the computer memory
(main or secondary) and the methods to process them are collectively
called data structures.
Introduction to Data Structure
5.
Review of C++Concepts
By Mr Wubie A. (M.Tech)
5
Arrays: An array is a collection of elements of the same type stored in contiguous and
homogeneous memory locations.
In C++ are a fundamental data structure that allows you to store a fixed-size
sequence of elements of the same type.
Used to store multiple values in a single variable, instead of declaring separate
variables for each value.
Declaring Arrays:
Syntax: type arrayName[arraySize];
Example:int numbers[5];
6.
Arrays
By Mr WubieA. (M.Tech)
6
Accessing Array Elements
Syntax: arrayName[index];
Example: int firstNumber = numbers[0]; // Accessing the first element
Modifying Array Elements
Example: numbers[2] = 10; // Changing the third element to 10
Looping Through Arrays
Using a for loop:
for(int i = 0; i < 5; i++) {
cout << numbers[i] << " ";
}
7.
Arrays
By Mr WubieA. (M.Tech)
7
A Linear Array in memory:
The elements of linear array are stored in consecutive memory locations.
It is shown below:
int A[5]={23, 4, 6, 15, 5, 7}
8.
Arrays
By Mr WubieA. (M.Tech)
8
Types of Arrays:
The elements of array will always be stored in the consecutive (continues) memory
location. The various types of Arrays are:
Single Dimension Array:
Array with one subscript
Ex: int A[i];
Two Dimension Array
Array with two subscripts (Rows and Column)
Ex: int A[i][j];
Multi Dimension Array:
Array with Multiple subscripts
Ex: int A[i][j]..[n];
9.
Arrays
By Mr WubieA. (M.Tech)
9
Basic operations of Arrays:
Some common operation performed on array are:
Traversing
Insertion
Deletion
Searching
Sorting
Merging
10.
Arrays
By Mr WubieA. (M.Tech)
10
Advantages of array
It is used to represent multiple data items of same type by using single
name.
It can be used to implement other data structures like linked lists, stacks,
queues, tree, graphs etc.
Two-dimensional arrays are used to represent matrices.
Many databases include one-dimensional arrays whose elements are
records.
11.
Arrays
By Mr WubieA. (M.Tech)
11
Disadvantages of arrays
We must know in advance the how many elements are to be stored in
array.
Array is static structure. It means that array is of fixed size. The memory
which is allocated to array cannot be increased or decreased.
Array is fixed size; if we allocate more memory than requirement then the
memory space will be wasted.
The elements of array are stored in consecutive memory locations. So
insertion and deletion are very difficult and time consuming.
12.
Pointers
By Mr WubieA. (M.Tech)
12
A Pointer is a variable whose value is the
address of another variable.
Like any variable or constant, you must
declare a pointer before you can work
with it. The general form of a pointer
variable declaration is:
type *var-name;
Here, type is the pointer's base type; it
must be a valid C++ type and var-name
is the name of the pointer variable.
13.
Pointers
By Mr WubieA. (M.Tech)
13
Scenario: You write a paper and include a lot of large images. You can send
the document as an attachment in the e-mail or upload it as a Google doc and
simply e-mail the URL. What are Pros and cons sending a URL?
Pros – Less info to send (send link, not all data)
– Reference to original (i.e. if original changes, you’ll see it)
Cons – Can treat the copy as a scratch copy and modify freely.
Phone numbers, e-mail or mailing addresses are references or “pointers”
to you or where you live
URLs (www.mtu.edu.et is a pointer to a physical HTML file on some
server) and can be used in any other page to "point to" MTU’s website
14.
Pointers
By Mr WubieA. (M.Tech)
14
When large data structures are being passed (i.e. arrays, class objects,
structs, etc.)
– So the computer doesn’t waste time and memory making a
copy
When we need to ask for more memory as the program is running (i.e.
dynamic memory allocation)
To provide the ability to access a specific location in the computer (i.e.
hardware devices)
– Useful for embedded systems programming
15.
Pointers
By Mr WubieA. (M.Tech)
15
#include<iostream.h>
int main()
{ int a = 30, b = 6, x, y,*ptr1, *ptr2;
ptr1 = &a; ptr2 = &b;
x = *ptr1 + *ptr2 – 6;
y = 6 - *ptr1 / *ptr2 + 30;
cout<<"Address of a = "<<ptr1<<endl;
cout<<"Address of b = "<<ptr2<<endl;
cout<<"a = "<<a<<"tb = "<<b<<endl;
cout<<"x = "<<x<<"ty = "<<y<<endl;
*ptr1 = *ptr1 + 70;
*ptr2 = *ptr2 * 2;
cout<<"a = "<<a<<"tb =
"<<b<<endl;
}
16.
Functions
By Mr WubieA. (M.Tech)
16
A function is a chunk of code that can be called to perform some well
defined task, such as calculating the area of a rectangle, computing the
weekly withholding tax for a company employee, or sorting a list of
names in ascending order.
- Syntax of function declaration and definition in C++.
- Example:
int add(int a, int b) {
return a + b;
}
17.
Functions
By Mr WubieA. (M.Tech)
17
Overloading means defining two or more functions or
operators that have the same name, but whose effect depends
on the types of their actual arguments. - Example:
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
18.
Structures
By Mr WubieA. (M.Tech)
18
A structure in C++ is a user-defined data
type that allows you to group different types
of variables together under a single name.
Structures are useful when you need to
represent an entity that has multiple
attributes.
For example, a student might have
attributes such as name, roll number, and
marks.
helps in organizing related data and
provides a way to manipulate them together.
19.
Structures
By Mr WubieA. (M.Tech)
19
Structures are a special type of classes.
By default, all members of a class are private, whereas by default all
members of a struct are public.
In C++, you define structure by using the reserved word struct.
If all members of a class are public, C++ programmers prefer to use a
struct to group the members.
A structure is defined just like a class.
20.
Review of C++Concepts
By Mr Wubie A. (M.Tech)
20
Thank you:
All the best