Loading...

This Artical offers a comprehensive walkthrough of CS301p Assignment 2 for the year 2023,



#include <iostream>
#include <vector>

using namespace std;

class Product
{
private:
     string name;
     int frequency;

public:
     Product() : name(""), frequency(0) {}

     Product(const string &productName, int productFrequency) : name(productName), frequency(productFrequency) {}

     string getName() const
     {
          return name;
     }

     int getFrequency() const
     {
          return frequency;
     }

     void incrementFrequency()
     {
          frequency++;
     }
};

class Heap
{
private:
     int currentSize;
     vector<Product> array;
     int capacity;

public:
     Heap(int capacity) : currentSize(0), capacity(capacity)
     {
          array.resize(capacity);
     }

     void percolateDown(int index)
     {
          int child;
          Product temp = array[index]; // new node bena raha ho

          while (index < currentSize / 2)
          {
               child = 2 * index + 1;

               if (child < currentSize - 1 && array[child].getFrequency() < array[child + 1].getFrequency())
               {
                    child++;
               }

               if (temp.getFrequency() < array[child].getFrequency())
               {
                    array[index] = array[child];
                    index = child;
               }
               else
               {
                    break;
               }
          }

          array[index] = temp;
     }

     void buildHeap(const vector<Product> &products, int size)
     {
          currentSize = size;
          for (int i = 0; i < size; i++)
          {
               array[i] = products[i];
          }

          for (int i = currentSize / 2 - 1; i >= 0; i--)
          {
               percolateDown(i);
          }
     }

     void updateFrequency(const string &productName)
     {
          for (int i = 0; i < currentSize; i++)
          {
               if (array[i].getName() == productName)
               {
                    array[i].incrementFrequency();
                    break;
               }
          }

          buildHeap(array, currentSize);
     }

     void traverse() const
     {
          for (int i = 0; i < currentSize; i++)
          {
               cout << array[i].getName() << " (" << array[i].getFrequency() << ") ";
          }
          cout << endl;
     }

     Product getMax() const
     {
          return array[0];
     }

     bool isEmpty() const
     {
          return currentSize == 0;
     }

     bool isFull() const
     {
          return currentSize == capacity;
     }
};

int main()
{
     int capacity = 7;
     Heap heap(capacity);

     vector<Product> products = {
         Product("P1", 15),
         Product("P2", 14),
         Product("P3", 13),
         Product("P4", 10),
         Product("P5", 16),
         Product("P6", 9),
         Product("P7", 12),
     };

     cout << "Current Products with their frequencies" << endl;
     for (const Product &product : products)
     {
          cout << product.getName() << "=" << product.getFrequency() << ", ";
     }
     cout << endl;

     int choice;
     do
     {
          cout << "Enter your choice:\n"
                  "Enter 1 to create Heap\n"
                  "Enter 2 to search for a product and update its frequency\n"
                  "Enter 3 to see the product with the highest frequency\n"
                  "Enter 0 to terminate the program"
               << endl;
          cin >> choice;

          switch (choice)
          {
          case 1:
               heap.buildHeap(products, products.size());
               cout << "Heap created: ";
               heap.traverse();
               break;

          case 2:
          {
               string productName;
               cout << "Enter the product you want to search or type 'No' to exit: ";
               cin >> productName;

              while (productName != "No" && productName != "no")
               {
                    heap.updateFrequency(productName);
                    cout << "Updated Heap: ";
                    heap.traverse();
                    cout << "Enter the product you want to search or type 'No' to exit: ";
                    cin >> productName;
               }
               break;
          }

          case 3:
               if (!heap.isEmpty())
               {
                    Product maxProduct = heap.getMax();
                    cout << "Top product: " << maxProduct.getName() << " (" << maxProduct.getFrequency() << ")" << endl;
               }
               else
               {
                    cout << "Heap is empty" << endl;
               }
               break;

          case 0:
               cout << "Terminating the program..." << endl;
               break;

          default:
               cout << "Invalid choice. Please try again." << endl;
               break;
          }
     } while (choice != 0);

     return 0;
}


    DOWNLOAD




Generated by Embed Youtube Video online

Get 50% Discount.

Lorem, ipsum dolor sit amet consectetur adipisicing elit. Exercitationem, facere nesciunt doloremque nobis debitis sint?