Data Structures and Algorithms
Data Structures and Algorithms
Lab-work # 2
Question 1:
ABSTRACT:
In this question we are given an array of size 100 and 10 elements are initialized into first 9 indexes. Then we used a
for loop and get a print of array elements , then we perform searching operation by comparing the all the indexes
with key to search , from this we found the index index number and then we showed that key found or not.
Next operation we have done on array elements is deletion , it is a little bit difficult and time taking process because it
first find out the index number and then step back all elements down the array. At the last we displayed elements list
with in which a key element will be deleted.
CODE:
#include<iostream>
{ {
int j; }
public: {
arr[5] = 88; cout << "Found " << searchKey << endl;
arr[6] = 11; }
nElems = 10; cout << "Deleting " << searchKey << endl; myarray a1;
};
OUTPUT:
Explanation:
I use the concept of class and object, I created a class named as myarray and then add some necessary
private data then in public I created four functions one for set/initializing data , I,e putting numbers in every
index of an array , second function is named as display() , obviously name show that it is going to display
data , third function is search () which search for a particular element inside the array, fourth and last
function is del() it delete a specific key element from the array by pushing back all the index values.
Then In main function I simply created an object of myarray class named as a1, this object call functions and
hence the same operation of question no 1 is done by object oriented way.
Question No 3:
Imagine a group of data you would like to put in a computer so it could be accessed and manipulated. For
example, if you collect old comic books, you might want to catalog them so you could search for a particular
author or a specific date.
Sort Players
Display Players
I created a Program which is for a cricket or any sports academy, 10 Players are registered in academy and
their attendance is marked on their shirt number , now I put shirt number of every player in an array of size
ten. Then I Used search algorithm for searching a player in the field , then at last I sorted all players from
low to high order.
CODE:
#include<iostream>
int main()
int temp;
for(i=0;i<numele;i++)//Display players
cout<<"\t"<<shirtno[i];
key=14; //Searching player number 14 in array
for(i=0;i<numele;i++)
if(shirtno[i]==key)
break;
if(i==numele)
else
for(int j=0;j<numele;j++)
if(shirtno[j]>shirtno[j+1])
temp=shirtno[j];
shirtno[j]=shirtno[j+1];
shirtno[j+1]= temp;
We are required to create a program which use structure to store name , id ,date of birth , GPA and
Semester of 10 students. Moreover followings are some requirements:
ABSTRACT:
I just created a structure named as student and put all required data along with three functions for
initializing data , displaying data and calculating age respectively.Now in main function I created an abject
s1 with array size 10 and it store data of 10 students, then I performed search operation on a specific name,
After this I created a list of students having GPA greater than 2.99 and another list of students having GPA
greater than 3.0 . At the last I showed both lists.
OUTPUT: