Delete and insert an element from/to array bag. Why boolean array instead of int?

563 Views Asked by At

I am implementing bag using array in C++. I can not figure out how to let the deleteElement function work. It is suppose to delete given element from the array. I also don't understand why the array is initialized with bool and how the insert function works.

So, I got three questions:\

  1. How to make the deleteElement function work?
  2. Why is the array initialized with bool?
  3. How does the insert function work? It looks like it only adds true value to the array, but when this program prints the array, you will see that the x value is printed out, I can not figure this out.
#include <iostream>
#include <math.h> 
#include <algorithm>
using namespace std;

// cin -> add 0 qry 0 del 0 qry 0 quit
// cout -> TF

// add x -> Adds the number x to the bag
// qry x -> Checks if x belongs to the bag then output T, otherwise output F
// del x -> If there is an element x in the bag, remove it, otherwise do nothing.
// quit  -> Stops the program

// Exercise: Fun with bags 1 (Here the bag is a set of int values).
/*
Example:
input: add 1 add 2 add 1 del 1 qry 1 qry 2 quit
output: FT
*/ 

// enumeration type for actions on the bag
enum action {add, qry, del, quit, none}; 

// translation of strings into actions
action str2action(string s) {
    if (s=="add")  return add;
    if (s=="qry")  return qry;
    if (s=="del")  return del;
    if (s=="quit") return quit;
    return none;
}

#define BAG_AS_ARRAY_SIZE 10

struct bag {
    bool as_array[BAG_AS_ARRAY_SIZE];    // using arrays
};

// Simple function to initialise the bag
void initialise(bag &b){

    // Array
    for(int i=0; i<BAG_AS_ARRAY_SIZE; i++){
        b.as_array[i] = false;
    }

}

// function to display the content of the bag
void display_bag(bag b) {

    cout << "The bag is : " << endl;

    // Array
    cout << " - (A) - : " ;
    for(int i=0; i<BAG_AS_ARRAY_SIZE; i++){
        if(b.as_array[i])
            cout << i << " " ;
    }
    
    cout << endl;


    return;

}

void insert(bag &b,unsigned int x){ //add

    // Array
    b.as_array[x] = true;
}

void check(bag &b,unsigned int x) //qry
{
    bool q = false;
    for(int i = 0; i < BAG_AS_ARRAY_SIZE; i++)
    { 
        if(b.as_array[x])
        {
            q = true;
        }
    }

   if(q == true)
   {
       cout << "T";
   }
   if(q == false) 
   {
       cout << "F";
   }
    cout << endl;
}

void DeleteElement(bag &b, unsigned int x) //del
{
    int i;
   for (i=0; i<BAG_AS_ARRAY_SIZE; i++) 
      if (b.as_array[i] == x) 
         break;

    if (i < BAG_AS_ARRAY_SIZE) 
   { 
     for (int j=i; j<BAG_AS_ARRAY_SIZE; j++) 
        b.as_array[j] = b.as_array[j+1]; 
   } 

}

// this function deals with actions on a bag
void update(bag &b, action a, unsigned int x){

    switch(a){
    case add:
        insert(b,x);
        break;
    case qry:
        check(b,x);
        break;
    case del:
        DeleteElement(b,x);
        break;
    case quit:
        break;
    case none:
        break;
    default:
        break;
    }

    return;
}

int main()
{
    bag my_bag; //We create an array of boolean type.
    string my_act_str;
    unsigned int x;

    initialise(my_bag); //The array is initialised with False, which is 0

    bool go_on = true;
    while(go_on)
    {
        display_bag(my_bag);    
        cout << "What's next? (actions = add x ,qry x ,del x ,quit)" << endl;
        cin >> my_act_str;
        action act = str2action(my_act_str);
        if(act == quit)
        {
            go_on = false;
        }
        if(act == add)
        {
            cin >> x;
            update(my_bag,act,x);
        }
        if(act == qry)
        {
            cin >> x;
            update(my_bag,act,x);
        }
        if(act == del)
        {
            cin >> x;
            update(my_bag,act,x);
        }

    }

    return 0;
}

Edit: I found out solution for the delete function. It is very easy one:

void delete_element(bag &b, unsigned int x) 
{
        b.as_array[x] = false;
}
1

There are 1 best solutions below

0
On

Your three questions actually come from the fact that this is not really a bag. What you have here is more like a "Boolean mask" that indicates if numbers from zero to BAG_AS_ARRAY_SIZE - 1 are true or false. That is why you have a Boolean array as the storage and all elements in it are initialized with false. That is, the mask is not set for any of the numbers from zero to BAG_AS_ARRAY_SIZE - 1.

Your deleteElement function then only needs to set the corresponding array position of the mask to false to "delete" that number and "inserting" a number corresponds to setting that specific position in the mask to true.

In the display_bag function, notice that you are not print the content of the array (which obviously can only be either true or false), but the index of the positions in the array that have a true value.