16
Arrays, Records and Pointers Csc-391

Arrays

Embed Size (px)

DESCRIPTION

As a part of the course CSC-391, Data Structure and algorithm

Citation preview

Page 1: Arrays

Arrays, Records and

PointersCsc-391

Page 2: Arrays

2

Data Structures and Algorithms

IntroductionLinear data structure. Is used to store similar types of data. An array is a

finite collection of similar elements stored in adjacent memory locations.

Decleration of the Arrays: (In C)the array name, the element type and the array size.Examples:int a[20], b[3],c[7]; // one-dimensional arraysfloat f[5], c[2];char m[4], n[20];

Initialization of an array is the process of assigning initial values. Examples:float, b[3]={2.0, 5.5, 3.14};char name[4]= {‘E’,’m’,’r’,’e’};int c[10]={0};

© SMT, Faculty, CSE, IUBAT

Page 3: Arrays

3

Data Structures and Algorithms

Two- dimensional arrays

©SMT, Faculty, CSE, IUBAT

Page 4: Arrays

4

Data Structures and Algorithms

Two- dimensional arrays

©SMT, Faculty, CSE, IUBAT

Page 5: Arrays

5

Data Structures and Algorithms

Two- dimensional arrays

©SMT, Faculty, CSE, IUBAT

Page 6: Arrays

6

Data Structures and Algorithms

RemarksPointer arrays and pointers:Array of pointers is called pointer array. Details on pointers and arrays is discussed in Array, func, pointdocument

Jagged arrays:Arrays whose rows orcolumns begins withdifferent numbers of data elements and ends with unused space, are called jagged array.

Dynamic arrays:int *my_array;my_array = new int[10]; // array size is defined during run-time

Group 1

Group 2

Group 3

Group 4

Evans Conrad Davis Baker

Harris Felt Segal Cooper

Lewis Glass Ford

Shaw Hill Gray

King Jones

Penn Reed

Silver

Troy

Wagner

* * * * 0 0 0 0 0

* * * * * * * * *

* * 0 0 0 0 0 0 0

* * * * * * 0 0 0

©SMT, Faculty, CSE, IUBAT

Page 7: Arrays

7

Data Structures and Algorithms

#include<iostream.h>int main (){int i,n; int * p;cout << "How many numbers would you like to type? ";cin >> i;p= new int[i]; // it takes memory at run-time from Heapif(p == NULL)cout << "Error: memory could not be allocated";else

{for(n=0; n<i; n++){

cout << "Enter number: ";cin >> p[n];

}int*k=p; // to hold the base address of dynamic arraycout << "You have entered: \n";for(n=0; n<i; n++) cout << *k<< ", "; k++;cout<<"\n";delete[] p; // it release the memory to send it back to Heap}

return 0;}

Sample code for Dynamic Array declaration

©SMT, Faculty, CSE, IUBAT

Page 8: Arrays

8

Data Structures and Algorithms

Records and structure (struct in C)Records may contain different types of information. Structures are used to store these records. A structure is a collection of logically related variables under a single unit/name. (In c++ we can create class to store records)

Example:struct Rectangle // this is type/name for structure{ float Length;

float width;float area;

};

A structure is usually declared before main( ) function.

©SMT, Faculty, CSE, IUBAT

Page 9: Arrays

9

Data Structures and Algorithms

Travers, Insert, Delete on Arraysa) Traversing in Linear Array

b) inserting in Linear Array

©SMT, Faculty, CSE, IUBAT

Page 10: Arrays

10

Data Structures and Algorithms

Travers, Insert, Delete on Arrays

c) Deleting from Linear Array

©SMT, Faculty, CSE, IUBAT

Page 11: Arrays

11

Data Structures and Algorithms

SearchingLinear Search

The linear search compares each element of the array with the search keyuntil the search key is found. To determine that a value is not in the array, the program must compare the search key to every element in the array. It is also called “Sequential Search” because it traverses the data sequentially to locate the element.

©SMT, Faculty, CSE, IUBAT

Page 12: Arrays

12

Data Structures and Algorithms

SearchingBinary Search

It is useful for the large sorted arrays. The binary search algorithm can only be used with sorted array and eliminates one half of the elements in the array being searched after each comparison.

©SMT, Faculty, CSE, IUBAT

Page 13: Arrays

13

Data Structures and Algorithms

SearchingBinary Search

©SMT, Faculty, CSE, IUBAT

Page 14: Arrays

14

Data Structures and Algorithms

Sorting

Bubble Sort

The technique we use is called “Bubble Sort” because the bigger valuegraduallybubbles their way up to the top of array like air bubble risingin water, while the small values sink to the bottom of array.

This technique is to make several passes through the array. Oneach pass,successive pairs of elements are compared. If a pair is inincreasing order (or the values are identical), we leave the values asthey are. If a pair is in decreasing order, their values are swapped inthe array.

©SMT, Faculty, CSE, IUBAT

Page 15: Arrays

15

Data Structures and Algorithms

Sorting

Bubble Sort

©SMT, Faculty, CSE, IUBAT

Page 16: Arrays

16

Data Structures and Algorithms

Sorting

Bubble Sort

©SMT, Faculty, CSE, IUBAT