ِ‫ن‬َ‫م‬ْ‫ح‬‫ه‬‫الر‬ ِ ‫ه‬
‫اَّلل‬ ِ‫م‬ْ‫س‬ِ‫ب‬
‫يم‬ ِ‫ح‬‫ه‬‫الر‬
Data Structures
& Algorithm
Lecture No. 5
By: Azhar Iqbal
Even / odd Algorithm
 Step 1: Start
Step 2: [ Take Input ] Read: Number
Step 3: Check: If Number%2 == 0 Then
Print : N is an Even Number.
Else
Print : N is an Odd Number.
Step 4: Exit
Array
 An array is a collection of items stored at contiguous memory locations. The
idea is to store multiple items of the same type together.
Applications on Array
1.Array stores data elements of the same data type.
2.Arrays can be used for CPU scheduling.
3.Used to Implement other data structures like Stacks, Queues, Heaps, Hash tables etc.
int arr[5] = { 10, 20, 30, 40, 50 };
int arr[5];
Declaration & initialization
int values[5];
printf("Enter 5 integers: "); // taking input and storing it in an array
for(int i = 0; i < 5; ++i)
{
scanf("%d", &values[i]);
}
Array Types
Single Dimensional Array / One Dimensional Array
 single dimensional arrays are used to store list of values of same
datatype. In other words, single dimensional arrays are used to store a
row of values. In single dimensional array, data is stored in linear form.
Single dimensional arrays are also called as one-dimensional arrays,
Linear Arrays or simply 1-D Arrays.
int rollNumbers [60] ;
int marks [6] = { 89, 90, 76, 78, 98, 86 } ;
2D Array
A two-dimensional array can be
thought of as a matrix with rows and
columns. The general syntax used to
declare a two-dimensional array is
int my_array[5][3];
int main()
{
int my_array[5][3] = {
{1, 2, 3}, //row 1
{4, 5, 6}, //row 2
{7, 8, 9}, //row 3
{10, 11, 12}, //row 4
{13, 14, 15} //row 5
};
Initializing two-dimensional arrays
int main()
{
int my_array[5][3] = {1, 2, 3, 4, 5,
6, 7, 8,9, 10, 11, 12, 13, 14, 15};
}
Array Operations
Here, we have accessed the
second value of the array using
its index, which is 1. The output
of this will be 200, which is
basically the second value of
the balanced array.
printf(balance[1])
Insert
With this operation, you can insert one or more items into an array at the beginning, end, or any
given index of the array. This method expects two arguments index and value.
SYNTAX
arrayName.insert(index, value)
Delete
With this operation, you can delete one item from an array by value. This method accepts
only one argument, value. After running this method, the array items are re-arranged, and
indices are re-assigned
arrayName.remove(value)
UPDATE
This operation is quite similar to the insert method, except that it will replace the existing
value at the given index. This means will simply assign a new value at the given index.
This method expects two arguments index and value.
arrayName.udpate(index, value)
SEARCH & Traversing
Thank You. . . . .
Any Question…?

Data structures Lecture 5

  • 1.
    ِ‫ن‬َ‫م‬ْ‫ح‬‫ه‬‫الر‬ ِ ‫ه‬ ‫اَّلل‬ِ‫م‬ْ‫س‬ِ‫ب‬ ‫يم‬ ِ‫ح‬‫ه‬‫الر‬
  • 2.
  • 3.
    Even / oddAlgorithm  Step 1: Start Step 2: [ Take Input ] Read: Number Step 3: Check: If Number%2 == 0 Then Print : N is an Even Number. Else Print : N is an Odd Number. Step 4: Exit
  • 4.
    Array  An arrayis a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. Applications on Array 1.Array stores data elements of the same data type. 2.Arrays can be used for CPU scheduling. 3.Used to Implement other data structures like Stacks, Queues, Heaps, Hash tables etc.
  • 5.
    int arr[5] ={ 10, 20, 30, 40, 50 }; int arr[5]; Declaration & initialization int values[5]; printf("Enter 5 integers: "); // taking input and storing it in an array for(int i = 0; i < 5; ++i) { scanf("%d", &values[i]); }
  • 6.
    Array Types Single DimensionalArray / One Dimensional Array  single dimensional arrays are used to store list of values of same datatype. In other words, single dimensional arrays are used to store a row of values. In single dimensional array, data is stored in linear form. Single dimensional arrays are also called as one-dimensional arrays, Linear Arrays or simply 1-D Arrays. int rollNumbers [60] ; int marks [6] = { 89, 90, 76, 78, 98, 86 } ;
  • 7.
    2D Array A two-dimensionalarray can be thought of as a matrix with rows and columns. The general syntax used to declare a two-dimensional array is int my_array[5][3]; int main() { int my_array[5][3] = { {1, 2, 3}, //row 1 {4, 5, 6}, //row 2 {7, 8, 9}, //row 3 {10, 11, 12}, //row 4 {13, 14, 15} //row 5 }; Initializing two-dimensional arrays int main() { int my_array[5][3] = {1, 2, 3, 4, 5, 6, 7, 8,9, 10, 11, 12, 13, 14, 15}; }
  • 8.
    Array Operations Here, wehave accessed the second value of the array using its index, which is 1. The output of this will be 200, which is basically the second value of the balanced array. printf(balance[1])
  • 9.
    Insert With this operation,you can insert one or more items into an array at the beginning, end, or any given index of the array. This method expects two arguments index and value. SYNTAX arrayName.insert(index, value) Delete With this operation, you can delete one item from an array by value. This method accepts only one argument, value. After running this method, the array items are re-arranged, and indices are re-assigned arrayName.remove(value) UPDATE This operation is quite similar to the insert method, except that it will replace the existing value at the given index. This means will simply assign a new value at the given index. This method expects two arguments index and value. arrayName.udpate(index, value) SEARCH & Traversing
  • 10.
    Thank You. .. . . Any Question…?