I am having trouble on removing the largest and smallest numbers and getting the sum and dividing it by

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Following the instructions presented in the Lecture 4.5 and 4.6, complete the class Bag we discussed in the lectures. It should include all the data members, constructors, as well as all the member functions we have discussed. To be more specific, the class should have an interface shown as following:

 

class Bag

{

private:

int items[100];

int itemCount;

public:

Bag();

int getItemCount();

bool add(int newItem);

void display();

bool contains();

bool remove(int a);

int getSum();

};

 

Please complete all the member functions listed above including the default constructor.

 

Then add following member function to the class:

 

getMax()

 

This function returns the largest integer stored in the Bag without changing the position of each integer stored in the Bag.

 

Write a C++ program in a .cpp file (source file) that include above class Bag and perform following tasks:

 

Use function rand() to randomly generate 80 integers between 0 and 99 and put them into an object of the class Bag.

 

Use the member functions in the class Bag, such as getMax(), getMin(), and so on to

 

find the largest number and the smallest number in the Bag

 

display the indices of above those two numbers in the Bag

 

remove one largest number and one smallest number from the Bag.

 

find the sum of the remaining numbers and divide it by 78, and then display the result on the screen.

 

===============================================================================================

 

You will need to upload two files for this assignment:

 

The header file (.h file) that contains the required class Bag.

 

The source file (.cpp file) of your program specified as above.

 

Note: The class Bag should be created exactly the way we discussed in the class lecture 4.5 and 4.6. Otherwise, the assignment submitted will not be credited.

 

this is what I have so far

 

//class Bag

#ifndef Bag_h

#define Bag_h

 

#include<iostream>

using namespace std;

 

class Bag

{

private:

   int items[100];

   int itemCount;

public:

   Bag();

   int getItemCount();

   void add(int newItem);

   void display();

   bool contains(int anItem);

   bool remove(int anItem);

   int getSum();

   int getMax();

   int getMin();

};

 

//Class Implementation

Bag::Bag()

{

   itemCount = 0;

}

int Bag::getItemCount()

{

   return itemCount;

}

void Bag::add(int newItem)

{

   items[itemCount];

   itemCount++;

}

void Bag::display()

{

   cout << "The bag contains following integers:" << endl;

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

       cout << items[i] << endl;

   }

}

bool Bag::contains(int anItem)

{

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

       if (items[i] == anItem)

           return true;

   }

   return false;

}

bool Bag::remove(int anItem)

{

   if (itemCount == 0) {

       return false;

       cout << "Item is not in the bag, removal failed. \n";

   }

   else {

       int index = 0;

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

           if (items[i] == anItem) {

               index = i;

               for (int k = index; k < itemCount - 1; k++) {

                   items[k] = items[k + 1];

               }

               itemCount--;

               return true;

           }

       }

       cout << "Item is not in the bag, removal failed. \n";

       return false;

   }

}

int Bag::getSum()

{

   int s = 0;

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

       s += items[i];

   return s;

}

int Bag::getMax()

{

   int max = items[0];

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

   {

       if (max < items[i])

       {

           max = items[i];

       }

   }

   return max;

}

int Bag::getMin()

{

   int min = items[0];

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

   {

       if (min > items[i])

       {

           min = items[i];

       }

   }

   return min;

}

#endif Bag_h

 

I am having trouble on removing the largest and smallest numbers and getting the sum and dividing it by 78

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 11 images

Blurred answer
Knowledge Booster
Matrix multiplication
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education