# SOLVED CS301 Assignment 1 Solution and Discussion

• ``````Assignment No. 01
``````

Semester Spring 2020
CS301- Data Structures
Total Marks: 20

Due Date :1 June 2020
Instructions
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).
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 stack data structure.

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

GOOD LUCK

``````Marks: 20
``````

You are required to write a program in C++ to implement Stack data structure and use this data structure to store different types of books based on their identity number. You have to implement Stack data structure using array. A stack is a LIFO structure in which data elements are stored in an order such that last element pushed on the stack will be popped up first.
Your program should cover the following scenario.
Suppose a student is collecting books and throwing them into stack of books. You need to develop an application that will count different books in the stack. This stack will consist of three types of books i.e. software, hardware and other books.
You will identify book with unique numeric identity number. If book identity number will fully dividable by 8, will consider it software book. While the number fully dividable by 5 will consider it hardware book and consider rest of the books as other books.

First, the program will ask the user to input size of the stack. Then the programmer will ask the user to input total number of books. Then according to the number of Books, it will ask the user to enter identity number for each book. The book identity number can be any random number between 1 and 99. If book identity number is fully dividable by 8, we will consider it software book. While the number fully dividable by 5 will consider it hardware book and consider rest of the books as other books.

Programmer should implement isFull() function of the stack to check if the number of books exceed the size of the stack and isEmpty() function to check if the stack is empty or not. Programmer should also display the message “Books not found” if no books found.

Program’s sample output is given below

Output 1:

Out Put 2:

Output 3:

Solution Guidelines:

1. First understand the code given in handouts about stack.
2. Get stack size from user to allocate space for stack dynamically.
3. Get identity number of books that user want to enter into the stack.
4. Get value one by one and push into stack.
5. Don’t allow popping if stack is empty and pushing if stack is full.
6. If identity number of book is dividable by 8 and 5 then priority should be given to 8.
Lectures Covered: This assignment covers Lecture # 1-7.

• ``````/* Implementation of stack using array */

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

class  Stack  /* the Stack class */
{
public:
Stack() {
size = 0;
current = -1;
} //constructor

int pop() {
return data[current--];
}    	// The pop function

void push(int x) {
data[++current] = x;
//cout<<A[current];
} // The push function

int top(){
return data[current];
}      	// The top function

int isEmpty(){
return ( current == -1 );
} // Will return true when stack is empty

int isFull(){
return ( current == size-1);
} // Will return true when stack is full
void setStackSize(int s){
size = s;
data = new int[size];
}

private:
int   object;  				// The data element
int   current; 				// Index of the array
int   size;    				// max size of the array
int   *data;    				// Array of 10 elements
};

// Main method
int main()
{
int t_books= 0, size = 0;
int scount = 0, hcount=0, ocount=0; // For counting of book i.e software , hardware and other books.

Stack stack; 				// creating a stack object

cout<<"Set size of stack: ";
cin>>size;
stack.setStackSize(size);

cout<<"How many Books you want to add into stack: ";
cin>>t_books;

int book;
for(int i = 0; i < t_books; i++)
{
cout <<"\nEnter Book identity number that you want to insert into stack: ";
cin>>book;

if(!stack.isFull()) {		// checking stack is full or not

stack.push(book);     	// push the element at the top
}
else{
cout<<"\n Unable to insert "<<book<<". Stack is full, can't insert more";
break;
}
}

// pop the elements at the stack

int current = 0;

for (int i = 0; i < t_books; i++)
{

if(!stack.isEmpty()) {		// checking stack is empty or not
current = stack.pop();
if(current % 8 == 0){
scount++;

}
else if(current % 5 == 0){
hcount++;
}
else {
ocount++;
}
}

else {

cout <<"\nStack is empty, sorry can't pop more";
getch();
break;
}
}

if(scount == 0 && hcount==0 && ocount==0){
}
else {
cout<<"\n\nTotal Software Book/Books = "<<scount;
cout<<"\n\nTotal Hardware Book/Books = "<<hcount;
cout<<"\n\nTotal Other Book/Books = "<<ocount;

}
getch();
}

``````

• @zareen
100% Solution Code

``````#include <iostream>
using namespace std;

/* The Student class */
class Student {
private:
string firstname, lastname, VUID;
int marks;
Student *nextStudent;

public:
// constructor of Student class to initialize data members of class
Student(){
VUID = "";
marks = 0;
firstname = "";
lastname = "";
nextStudent = NULL;
}

// Student class method to set VU  ID of Student
void setVUID(string val){
VUID = val;
};

//Student class method to get VU ID of Student
string getVUID(){
return VUID;
};

// Student class method to set first name of Student
void setFirstName(string val){
firstname = val;
};

//Student class method to get first name of Student
string getFirstName(){
return firstname;
};

// Student class method to set last name of Student
void setLastName(string val){
lastname = val;
};

//Student class method to get last name of Student
string getLastName(){
return lastname;
};

//Student class method to set the Marks of Student
void setMarks(int val) {
marks = val;
};
// Student class method to get the Marks of Student
int getMarks() {
return   marks;
}

//Student class method to point current Student to next Student
void setNext(Student *nextStudent) {
this->nextStudent   =   nextStudent;
}

// Student class method to get memory address where pointer is pointing
Student *getNext() {
return   nextStudent;
}
};

/* The List class */
class List {

private:
Student *   current;

public:
// constructor of list class to initialize data members of class
List() {
current   =   NULL;
}

// list class method to add Students into list
Student *newStudent = new Student();
int loc_marks = 0;
string loc_vuid = "", loc_fname = "", loc_lname = "";

cout<<"\nEnter VU ID: ";
cin>>loc_vuid;
newStudent->setVUID(loc_vuid);

cout<<"Enter Marks: ";
cin>>loc_marks;
newStudent->setMarks(loc_marks);

cout<<"Enter First Name: ";
cin>>loc_fname;
newStudent->setFirstName(loc_fname);

cout<<"Enter Last Name: ";
cin>>loc_lname;
newStudent->setLastName(loc_lname);

newStudent->setNext(NULL);
current   =   newStudent;
}
else{
while(temp->getNext() != NULL && temp->getNext()->getMarks() >= loc_marks){
temp = temp->getNext();
}
current = temp;
newStudent->setNext(current->getNext());
current->setNext( newStudent );
current   =   newStudent;
}
};

// list class method to get the information of Student
void getInfo() {
if (current  !=  NULL){
cout<<"VU ID: "<<current->getVUID()<<endl;
cout<<"Marks: "<<current->getMarks()<<endl;
cout<<"First Name: "<<current->getFirstName()<<endl;
cout<<"Last Name: "<<current->getLastName()<<endl<<endl;
}
};

// list class method to move current to next Student
bool next() {
if (current  ==  NULL){
return  false;
}
current  =  current->getNext();
};

// frient function to list class to show all students in the list
friend void showStudents(List list){
Student* savedCurrent  =  list.current;

for(int i = 1; list.next(); i++){
list.getInfo();
}
list.current  =  savedCurrent;
};
};

main() {
int input = 0;
List lst;

while(input != -1) {

cout<<"1. To Add New Student in Ranking"<<endl;
cout<<"2. To Display Ranking"<<endl;
cout<<"3. To Close"<<endl<<endl;
cout<<"Enter Your Choice: (1, 2 or 3) ";
cin>> input;

if(input == 1) {
cout<<"Student's information saved successfully.\n";
}
else if(input == 2) {
cout<<"\nRanking Chart"<<endl;
showStudents(lst);
return 0;
}
else {
return 0;
}
}
}

``````

• Data Structures (CS301)
Assignment # 01
Semester Fall 2020
Total Marks = 20
26th Nov 2020

RULES FOR MARKING
It should be clear that your assignment would not get any credit if:
• The assignment is submitted after the due date.
• The submitted assignment does not open or file is corrupt.
• Strict action will be taken if submitted solution is copied from any other student or from the internet.

Lectures:

• Lectures 1 to 7 are covered in this assignment
NOTE
No assignment will be accepted after the due date via email in any case (whether it is the case of load shedding or internet malfunctioning etc.). Hence refrain from uploading assignment in the last hour of deadline. It is recommended to upload solution file at least one day before its closing date.
If you people find any mistake or confusion in assignment (Question statement), please consult with your instructor before the deadline. After the deadline no queries will be entertained in this regard.
For any query, feel free to email at: [email protected]

Problem Statement:
Suppose you are asked to make a Ranking information chart of students in a dynamic list called linked list using C++ language. The data will be stored in this list in order i.e. the highest marks student will be on top position with other information whereas the next highest marks student will be on second top position and so on.
Your program will get the information of a student in the form of input (Student ID, Name, Marks) through console. After filling the required information, the student will be inserted in linked list at right place. The right place of student will be decided on the basis of marks.
Furthermore, you are required to use only classes for this assignment. As we are not covering Struct in this course so using it in assignment solution is not allowed.
Sample Output:
See the gif file attached with this assignment file.

Note:
DO REMEMBER that you must use your VUID as an input in this program.

Submission details
Following Files Must be submitted in a single zip or rar file.
• C++ code file (file name should be your VUID)
• A .gif file which shows only “execution” of your Application (For Recording .gif a software named Screentogif is uploaded on LMS, or you can use any other gif recording tool as well)
• First record must be with your own VU-ID in .gif file.
If you do not submit any of the above-mentioned file or use some other VU-ID, you will be awarded Zero Marks.

“The End”

• ``````/* Implementation of stack using array */

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

class  Stack  /* the Stack class */
{
public:
Stack() {
size = 0;
current = -1;
} //constructor

int pop() {
return data[current--];
}    	// The pop function

void push(int x) {
data[++current] = x;
//cout<<A[current];
} // The push function

int top(){
return data[current];
}      	// The top function

int isEmpty(){
return ( current == -1 );
} // Will return true when stack is empty

int isFull(){
return ( current == size-1);
} // Will return true when stack is full
void setStackSize(int s){
size = s;
data = new int[size];
}

private:
int   object;  				// The data element
int   current; 				// Index of the array
int   size;    				// max size of the array
int   *data;    				// Array of 10 elements
};

// Main method
int main()
{
int t_books= 0, size = 0;
int scount = 0, hcount=0, ocount=0; // For counting of book i.e software , hardware and other books.

Stack stack; 				// creating a stack object

cout<<"Set size of stack: ";
cin>>size;
stack.setStackSize(size);

cout<<"How many Books you want to add into stack: ";
cin>>t_books;

int book;
for(int i = 0; i < t_books; i++)
{
cout <<"\nEnter Book identity number that you want to insert into stack: ";
cin>>book;

if(!stack.isFull()) {		// checking stack is full or not

stack.push(book);     	// push the element at the top
}
else{
cout<<"\n Unable to insert "<<book<<". Stack is full, can't insert more";
break;
}
}

// pop the elements at the stack

int current = 0;

for (int i = 0; i < t_books; i++)
{

if(!stack.isEmpty()) {		// checking stack is empty or not
current = stack.pop();
if(current % 8 == 0){
scount++;

}
else if(current % 5 == 0){
hcount++;
}
else {
ocount++;
}
}

else {

cout <<"\nStack is empty, sorry can't pop more";
getch();
break;
}
}

if(scount == 0 && hcount==0 && ocount==0){
}
else {
cout<<"\n\nTotal Software Book/Books = "<<scount;
cout<<"\n\nTotal Hardware Book/Books = "<<hcount;
cout<<"\n\nTotal Other Book/Books = "<<ocount;

}
getch();
}

``````

1

1

2

1

8

2

4

1
| |