### CS301 Assignment No 1 Fall 2017

Dear Students,**CS301 Assignment Solution**has been added. Here you can read or

__Download CS301__- Data Structures

__Assignment No 1 Solution__and Discussion of Semester

**Fall 2017**. Lectures covered in this assignment are Lecture 1 to 12. Assignment Due Date is 20 November 2017. Total Marks are 20. We are here to facilitate your learning and we do not appreciate the idea of copying or replicating solutions.

#### CS301 Assignment Instructions

Please read the following instructions carefully before solving & submitting the assignment:It should be clear that your assignment will not get any credit (marks) if:

- The assignment is submitted after due date.
- The submitted code does NOT compile.
- The submitted assignment file is not in .CPP format.
- The submitted assignment file does not open or is corrupted.
- The assignment is copied (from other student or ditto copy from handouts or internet).

### CS301 Assignment Uploading instructions

For clarity and simplicity, You are required to Upload/Submit only ONE.CPP file which will contain the solution of question.- Do not wait for grace day. Grace Day is given only if there is problem with LMS on due date. Submit your solution within due date.
- Note that no assignment will be accepted through email if there is any problem in LMS on grace day.

**Please Note:**

**Use only dev-C++ IDE.**

### CS301 Assignment Objective

The objective of this assignment is- To make you familiar with working of stack data structure and programming techniques to implement and understand the working of this data structure.

### CS301 Assignment Question:

Write a C++ program to balance three (3) stacks so the sum of numbers in each stack should be same. [Marks 20]CS301 Assignment No 1 Question |

Your program should fulfill following requirements.

- Create three (3) stacks, initially each stack will have three same numbers. A number of each stack should be different from other stack numbers. You can see in above diagram that initially each stack have same number but the number is different from the numbers in other stacks.
- Your program should swap values between stack in the way that after balancing the stacks. Each stack should have the different number as shown in above diagram.
- Please note that user will enter numbers into stack, not X, Y and Z.
- User will enter a number for each stack which will be inserted 3 times in each stack.
- You can implement stack through array or link list. It is your choice.

**Sample Output:**

CS301 Assignment Solution Sample Required output |

### CS301 Assignment Solution Guidelines:

- First, understand the code given in handouts about the stack.
- Don’t allow popping if the stack is empty and pushing if the stack is full.

#### CS301 Assignment No 1 Solution Fall 2017

You can see the sample Solution file page preview below. and You can easily Download CS301 Assignment Solution File from the Download Button given below:#### CS301 Assignment Solution Code

Please carefully read all instructions in assignment file and understand the point No. 5.**You can implement stack through the array or link list**. It is your choice.

In this idea solution,

**we only used array not implement stack through array or link list.**`#include <iostream> `

using namespace std;

class Stack

{

public:

Stack()

{

size = 3; current = -1;

} //constructor

int pop()

{

return A[current--];

} // The pop function

void push(int x)

{

A[++current] = x;

} // The push function

int top()

{

return A[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

private:

int object; // The data element

int current; // Index of the array

int size; // max size of the array

int A[3]; // Array of 10 elements

};

//----------------------------------------------------------------------------------------

Stack pushStack(int num);

void popStack(Stack stack);

//----------------------------------------------------------------------------------------

// The main method

int main()

{

int num;

Stack stackFirst;

Stack stackSec;

Stack stackThird;

int sFPop=0;

int sSPop=0;

int sTPop=0;

//-----------------------------------------------------------------------------------------

cout<< "Enter a number to push in stack 1: ";

cin>> num;

sFPop = num;

stackFirst = pushStack(num);

cout<< "Enter a number to push in stack 2: ";

cin>> num;

sSPop = num;

stackSec = pushStack(num);

cout<< "Enter a number to push in stack 3: ";

cin>> num;

sTPop = num;

stackThird = pushStack(num);

//-----------------------------------------------------------------------------------------

// pop the elements at the stack

cout << "\nStacks before Balancing... " ;

cout << "\nStack 1 numbers: ";

popStack(stackFirst);

cout << "\nStack 2 numbers: ";

popStack(stackSec);

cout << "\nStack 3 numbers: ";

popStack(stackThird);

//-----------------------------------------------------------------------------------------

stackFirst.pop();

stackSec.pop();

stackThird.pop();

sFPop = stackFirst.pop();

sSPop = stackSec.pop();

sTPop = stackThird.pop();

stackFirst.push(sSPop);

stackFirst.push(sTPop);

stackSec.push(sTPop);

stackSec.push(sFPop);

stackThird.push(sSPop);

stackThird.push(sFPop);

//-----------------------------------------------------------------------------------------

// pop the elements at the stack

cout << "\n\n\nStacks After Balancing... " ;

cout << "\nStack 1 numbers: ";

popStack(stackFirst);

cout << "\nStack 2 numbers: ";

popStack(stackSec);

cout << "\nStack 3 numbers: ";

popStack(stackThird);

cout <<endl<<endl<<endl;

system("pause");

}

//------------------------------------------------------------------------------------------

Stack pushStack(int num){

Stack stack;

for(int i = 0; i <=2; i++)

{

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

stack.push(num);

}else

cout <<"\n Stack is full, can't insert new element";

}

return stack;

}

//------------------------------------------------------------------------------------------

void popStack(Stack stack){

for (int i = 0; i <=2; i++)

{

if(!stack.isEmpty()) { // checking stack is empty or not

cout << stack.pop()<<"\t";

} else

cout <<"\n Stack is empty, can't pop ";

}

}