CS301 Assignment 1 Solution and Discussion

Re: CS301 Assignment 1 Solution and Discussion
Assignment No. 01
Semester Spring 2020
CS301 Data Structures
Total Marks: 20Due Date :1 June 2020
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 DevC++ IDE.
Objective
The objective of this assignment iso 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:
 First understand the code given in handouts about stack.
 Get stack size from user to allocate space for stack dynamically.
 Get identity number of books that user want to enter into the stack.
 Get value one by one and push into stack.
 Don’t allow popping if stack is empty and pushing if stack is full.
 If identity number of book is dividable by 8 and 5 then priority should be given to 8.
Lectures Covered: This assignment covers Lecture # 17.
Deadline: Your assignment must be uploaded/submitted at or before, 1 June 2020

/* 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 == size1); } // 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){ cout<<"\nBooks not found\n"; } else { cout<<"\n\nTotal Software Book/Books = "<<scount; cout<<"\n\nTotal Hardware Book/Books = "<<hcount; cout<<"\n\nTotal Other Book/Books = "<<ocount; } getch(); }

/* 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 == size1); } // 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){ cout<<"\nBooks not found\n"; } else { cout<<"\n\nTotal Software Book/Books = "<<scount; cout<<"\n\nTotal Hardware Book/Books = "<<hcount; cout<<"\n\nTotal Other Book/Books = "<<ocount; } getch(); }