Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I would like to write a C++ program (with class) that will output the length of the longest subset of numbers that are balanced. their

I would like to write a C++ program (with class) that will output the length of the longest subset of numbers that are balanced. their summation must be 0 and the positive and negative numbers must match in reverse order. For example [1, 2, -2, -1] is balanced (length of 4) and [1, 2, -1, -2] is not balanced (but there are two balanced sets with size 2 each). please see attached. Thank you

You will be given a set of positive and negative numbers (from standard input), and will output the length of the longest subset of numbers that are balanced. In order for the subset to be balanced, their summation must be 0 and the positive and negative numbers must match in reverse order. For example [1, 2, -2, -1] is balanced (length of 4) and [1, 2, -1, -2] is not balanced (but there are two balanced sets with size 2 each). You will need to create a custom stack class for this problem, the class will look as follows

SPECIFCATIONS IN MAIN The input will consist of a set of positive and negative integers and you will need to match the positive and negative numbers in order to find the balanced subset of items. In order to maintain all the possible subsets, you will need to have an array of myStack objects and an array of the count (length of each subset). myStack * stack; //a pointer to a new array of stacks int * sizes; //hold length of subset of that particular set of numbers Whenever you encounter a positive number, you will push it onto all the non empty stacks in the list (and increment each size by 1 using the array of lengths) and then you will resize the array of stacks and array of lengths by 1 element and push that number onto that empty stack and its length will be 1. When- ever you encounter a negative, you will traverse through all your array of stacks and if the absolute value of the negative number matches the top element of a stack, you will pop that stack and increment its length by 1. Once you read 0, you will stop reading and then you will search the arrays for the maximum balanced subset. You will traverse the arrays of stacks and if you find a stack that is empty, you will check that stack's length and try to update the max value (the same way you've always used to find the max in a list) Make sure your code is memory leak free, so properly resize your arrays to avoid this issue and deallocate everything before the program terminates!

Each member will do the following: - node * topOfmyStack is a pointer that points to the top of the stack - myStack::myStack() is the default constructor that will initialize an empty stack - myStack::myStack(const myStack& copy) is the copy constructor which performs a deep copy of the stack object passed in. - const myStack& myStack::operator=(const myStack& rhs) is the assignment operator that performs a deep copy of the right hand side object - myStack::~myStack() is the destructor that will deallocate all the nodes in the stack object - void myStack::push(const Type& insert) performs a push operation (head insert) - void myStack::pop() performs a pop operation if the stack is not empty (head remove) - void myStack::pop(Type& item) performs a pop operation but first will place the stack top element into item before doing a head removal - Type myStack::top() const returns the top element of the stack - bool myStack::isEmpty() const returns true if the stack is empty and false if the stack is not empty

#include using namespace std;

template class myStack { public: myStack(); myStack(const myStack&); const myStack& operator=(const myStack&); ~myStack(); void push(const Type&); //performs a push operation (head insert) void pop(); //pop() performs a pop operation if the stack is not empty (head remove) void pop(Type&); //performs a pop operation but first will place the stack top element into item before doing a head removal Type top() const; //returns the top element of the stack bool isEmpty() const; //returns true if the stack is empty and false if the stack is not empty private: struct node { Type item; node * next; }; node * topOfmyStack; //a pointer that points to the top of the stack };

template myStack::myStack() { }

template myStack::myStack(const myStack& copy) { }

template const myStack& myStack::operator=(const myStack& rhs) { if (this != &rhs) { } return *this; }

template myStack::~myStack() { }

template void myStack::push(const Type& insert) { }

template void myStack::pop() { }

template void myStack::pop(Type& item) { }

template Type myStack::top() const { }

template bool myStack::isEmpty() const { }

int main() {

return 0; }

SAMPLE OUTPUT Enter input : 1 2 -2 -1 0 Longest balanced subset length is 4

Enter input : 1 2 -1 -2 0 Longest balanced subset length is 2

Enter input : 1 2 3 -3 4 -4 -2 -1 0 Longest balanced subset length is 8

Enter input : 1 2 3 -3 4 5 6 -5 -6 0 Longest balanced subset length is 2

Enter input : 1 2 -2 3 4 5 -5 -4 -3 0 Longest balanced subset length is 6

Enter input : 1 2 -2 0 Longest balanced subset length is 2

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Select Healthcare Classification Systems And Databases

Authors: Katherine S. Rowell, Ann Cutrell

1st Edition

0615909760, 978-0615909769

More Books

Students also viewed these Databases questions

Question

What about leadership lessons from particularly good or bad bosses?

Answered: 1 week ago

Question

How would you assess the value of an approach like this?

Answered: 1 week ago