Question

Please Help This: please follow style guidelines Rule 1: use const where appropriate Rule 2: every...

Please Help This:

please follow style guidelines

Rule 1: use const where appropriate

Rule 2: every member function must include a description in the header file including precondition and postcondition.

Rule 3: every member variable must include a description of what the variable holds.

Rule 4: Classes should be capitalized. Bag, not bag. Person, not person. (I know this is different than how the book does it)

Rule 5: member variables of classes should be preceded by “m_”. If it’s not a member variable DON’T precede with m_

Rule 6: static const variables should be all capital letters. e.g: static const std::size_t CAPACITY = 30;

Rule 7: declare public:, protected:, then private:

Rule 8: non-member variable names should be lowercase.

Rule 9: Every class you define should be in its own .cpp and .h.

create a member function in Bag called get that has a parameter called indexwhich is the index of the element in the Bag to return. The get function should return this element.

Make sure you have a reasonable Precondition for this function (Hint: what if the user passes an index of 7 when you only have 5 elements in the Bag?). Use assert to guarantee your precondition.

Write a main() that puts 5 ints in a Bag and then iterates through the bag with a for loop.

No user input is necessary for Part 1. Just pick 5 numbers to add to the bag.

//////// Bag.h //////

#ifndef BAG_BAG_H

#define BAG_BAG_H

#include <cstdlib>

namespace CSCI2421

{

    class Bag

    {

    public:

        typedef int value_type;

        static const std::size_t MAX_BAG_SIZE = 30;

        // Initializes the Bag

        // Postcondition: Bag is initialized

        Bag();

        // inserts an element into the Bag

        // Precondition: size() < CAPACITY

        // Postcondition: A new copy of entry has been added to the Bag

        void insert (const value_type& x);

        // attempts to remove one element that is equal to x from the Bag.

        // Returns: true if the element was removed, else false. \

        // Precondition: none

        // Postcondition: one element that is equal to x is removed from the Bag.

        bool erase_one (const value_type& x);

        // erases every element that is equal to x from the Bag.

        // Returns: the number of elements erased

        // Precondition: none

        // Postcondition: all the elements that are equal to x are removed from the Bag

        std::size_t erase (const value_type& x);

        // returns the number of elements in the bag

        // Precondition: none

        // Postcondition: none

      std::size_t size() const;

        // returns the number of elements equal to x in the Bag

        // Precondition: none

        // Postcondition: none

        std::size_t count (const value_type& x) const;

    private:

        // the number of elements currently in the Bag

        std::size_t m_NumberOfElements;

        // where the Bag stores it's elements

        value_type m_Data[MAX_BAG_SIZE];

    };

}

#endif //BAG_BAG_H

///// Bag.cpp ///////

#include "Bag.h"

#include <cassert>

CSCI2421::Bag::Bag() : m_NumberOfElements(0)

{

}

void CSCI2421::Bag::insert(const value_type& x)

{

    assert (size() < MAX_BAG_SIZE);

    m_Data[m_NumberOfElements] = x;

    m_NumberOfElements++;

}

bool CSCI2421::Bag::erase_one(const value_type& x)

{

    for (std::size_t index = 0; index < m_NumberOfElements; index++)

    {

        if (m_Data[index] == x)

        {

            m_NumberOfElements--;

            m_Data[index] = m_Data[m_NumberOfElements];

            return true;

        }

    }

    return false;

}

std::size_t CSCI2421::Bag::erase(const value_type& x)

{

    std::size_t index = 0;

    std::size_t numberRemoved = 0;

    while (index < m_NumberOfElements)

    {

        if (m_Data[index] == x)

        {

            m_NumberOfElements--;

            m_Data[index] = m_Data[m_NumberOfElements];

            numberRemoved++;

        }

        else

        {

            index++;

        }

    }

    return numberRemoved;

}

std::size_t CSCI2421::Bag::size() const

{

    return m_NumberOfElements;

}

std::size_t CSCI2421::Bag::count(const value_type& x) const

{

    std::size_t counter = 0;

    for (std::size_t i = 0; i < m_NumberOfElements; i++)

    {

        if (m_Data[i] == x)

            counter++;

    }

    return counter;

}

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
Please Help This: please follow style guidelines Rule 1: use const where appropriate Rule 2: every...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • PLEASE HURRY. Below is the prompt for this problem. Use the code for bag1.cxx, bag1.h and...

    PLEASE HURRY. Below is the prompt for this problem. Use the code for bag1.cxx, bag1.h and my code for bag.cpp. Also I have provided errors from linux for bag.cpp. Please use that code and fix my errors please. Thank you The goal of assignment 3 is to reinforce implementation of container class concepts in C++. Specifically, the assignment is to do problem 3.5 on page 149 of the text. You need to implement the set operations union, intersection, and relative...

  • The goal of this task is to reinforce the implementation of container class concepts using linked...

    The goal of this task is to reinforce the implementation of container class concepts using linked lists. Specifically, the task is to create an implementation file using a linked list. You need to use the header files, set3.h and node1.h, and the test program, test_set3.cpp. Your documentation must include the efficiency of each function. Please make your program as efficient and reusable as possible. set3.h #ifndef _SET_H #define _SET_H #include <cstdlib> #include <iostream> class set { public: typedef int value_type;...

  • The purpose of this program is to help reinforce container class concepts and linked list concepts...

    The purpose of this program is to help reinforce container class concepts and linked list concepts in C++. Specifically, the task is to implement the sequence class using a linked list. You need to use the author's file sequence3.h to create your implementation file named sequence3.cpp. Please make your code as efficient and reusable as possible. Please make sure code can pass any tests. sequence3.h // FILE: sequence3.h // CLASS PROVIDED: sequence (part of the namespace main_savitch_5) // This is...

  • The purpose of this lab is to help reinforce container class concepts and linked list concepts...

    The purpose of this lab is to help reinforce container class concepts and linked list concepts in C++. Specifically, the lab to repeat the sequence lab from last week except to use a linked list. You need to use the author's files sequence3.h and sequence_exam3.cpp. Author - Michael Main, Book - Data Structures and other objects using c++, 4th edition // FILE: sequence3.h // CLASS PROVIDED: sequence (part of the namespace main_savitch_5) // This is the header file for the...

  • C++ programming language: In this program you will create a simplified bag that acts like a...

    C++ programming language: In this program you will create a simplified bag that acts like a stack meaning that the Last item inserted is the First Item that comes out. Your backend implementation must use a linked list. The code should pass the test (there's only 1) and there should be no memory leaks. Note that passing the test does not ensure full credit! The functions are listed in the suggested order of implementation but you may implement them in...

  • I need help implemeting the remove_repetitions() Here is a brief outline of an algorithm: A node...

    I need help implemeting the remove_repetitions() Here is a brief outline of an algorithm: A node pointer p steps through the bag For each Item, define a new pointer q equal to p While the q is not the last Item in the bag If the next Item has data equal to the data in p, remove the next Item Otherwise move q to the next Item in the bag. I also need help creating a test program _____________________________________________________________________________________________________________________________________________________ #ifndef...

  • Requirements Print a range Write a bag member function with two parameters. The two parameters are...

    Requirements Print a range Write a bag member function with two parameters. The two parameters are Items x and y. The function should write to the console all Items in the bag that are between the first occurrence of x and the first occurrence of y. You may assume that items can be compared for equality using ==. Use the following header for the function: void print_value_range(const Item& x, const Item& y); print_value_range can be interpreted in a number of...

  • 1. Here are codes to define a stack class based on dynamic array, please complete the...

    1. Here are codes to define a stack class based on dynamic array, please complete the copy constructor //--- Definition of Stack copy constructor Stack::Stack(const Stack & original) : myCapacity(original.myCapacity), myTop(original.myTop) { //--- Get new array for copy myArray = new(nothrow) StackElement[myCapacity]; if (myArray != 0) // check if memory available                         // copy original's array member into this new array {              // Please complete the function here        } else {          cerr << "*Inadequate memory to allocate...

  • This is a C++ program about link list, please help me complete the codes, thanks!(the .h...

    This is a C++ program about link list, please help me complete the codes, thanks!(the .h file has been provided) /* * List.h * * Class Description: List data collection ADT. * Class Invariant: Data collection with the following characteristics: * - Each element is unique (no duplicates). * - (What other characteristic does our List have?) * * * */ #pragma once // You can add #include statements if you wish. #include <string> #include "Patient.h" using namespace std; class...

  • Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo t...

    Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo this program so that it uses the STL list and STL queue! Thank you! HighestGPAData.txt* 3.4 Randy 3.2 Kathy 2.5 Colt 3.4 Tom 3.8 Ron 3.8 Mickey 3.6 Peter 3.5 Donald 3.8 Cindy 3.7 Dome 3.9 Andy 3.8 Fox 3.9 Minnie 2.7 Gilda 3.9 Vinay...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT