0% found this document useful (0 votes)
6 views

02.array of Objects

Uploaded by

iqraqui
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

02.array of Objects

Uploaded by

iqraqui
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

Lecture Notes Data Structures and Algorithms

Array

Contents
 Array of Objects
 Operations on Arrays
 Main Function

Array of Objects
Arrays are probably the most common data structure used to store collections of elements.
An array is a collection of same data types which occupies contiguous memory locations. The
elements to be placed in an array must be of same data type and there should be contiguous
memory locations for allocation of arrays. Arrays are frequently used to store relatively
permanent data, as it is easy to traverse, search and sort. If size of structure and data are
constantly changing, Arrays are not preferred.
User-defined class
class Book {
private:
int id;
string title;
float price; // numbers of elements in a
public:
Book(){
id=0;
title=””;
price=0.0;
}
void setter() {
cin>> id >> title >> price;
}
void getter() {
cout << id << title << price;
}
int getId(){
return id;
}
};

1/7
Lecture Notes Data Structures and Algorithms

Operations on Arrays
Following are the major operations to manipulate array.
class myArray{
private:
Book a[10];
int N; // numbers of elements in a
public:
myArray(){
N=0;
}
void traverse();
void Insert();
void InsertAtStart();
void InsertAtEnd();
void Delete();
void DeleteStart();
void DeleteEnd();
void LinearSearch();
void BinarySearch();
void BubbleSort();
void Merge();
};
1. Traversing
The C++ code for traversing of an array is as follows.
void myArray :: traverse() {
for (i=0; i < N; i++) // traverse each element of array
a[i].getter();
}

2. Insertion
The C++ code for insertion of an element in a specified location in array is as follows.
void myArray :: Insert() {
if (N == 10) { // to check overflow
cout << “Array is full”;
return;
}
int i, ind; // N is index of last element in “a”
do {
cout << endl << “Enter the index to delete: “;
cin >> ind; // must be from 0 to 9
} while (ind<0 || ind >N-1);
Book b;
b.setter();
2/7
Lecture Notes Data Structures and Algorithms

for (i=N-1; i >= ind; i--) // shift element of array one


a[i+1] = a[i]; // starting from ind
a[ind] = b;
N++;
}
3. Insert at Start
The C++ code for insertion of an element at start of array is as follows.
void myArray :: InsertAtStart() {
if (N == 10) { // to check overflow
cout << “Array is full”;
return;
}
int i, ind=0; // N is index of last element in “a”
Book b;
b.setter();
for (i=N-1; i >= ind; i--) // shift element of array one
a[i+1] = a[i]; // starting from ind
a[ind] = b;
N++;
}

4. Insert At End
The C++ code for insertion of an element at end of array is as follows.
void myArray :: InsertAtEnd() {
if (N == 10) { // to check overflow
cout << “Array is full”;
return;
}
Book b;
b.setter();
a[N] = b;
N++;
}

5. Deletion
The C++ code for deletion of an element from array is as follows.
void myArray :: Delete() {
if (N == 0) { // to check underflow
cout << “Array is empty”;
return;
}
int i, ind;
do {
cout << endl << “Enter the index to delete: “;
cin >> ind; // must be from 0 to 9
} while (ind<0 || ind >N-1);
3/7
Lecture Notes Data Structures and Algorithms

for (i=ind; i < N-1; i++) // traverse each element of array


a[i] = a[i+1]; // override element of ind
a[N-1]=NULL;
N--;
}
6. Delete Start
The C++ code for deletion of an element from start of array is as follows.
void myArray :: DeleteStart() {
if (N == 0) { // to check underflow
cout << “Array is empty”;
return;
}
int i, ind=0;
for (i=ind; i < N-1; i++) // traverse each element of array
a[i] = a[i+1]; // override element of ind
a[N-1]=NULL;
N--;
}

7. Delete End
The C++ code for deletion of an element from end of array is as follows.
void myArray :: DeleteEnd() {
if (N == 0) { // to check underflow
cout << “Array is empty”;
return;
}
a[N]=NULL;
N--;
}

8. Searching
The C++ code for searching of an element in an array using linear search is as follows.
void myArray :: LinearSearch() {
if (N == 0) { // to check underflow
cout << “Array is empty”;
return;
}
int i, iid, flag=0;
cout << endl << “Enter a number to search: “;
cin >> iid;
for (i=0; i < N; i++)
if(iid == a[i].getId()) {
cout << “element is found”;
a[i].getter();
flag=1;

4/7
Lecture Notes Data Structures and Algorithms

}
if (flag == 0)
cout << “Item not found”;
}
Algorithm to search for an element from a sorted sequence is called Binary Search. The C++ code for
Binary Search is as follows.
void myArray :: BinarySearch() {
if (N == 0) { // to check underflow
cout << “Array is empty”;
return;
}
int i, j, num, Center, MAXINDEX=N-1, MININDEX=0, loc=0;

BubbleSort();

/* Binary search. */
while (MAXINDEX >= MININDEX) {
Center=(MAXINDEX+MININDEX)/2;
if (num==a[Center].getId()) {
loc=1;
break;
}
if (num<a[Center])
MAXINDEX=Center-1;
else
MININDEX=Center+1;
}
if (loc==1) {
cout << “Number found at index ” << Center;
a[center].getter();
}
else
cout << “Number not found.”;
}

9. Sorting
The C++ code for sorting of an array using Bubble S is as follows.
void myArray :: BubbleSort() {
if (N == 0) { // to check underflow
cout << “Array is empty”;
return;
}

int temp, i, j, s;

/* Sorting the Array in ascending order */


s=N-1;
for (i=1; i<N; i++) {
for (j=0; j<=(s-i); j++) {
5/7
Lecture Notes Data Structures and Algorithms

if (a[j]>a[j+1]) {
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}

10.Merging
The C++ code for merging of two arrays is as follows.
void myArray :: Merge() {
int i, j;
int a[5], b[5]; // array “mArr” of 10 integers is for
// merging a and b

cout << endl << “Enter five number for first array: “;
for (i=0; i < 5; i++) // get numbers for array “a”
cin >> a[i];

cout << endl << “Enter five number for second array: “;
for (i=0; i < 5; i++) // get numbers for array “b”
cin >> b[i];

for (i=0; i < 5; i++) // placement of element of array “a”


mArr[i] = a[i];
j=5; // used for merging array “b”
for (i=0; i < 5; i++) { // merging each element of array “b”
mArr[j] = b[i];
j++;
}
}

Main Function
int main () {
myArray arr;
arr.InsertAtStart();
arr.InsertAtEnd();
arr.InsertAtStart();
arr.InsertAtEnd();
arr.Insert();
arr.traverse();
arr.deleteStart();
arr.deleteEnd();
arr.delete();
arr.LinearSearch();
arr.BinarySearch();
arr.BubbleSort();
arr.Merge();
}

6/7
Lecture Notes Data Structures and Algorithms

7/7

You might also like