02.array of Objects
02.array of Objects
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
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
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;
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];
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