Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
49 views

Data Structures and Algorithms

The document discusses four questions related to data structures and algorithms lab work. Question 1 involves initializing an array, searching for an element, and deleting an element. Question 2 redoes Question 1 using object-oriented concepts by defining a class. Question 3 proposes tracking sports club attendance by storing player shirt numbers in an array. Question 4 defines a structure to store student data and performs operations like searching, sorting by GPA, and calculating age.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Data Structures and Algorithms

The document discusses four questions related to data structures and algorithms lab work. Question 1 involves initializing an array, searching for an element, and deleting an element. Question 2 redoes Question 1 using object-oriented concepts by defining a class. Question 3 proposes tracking sports club attendance by storing player shirt numbers in an array. Question 4 defines a structure to store student data and performs operations like searching, sorting by GPA, and calculating age.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Data Structures and Algorithms

Lab-work # 2

Dated: October 25, 2021 Department: I-T


Submitted to: Sir Mushtaq Submitted by: Hassan Ali
BSSE 3 (A) Roll Number: (302-2017)

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.

Question No 2: Do the previous program by object oriented concepts.

CODE:

#include<iostream>

using namespace std;


class myarray void displaydata()

{ {

private: for(j=0; j<nElems; j++)

int arr[100]; cout << arr[j]<<" ";

int nElems = 0; cout << endl;

int j; }

int searchKey; void search()

public: {

void setdata() searchKey = 66;

{ for(j=0; j<nElems; j++)

arr[0] = 77; if(arr[j] == searchKey)

arr[1] = 99; break;

arr[2] = 44; if(j == nElems)

arr[3] = 55; cout<<"Can’t find" << searchKey;

arr[4] = 22; else

arr[5] = 88; cout << "Found " << searchKey << endl;

arr[6] = 11; }

arr[7] = 00; void del()

arr[8] = 66; { int main()

arr[9] = 33; searchKey = 55; {

nElems = 10; cout << "Deleting " << searchKey << endl; myarray a1;

} for(j=0; j<nElems; j++) a1.setdata();


if(arr[j] == searchKey) a1.displaydata();
break; a1.search();
for(int k=j; k<nElems; k++) a1.del();
arr[k] = arr[k+1]; a1.displaydata();
nElems--; }
}

};
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.

PROGRAM TITLE: SPORTS CLUB ATTENDANCE SYSTEM


Enter Shirt Number of Player

Search for a Player

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>

using namespace std;

int main()

int shirtno[10]={20,19,17,18,16,14,15,13,11,12};//Shirt numbers of 10 players store in array

int key; //Key for searching a specific player

int i; // for loop execustion variable

int numele=10; //Number of elements in array

int temp;

cout<<"Shirt Numbers of Player"<<endl;

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)

cout<<"Player Number "<<key<<" Not found"<<endl;

else

cout<<"\nPlayer Number "<<key<<" Found at index "<<i;

for(i=0;i<numele;i++) //Sorting Roll Numbers

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;

cout<<"\n AFTER SORTING\n";

cout<<"Shirt Numbers of Players"<<endl;

for(i=0;i<numele;i++)//Display Roll Numbers

cout<<"\t"<<shirtno[i]; } OUTPUT AT NEXT PAGE:


Question No 4:

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:

1. Make a list of student with GPA less than 3.00

2. Make a list of student with GPA more than 2.99

3. Search for a particular student

4. write a function which calculate student age

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:

NOTE : Coding file named as “Question 4.cpp” is attached in relevant folder.

You might also like