Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c++, program works as intended (50 random numbers, displays only multiple of 3)

ID: 3882173 • Letter: C

Question

c++, program works as intended (50 random numbers, displays only multiple of 3) although I am looking to remove duplicate random numbers if there are any. Thanks.

//Source File for Testing Bag.h

#include<iostream>

#include"Bag.h"

#include<time.h>

using namespace std;

int main()

{

Bag<int> myBag;

int a;

srand(time(0));

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

a = rand() % 100;

if (a % 3 == 0) { // add only if the number is a multiple of 3

myBag.add(a);

}

if (my)

}

myBag.display();

system("pause");

return 0;

}

//Header File for Class Bag (as a sample of an implementation of ADT

#ifndef _Bag

#define _Bag

#include<iostream>

using namespace std;

//Class Definition (Interface)

template<class ItemType>

class Bag

{

private:

ItemType bagStorage[100];

int size;

public:

Bag(); //This is a default constructor

int getSize();

bool isEmpty();

void add(ItemType item);

void remove(ItemType item);

void display();

void clear();

bool contains(ItemType item);

};

//Definition of Constructors

template<class ItemType>

Bag<ItemType>::Bag()

{

size = 0;

}

//Definition of Methods (Member Functions)

template<class ItemType>

int Bag<ItemType>::getSize()

{

return size;

}

template<class ItemType>

bool Bag<ItemType>::isEmpty()

{

if (size == 0)

return true;

else

return false;

}

template<class ItemType>

void Bag<ItemType>::add(ItemType item)

{

if (size == 100)

cout << "The bag is full!" << endl;

else {

bagStorage[size] = item;

size++;

}

}

template<class ItemType>

void Bag<ItemType>::display()

{

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

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

cout << bagStorage[i] << endl;

}

template<class ItemType>

void Bag<ItemType>::clear()

{

size = 0;

}

template<class ItemType>

bool Bag<ItemType>::contains(ItemType item)

{

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

if (bagStorage[i] == item)

return true;

}

return false;

}

template<class ItemType>

void Bag<ItemType>::remove(ItemType item)

{

if (isEmpty()) {

cout << "Removal is failed! The bag is empty!" << endl;

}

else if (!contains(item)) {

cout << "Removal is failed! The item is not in the bag." << endl;

}

else {

//First, find the index of the item

int index = 0;

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

if (bagStorage[i] == item) {

index = i;

break;

}

}

//To remove the item, shift all the numbers on the right side of item to the left by one place

for (int i = index; i<size - 1; i++) {

bagStorage[i] = bagStorage[i + 1];

}

size--;

}

}

#endif _Bag

Explanation / Answer

I have fixed the code to store 50 random numbers which are multiples of 3.
Also fixed printing and remove functions in Bag . Now 10 numbers will be printed per line.
After the numbers are filled in the bag, user is prompted for a number to be removed and a single instance of hte number is removed from the bag. User can stop input by typing -1. Hope the fixes help. Let me know if any issues. Please do rate the answer if it was helpful. Thank you.

Bag.h
=====
//Header File for Class Bag (as a sample of an implementation of ADT

#ifndef _Bag

#define _Bag

#include<iostream>
#include <iomanip>
using namespace std;


//Class Definition (Interface)

template<class ItemType>

class Bag

{
  
private:
  
ItemType bagStorage[100];
  
int size;
  
public:
  
Bag(); //This is a default constructor
  
int getSize();
  
bool isEmpty();
  
void add(ItemType item);
  
void remove(ItemType item);
  
void display();
  
void clear();
  
bool contains(ItemType item);
  
};

//Definition of Constructors

template<class ItemType>

Bag<ItemType>::Bag()

{
  
size = 0;
  
}

//Definition of Methods (Member Functions)

template<class ItemType>

int Bag<ItemType>::getSize()

{
  
return size;
  
}

template<class ItemType>

bool Bag<ItemType>::isEmpty()

{
  
if (size == 0)
  
return true;
  
else
  
return false;
  
}

template<class ItemType>

void Bag<ItemType>::add(ItemType item)

{
  
if (size == 100)
  
cout << "The bag is full!" << endl;
  
else {
  
bagStorage[size] = item;
  
size++;
  
}
  
}

template<class ItemType>

void Bag<ItemType>::display()

{
  
cout << "The bag contains following " << size << " integers: " << endl;
  
for (int i = 0; i<size; i++)
{
if(i % 10 == 0) //display only 10 numbers per line
cout << endl;
cout << setw(5) << bagStorage[i] << " ";
  
}
cout << endl;
  
}

template<class ItemType>

void Bag<ItemType>::clear()

{
  
size = 0;
  
}

template<class ItemType>

bool Bag<ItemType>::contains(ItemType item)

{
  
for (int i = 0; i<size; i++) {
  
if (bagStorage[i] == item)
  
return true;
  
}
  
return false;
  
}

template<class ItemType>

void Bag<ItemType>::remove(ItemType item)

{
  
if (isEmpty()) {
  
cout << "Removal is failed! The bag is empty!" << endl;
  
}
  
else if (!contains(item)) {
  
cout << "Removal is failed! The item is not in the bag." << endl;
  
}
  
else {
  
//First, find the index of the item
  
int index = 0;
bool found = false;
for (int i = 0; i<size; i++) {
  
if (bagStorage[i] == item) {
  
index = i;
found = true;
  
break;
  
}
  
}
  
//To remove the item, shift all the numbers on the right side of item to the left by one place
if(found)
{
for (int i = index + 1; i<size - 1; i++) {
  
bagStorage[i-1] = bagStorage[i];
  
}
size--;
}
}
  
}

#endif //_Bag


Test.cpp
======


//Source File for Testing Bag.h

#include<iostream>

#include "Bag.h"

#include<time.h>

using namespace std;

int main()

{
  
Bag<int> myBag;
  
int a;
  
  
  
srand(time(0));
  

for (int i = 0; i<50; ) {

a = rand() % 200;

if (a % 3 == 0 && !myBag.contains(a)) { // add only if the number is a multiple of 3

myBag.add(a);

i++;

  

}

}

  
while(true)
{
myBag.display();
cout << "Enter the number to remove (type -1 to stop): ";
cin >> a;
if(a == -1)
break;
myBag.remove(a);
}
  
  
system("pause");
  
return 0;
  
}

output
=====
The bag contains following 50 integers:

69 3 54 42 27 39 15 0 12 24
96 30 18 30 9 45 72 39 45 18
54 24 87 72 57 54 36 33 51 3
90 75 54 12 45 0 90 18 27 36
57 72 24 72 24 45 12 12 66 90
Enter the number to remove (type -1 to stop): 18
The bag contains following 49 integers:

69 3 54 42 27 39 15 0 12 24
96 30 30 9 45 72 39 45 18 54
24 87 72 57 54 36 33 51 3 90
75 54 12 45 0 90 18 27 36 57
72 24 72 24 45 12 12 66 66
Enter the number to remove (type -1 to stop): 66
The bag contains following 48 integers:

69 3 54 42 27 39 15 0 12 24
96 30 30 9 45 72 39 45 18 54
24 87 72 57 54 36 33 51 3 90
75 54 12 45 0 90 18 27 36 57
72 24 72 24 45 12 12 66
Enter the number to remove (type -1 to stop): 12
The bag contains following 47 integers:

69 3 54 42 27 39 15 0 24 96
30 30 9 45 72 39 45 18 54 24
87 72 57 54 36 33 51 3 90 75
54 12 45 0 90 18 27 36 57 72
24 72 24 45 12 12 12
Enter the number to remove (type -1 to stop): -1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote