Download presentation
Presentation is loading. Please wait.
1
Data Structures and Algorithms
Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering IIT Bombay Session: Mumbai Vada-Pav Restaurant (Randomized simulation) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
2
Random Numbers In the previous example, we simulated the arrival time of customers using some ‘assumed’ time instances. In practice, such events will occur randomly When we throw a dice, we get a random value between 1 and 6 We do not know, which will be next value On an average, we make 600 throws, we will get 100 of each value It is possible to ‘simulate’ such events by generating random numbers Called ‘pseudo’ random numbers, (generated by an algorithm) C++ provides functions to generate random numbers Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
3
Random Numbers Use ‘rand()’ to generate ‘uniformly distributed’ random numbers Returns a number from 0 to RAND_MAX int r, dice, i; for (i=0; i<10; i++) { r = rand(); dice = r%6 + 1; cout << dice << “ “; } Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
4
Random number generation
We wish to generate a different sequence of numbers for each ‘run’ Use ‘srand()’ Initialize random number generator with a ‘seed’ Different seed ensures a different sequence to be generated Use current time as seed for random generator std::srand(std::time(0)); //use current time as seed Output from two different executions: Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
5
Data Structures Maintain information for food and customer arrival
Structure 1: foodInfo Contains information of food item (ID, name, and rate) Structure 2: customerInfo Contains information of customer (ID and arrival time) that arrive and stand in the queue to place an order Queue 1: customerQueue It is of type structure, ‘customerInfo’ Contains information of customer Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
6
Data Structures Maintain information of the orders placed and
The order that goes to the kitchen for preparation Structure 3: orders Contains information of the order placed by each customer (Token ID, Items, Quantity, Cost, Number of items ordered, Total Cost to be paid, and time for placing order) Structure 4: kitchen Contains the information stored in structure ‘order’, structure ‘customerInfo’, preparation and fulfilment time of order Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
7
Functions Function 1: loadMenu
Passes information of one menu item to the function ‘setFoodItems’ Function 2: setFoodItems Loads the menu of the restaurant Function 3: customerArrives Pushes the information of the customer on the queue ‘customerQueue’ (includes customer id and arrival time) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
8
Functions Function 4: placeOrder Generates token ID Randomizes
Number of items Item ID, Quantity of each item Calculates cost, and total cost Calculates time for placing order (based on randomization) Sends the order information to the kitchen Customer exits from the queue and waits at a table Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
9
Functions Function 5: orderGoesToKitchen Loads information of
Order placed Customer Calculates Preparation time for the order Order fulfilment time Updates the ‘listInKitchen’ list Function 6: dispatchOrders Sorts the ‘listInKitchen’ list based on fulfilment time Dispatches orders Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
10
Program struct customerInfo{ int custID; int arrivalTime;
}; //End of structure struct foodInfo{ int foodID; string foodName; float rate; }; //End of structure struct orders { int tokenID; int numberOfItems; int item[20]; int qty[20]; int cost[20]; float totalCost; int placingOrderTime; }; //End of structure struct kitchen { struct orders order; struct customerInfo cinfo; int orderfulfilmentTime; int preparationTime; }; //End of structure Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
11
Program class restaurant { private:
static int custID, tokenID, foodIndex; struct foodInfo food[100]; struct orders order; list<kitchen> listInKitchen; queue<customerInfo> customerQueue; public: void loadMenu(); void setFoodItems(int id, string name, float amount); void customerArrives(int time); void placeOrder(); void orderGoesToKitchen(); void dispatchOrders(); }; //End of class Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
12
Program int restaurant::custID = 0; int restaurant::tokenID = 1000;
int restaurant::foodIndex = 0; int generateRandom(int low, int high) { //Return a random number between low and high return ((rand() % (high-low+1)) + low); } //End of function Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
13
Program void restaurant::setFoodItems(int id, string name, float rate) { food[foodIndex].foodID = id; food[foodIndex].foodName = name; food[foodIndex].rate = rate; foodIndex++; } //End of function void restaurant::loadMenu() { setFoodItems(1,"Vada Pav",10.0); setFoodItems(2,"Uttappa",18.0); … setFoodItems(25,"Kothmir Wadi",26.0); } //End of function Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
14
Program void restaurant::customerArrives(int time) {
struct customerInfo cust; cust.custID = ++custID; cust.arrivalTime = time; customerQueue.push(cust); cout << "Customer: " << customerQueue.back().custID << ", " << customerQueue.back().arrivalTime << endl; } //End of function Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
15
Program void restaurant::placeOrder() {
int index=0, itemID, qty, i, low; order.tokenID = ++tokenID; int totalCost, ptime; // Code for placing order // Code for calculating placing order time // Code to display order info of customer } //End of function Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
16
Program //Place order for numberOfItems items. where numberOfItems<=6 order.numberOfItems = generateRandom(1,6); itemID = generateRandom(1,3); index = generateRandom(1,3); for (i=0;i<order.numberOfItems;i++) { itemID = itemID + index; qty = generateRandom(1,10); //Qty is from 1 to 10 order.item[i] = itemID; order.qty[i] = qty; order.cost[i] = food[itemID].rate * qty; totalCost = totalCost + order.cost[i]; } order.totalCost = totalCost; Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
17
Program //Calculate placing order time low = order.numberOfItems * 5;
ptime = generateRandom(low, low+15); if (listInKitchen.empty()) order.placingOrderTime = ptime + customerQueue.front().arrivalTime; else { if (customerQueue.front().arrivalTime > listInKitchen.back().order.placingOrderTime) else order.placingOrderTime = ptime + listInKitchen.back().order.placingOrderTime; } //End of else Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
18
Program //Display order info of customer
cout << "Order placed for customer " << customerQueue.front().custID << " with token " << order.tokenID << " and timetaken is " << ptime << endl; orderGoesToKitchen(); customerQueue.pop(); } //End of function placeOrder() Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
19
Program void restaurant::orderGoesToKitchen() { kitchen k; int pTime;
k.order = order; k.cinfo = customerQueue.front(); pTime = order.numberOfItems * 120; k.preparationTime = generateRandom(pTime,pTime+60) ; k.orderfulfilmentTime = k.preparationTime + order.placingOrderTime; listInKitchen.push_back(k); cout << "List in kitchen updated for token id: " << listInKitchen.back().order.tokenID << ", Preparation time: " << listInKitchen.back().preparationTime << endl << endl; } //End of function Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
20
Program bool compare(kitchen first, kitchen second) {
return (first.orderfulfilmentTime < second.orderfulfilmentTime); } //End of function void restaurant::dispatchOrders() { listInKitchen.sort(compare); int i; while (!listInKitchen.empty()) { cout << "Dispatched Token ID: " << listInKitchen.front().order.tokenID << " fulfilled at: " << listInKitchen.front().orderfulfilmentTime << endl; listInKitchen.pop_front(); } } //End of function Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
21
Program int main() { std::srand(std::time(0)); restaurant MVPRestaurant; int i, custarrive=0; MVPRestaurant.loadMenu(); for (i=1; i<=5; i++) { custarrive = custarrive + generateRandom(1,20); MVPRestaurant.customerArrives(custarrive); } cout << endl; MVPRestaurant.placeOrder(); MVPRestaurant.dispatchOrders(); return 0; } //End of main() Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
22
Thank you Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.