0% found this document useful (0 votes)
1 views12 pages

Dsa Assignment#01

The document contains two C++ programs: one for a stock trading system that manages shares using a queue, allowing users to buy and sell shares while calculating total capital gains, and another for a basketball drill that manages player turns in a queue format. Each program includes functions for enqueueing and dequeueing, along with user interaction for inputting transactions or player scores. The stock trading system emphasizes profit calculation, while the basketball drill focuses on player participation in turns.

Uploaded by

aizasarfaraz921
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views12 pages

Dsa Assignment#01

The document contains two C++ programs: one for a stock trading system that manages shares using a queue, allowing users to buy and sell shares while calculating total capital gains, and another for a basketball drill that manages player turns in a queue format. Each program includes functions for enqueueing and dequeueing, along with user interaction for inputting transactions or player scores. The stock trading system emphasizes profit calculation, while the basketball drill focuses on player participation in turns.

Uploaded by

aizasarfaraz921
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

HAMNA SIDDIQUI

02-134241-016
BS(CS)-3A
DATA STRUCTURE AND
ALGORITHM
DATE: 24-MARCH-2025
CODE

#include <iostream>
using namespace std;

struct Share
{
int quantity;
int purchasePrice;
};

const int MAX = 100;


Share queue[MAX];
int front = 0, rear = -1, queueSize = 0;

bool isEmpty()
{
return queueSize == 0;
}

void enqueue(const Share& share)


{
if (queueSize == MAX)
{
cout << "Queue is full. Cannot add more shares." << endl;
return;
}
rear++;
queue[rear] = share;
queueSize++;
}

void dequeue()
{
if (isEmpty())
{
cout << "Queue is empty. No shares to sell." << endl;
return;
}

for (int i = 0; i < queueSize - 1; i++)


{
queue[i] = queue[i + 1];
}

queueSize--;
}

Share& peekFront()
{
return queue[front];
}

int main()
{
int totalCapitalGain = 0;

cout << "============================ STOCK TRADING SYSTEM


============================" << endl;

int numTransactions;
cout << "Enter number of transactions: ";
cin >> numTransactions;

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


{
string transactionType;
int quantity, price;

cout << "Enter transaction (buy/sell): ";


cin >> transactionType;

if (transactionType == "buy")
{
cout << "Enter number of shares to buy: ";
cin >> quantity;
cout << "Enter price per share: $";
cin >> price;

enqueue({ quantity, price });


cout << "* Bought " << quantity << " shares at $" << price << " each." << endl;
cout << "--------------------------------------" << endl;
}
else if (transactionType == "sell")
{
cout << "Enter number of shares to sell: ";
cin >> quantity;
cout << "Enter selling price per share: $";
cin >> price;

int totalProfit = 0;
while (quantity > 0 && !isEmpty())
{
Share& frontShare = peekFront();
if (frontShare.quantity <= quantity)
{
totalProfit += frontShare.quantity * (price - frontShare.purchasePrice);
quantity -= frontShare.quantity;
dequeue();
}
else
{
totalProfit += quantity * (price - frontShare.purchasePrice);
frontShare.quantity -= quantity;
quantity = 0;
}
}

if (quantity > 0)
{
cout << "Warning: Not enough shares available to complete the sale!" << endl;
}

totalCapitalGain += totalProfit;

cout << "* Sold shares for a profit/loss of: $" << totalProfit << endl;
cout << "--------------------------------------" << endl;
}
}

cout << "\n============================ TOTAL CAPITAL GAIN


============================" << endl;
cout << "Total Capital Gain (or Loss) for all transactions: $" << totalCapitalGain << endl;

return 0;
system("pause");

OUTPUT
CODE

#include <iostream>
using namespace std;

struct BasketballDrill
{
int players[7];
int front = -1;
int rear = -1;
int size = 7;
int turns[7][5] = { 0 };

void enqueue(int player)


{
if (front == -1)
{
front = 0;
rear = 0;
}
else
{
rear++;
if (rear >= size)
rear = 0;
}
players[rear] = player;
}

int dequeue()
{
int player = players[front];
if (front == rear)
{
front = -1;
rear = -1;
}
else
{
front++;
if (front >= size)
front = 0;
}
return player;
}

void setup()
{
cout << "\n==================================================" <<
endl;
cout << " Basketball Passing and Shooting Drill " << endl;
cout << "==================================================" <<
endl;
cout << "\nCoach: Let's start the drill with 7 players!" << endl;

cout << "\nInitial Queue: ";


for (int i = 1; i <= 7; i++)
{
enqueue(i);
cout << i << " ";
}
cout << "\n\n";
}

void setTurns()
{
for (int i = 0; i < 7; i++)
{
cout << "Enter the points for player " << i + 1 << " (5 turns): ";
for (int j = 0; j < 5; j++)
{
cin >> turns[i][j];
}
}
}

void displayTurns()
{
cout << "\n===== DETAILS OF EACH PLAYER TURN'S =====:\n";
for (int i = 0; i < 7; i++)
{
cout << "Player " << i + 1 << " turns: ";
for (int j = 0; j < 5; j++)
{
cout << turns[i][j] << " ";
}
cout << endl;
cout << "----------------------------------------------" << endl;
}
}

void start()
{
for (int round = 1; round <= 5; round++)
{
cout << " Round " << round << ":" << endl;
cout << " --------" << endl;

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


{
int player = dequeue();
cout << " Player " << player << " takes a turn." << endl;
enqueue(player);
}
cout << endl;
}
cout << "\nDrill Complete!" << endl;
cout << "(Each player gets 5 turns)" << endl;
cout << "==================================================\n" <<
endl;
}
};

int main()
{
BasketballDrill drill;
drill.setup();
drill.setTurns();
drill.displayTurns();
drill.start();

return 0;
system("pause");
}

OUTPUT

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy