CS301 Assignment 2 Solution and Discussion

Assignment No. 02
SEMESTER Fall 2019
CS301- Data Structures
Total Marks: 20

Due Date: 29-Nov, 2019
Instructions
Please read the following instructions carefully before solving & submitting assignment:
It should be clear that your assignment will not get any credit (zero marks) if:
o The assignment is submitted after due date.
o The submitted code does NOT compile.
o The submitted assignment is other than .CPP file.
o The submitted assignment does NOT open or file is corrupted.
o The assignment is copied (from other student or ditto copy from handouts or internet).
Uploading instructions
For clarity and simplicity, You are required to Upload/Submit only ONE .CPP file.

Note: Use ONLY Dev-C++ IDE.
Objective
The objective of this assignment is

o To make you familiar of Programming with Queue Data Structure.

For any query about the assignment, contact at [email protected]

GOOD LUCK

Marks: 20

Problem Statement:

You have to implement a Queue Data Structure for student’s admission applications in the university by using Array in C++ language. In which you have to create:

  1. A structure (using struct keyword) named Student.
  2. A class named ArrQueue.

Details:

Student structure must have two variables of type string (using string.h library) in it.

  1. userVUID
  2. userDetail
Structure Variables Description
userVUID It will store the student id e.g. BC12345688
userDetail It will store user details as Name (Degree Program) e.g. Bilal (BSCS)

ArrQueue should implement following data members and member functions:

Data Members Description
arr[arrLength] arr” is an array of type Student which will store queue students information in the array. While “arrLength” is the length of the array which is 5.
front front variable will store the value of array index of first user in the queue.
rear rear variable will store the value of array index of last user in the queue.
Methods Description
enQue(X) Place X after the rear of the queue (If array have empty space). e.g. queue.enQue(X). You have to handle these cases for enQue(). 1. If queue is empty. Then add Student to Queue. 2. If queue is full. Then show message: “Queue is full”.
deQue() Remove the student from front position in the queue and move all students forward in the queue. (If array is not empty). e.g. queue.deQue(). You have to handle these cases for deQue(). 1. If queue is empty. Then show message at “Que is empty cannot remove students”. 2. If queue is not empty, remove element from the queue.
Note: Removal of Students from Queue will be according to First In First Out (FiFO) Method (Means a student who comes to queue first will remove from the queue first then others).
queLength() Return size of Queue. (Not array size) e.g. queue.queLength().
isEmpty() Return TRUE if Queue is empty, FALSE otherwise.
isFull() Return TRUE if Queue is full, FALSE otherwise.
showQue() Will show the Queue data as given in below screenshots (Detailed Output Screenshot). e.g. queue.showQue().

(“X” denote “Student” structure Object means a student profile record while “queue” denotes and Object of the class “ArrQueue”.)

In the main() function you have to:

  1. Create an array of objects having size 5 and of type “Student” and store five student’s data in it.
  2. Make a queue and add all students in it by using for loop with enQue() method. Then show the array using showQue() method like given in “Main Output Screenshot”.
  3. Then you have to remove students from the queue according the following conditions e.g.
    Your id is “BC12345687” then last digit is 7.
    a. If last digit is Even Number then remove first two students from the queue using for loop with deQue() method. There will be 3 remaining students in the queue out of 5 as we have removed two students.
    For details see screenshots under “Main Output Screenshot” heading.
    b. If last digit is an Odd Number (e.g. in above id 7 is an odd number) then remove first one student form the queue using for loop with deQue() method. There will be 4 remaining students in the queue out of 5 as we have removed three students.
    For details see screenshots under “Main Output Screenshot” heading.
  4. After that show queue data using showQueue() method as given in “Main Output Screenshot”.
  5. Last student data should be your own data and all above ids must be one less than your id number according to Detailed Output Screenshot.

Note:
If you will not follow variables, functions and class names as mentioned and explained in this file also your actual output under heading “Main Output Screenshot” is not according to “Detailed Output Screenshot” then you will get Zero marks.
Detailed Output Screenshot:

Just For Your Information.

793f564b-bafc-4c4b-8a42-1ddaf5058848-image.png

Note: Last record must be your VU ID, Name and Degree Program.

Main Output Screenshot:

  1. For Students Id’s having Odd number in last of your id.
    d83ecb32-2d3d-4a30-bbe5-65f21401bbc0-image.png

  2. For Students Id’s having Even number in last of your id.
    a7616a16-9840-485c-9039-dbe5517aeae3-image.png

Lectures Covered: This assignment covers Lecture # 1-14.
Deadline: Your assignment must be uploaded/submitted at or before 29-Nov, 2019.

100% Solved:

#include <iostream>
#include <string.h> 
using namespace std;

const int arrLength = 10;

struct Student{
	string userVUID;
	string userDetails;
	
}std1={"NULL","NULL"};

class ArrQueue{
private:
	//Data Members
	Student arr[arrLength];
	int front;
	int rear;
	
public:
	//Constructor
	ArrQueue(){
		for(int i=0; i<arrLength; i++)
			arr[i] = std1;
		
		front = -1;
		rear = -1;
	}
	
	//Member Functions
	void enQue(Student std){
		
		if(isEmpty()){
			arr[0] = std;
			front++;
			rear++;
		}
		else if(isFull()){
			cout << " Queue is full.";
		}
		else{
			arr[rear+1] = std;
			rear++;
		}
	}
	
	void deQue(){
		
		if(isEmpty()){
			cout<< " No Student In the Queue.\n";
		}
		else if(rear == 0){
			arr[front] = std1;
			front = -1;
			rear = -1; 
		}
		else{
			
			int tempfront = front;
			arr[front] = std1;
			
			for(int i=1; i<=rear; i++ ){
				arr[front] = arr[i];
				front++;
			}

			rear--;
			front = tempfront;
		}
	}
	
	int queLength(){
		return rear+1;
	}
	
	bool isEmpty(){
		if(front==-1 || rear==-1)
			return true;
		else
			return false;
	}
	
	bool isFull(){
		if(rear == arrLength-1)
			return true;
		else
			return false;
	}
	
	void showQue(){
		cout << "\n |Sr. VU ID            Details     |";
		cout << "\n --- -------------------------------\n";
		for(int i=front; i<=rear; i++){
			cout<<"  "<< i+1<< ".  "<< arr[i].userVUID << "       " << arr[i].userDetails <<"\n";
		}
	}
	
};

int main(){
	/*Code For Even Id's 
	Student std[] = {{"BC12345684","Bilal (BSCS)"},
					 {"BC12345685","Bilal (BSCS)"},{"BC12345686","Bilal (BSCS)"},{"BC12345687","Bilal (BSCS)"},
					 {"BC12345688","Bilal (BSCS)"}};*/
	
	/*Code For Odd Id's */
	Student std[] = {{"BC12345683","Bilal (BSCS)"},
					 {"BC12345684","Bilal (BSCS)"},{"BC12345685","Bilal (BSCS)"},{"BC12345686","Bilal (BSCS)"},
					 {"BC12345687","Bilal (BSCS)"}};
	ArrQueue arrQue;
	
	cout << "\n -----------------------------------";
	cout << "\n |  Queue (After Adding Students)  |";
	cout << "\n -----------------------------------";
	for(int i=0; i<=4; i++){
		arrQue.enQue(std[i]);
	}
	arrQue.showQue();
	
	cout << "\n -----------------------------------";
	cout << "\n | Queue (After Removing Students) |";
	cout << "\n -----------------------------------";
	
	/* Code For Even Id's
	for(int i=0; i<=1; i++){
		arrQue.deQue();
	}*/
	
	/*Code For Odd Id's*/
	for(int i=0; i<1; i++){
		arrQue.deQue();
	}
	
	arrQue.showQue();
}

100% Solved:

#include <iostream>
#include <string.h> 
using namespace std;

const int arrLength = 10;

struct Student{
	string userVUID;
	string userDetails;
	
}std1={"NULL","NULL"};

class ArrQueue{
private:
	//Data Members
	Student arr[arrLength];
	int front;
	int rear;
	
public:
	//Constructor
	ArrQueue(){
		for(int i=0; i<arrLength; i++)
			arr[i] = std1;
		
		front = -1;
		rear = -1;
	}
	
	//Member Functions
	void enQue(Student std){
		
		if(isEmpty()){
			arr[0] = std;
			front++;
			rear++;
		}
		else if(isFull()){
			cout << " Queue is full.";
		}
		else{
			arr[rear+1] = std;
			rear++;
		}
	}
	
	void deQue(){
		
		if(isEmpty()){
			cout<< " No Student In the Queue.\n";
		}
		else if(rear == 0){
			arr[front] = std1;
			front = -1;
			rear = -1; 
		}
		else{
			
			int tempfront = front;
			arr[front] = std1;
			
			for(int i=1; i<=rear; i++ ){
				arr[front] = arr[i];
				front++;
			}

			rear--;
			front = tempfront;
		}
	}
	
	int queLength(){
		return rear+1;
	}
	
	bool isEmpty(){
		if(front==-1 || rear==-1)
			return true;
		else
			return false;
	}
	
	bool isFull(){
		if(rear == arrLength-1)
			return true;
		else
			return false;
	}
	
	void showQue(){
		cout << "\n |Sr. VU ID            Details     |";
		cout << "\n --- -------------------------------\n";
		for(int i=front; i<=rear; i++){
			cout<<"  "<< i+1<< ".  "<< arr[i].userVUID << "       " << arr[i].userDetails <<"\n";
		}
	}
	
};

int main(){
	/*Code For Even Id's 
	Student std[] = {{"BC12345684","Bilal (BSCS)"},
					 {"BC12345685","Bilal (BSCS)"},{"BC12345686","Bilal (BSCS)"},{"BC12345687","Bilal (BSCS)"},
					 {"BC12345688","Bilal (BSCS)"}};*/
	
	/*Code For Odd Id's */
	Student std[] = {{"BC12345683","Bilal (BSCS)"},
					 {"BC12345684","Bilal (BSCS)"},{"BC12345685","Bilal (BSCS)"},{"BC12345686","Bilal (BSCS)"},
					 {"BC12345687","Bilal (BSCS)"}};
	ArrQueue arrQue;
	
	cout << "\n -----------------------------------";
	cout << "\n |  Queue (After Adding Students)  |";
	cout << "\n -----------------------------------";
	for(int i=0; i<=4; i++){
		arrQue.enQue(std[i]);
	}
	arrQue.showQue();
	
	cout << "\n -----------------------------------";
	cout << "\n | Queue (After Removing Students) |";
	cout << "\n -----------------------------------";
	
	/* Code For Even Id's
	for(int i=0; i<=1; i++){
		arrQue.deQue();
	}*/
	
	/*Code For Odd Id's*/
	for(int i=0; i<1; i++){
		arrQue.deQue();
	}
	
	arrQue.showQue();
}