
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Display Table of Food Orders in a Restaurant in C++
Suppose we have an array orders, which represents the orders that customers have done in a restaurant. So, orders[i]=[cust_namei, table_numi, food_itemi] where cust_namei is the customer name, table_numi is the customers table number, and food_itemi is the item customer orders.
We have to return the restaurant's “display table”. Here the “display table” is a table whose row entries denote how many of each food item each table ordered. The first column will be the table number and the remaining columns correspond to each food item in alphabetical order. First row should be a header whose first column is “Table”, followed by the names of the foods.
So, if the input is like orders = [["Amal","3","Paratha"],["Bimal","10","Biryni"],["Amal","3","Fried Chicken"],["Raktim","5","Water"],["Raktim","5","Paratha"],["Deepak","3","Paratha"]], then the output will be [["Table","Biryni","Fried Chicken","Paratha","Water"],["3","0","1","2","0"],["5","0","0","1","1"],["10","1","0","0","0"]]
To solve this, we will follow these steps −
Define one map m
Define one set names
Define one set t
for each element it in order list
insert it[1] into t
ok := true
if ok is false, then −
v = an array after splitting it[2] using blank space
for each element x in v, do
(increase m[it[1], x] by 1)
insert x into names
Otherwise
(increase m[it[1], it[2]] by 1)
insert it[2] into names
Define one 2D array ret
Define an array temp and copy element from names
insert "Table" as the first element into temp
insert temp at the end of ret
for each element it in t, do
Define an array te
insert it at the end of te
for each element x in names, do
insert m[it, x] as string at the end of te
insert te at the end of ret
sort the array ret
return ret
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; void print_vector(vector<vector<string> > v){ cout << "["; for(int i = 0; i<v.size(); i++){ cout << "["; for(int j = 0; j <v[i].size(); j++){ cout << v[i][j] << ", "; } cout << "],"; } cout << "]"<<endl; } typedef long long int lli; class Solution { public: vector <string> split(string& s, char delimiter){ vector <string> tokens; string token; istringstream tokenStream(s); while(getline(tokenStream, token, delimiter)){ tokens.push_back(token); } return tokens; } static bool cmp(vector <string>& a, vector <string>& b){ lli an = stol(a[0]); lli bn = stol(b[0]); return an < bn; } vector<vector<string>> displayTable(vector<vector<string>>& o) { map <string, map < string, int> >m; set <string> names; set <string> t; for(auto &it : o){ t.insert(it[1]); bool ok = true; if(!ok){ vector <string> v = split(it[2], ' '); for(auto& x : v){ m[it[1]][x]++; names.insert(x); } } else{ m[it[1]][it[2]]++; names.insert(it[2]); } } vector < vector <string> > ret; vector <string> temp(names.begin(), names.end()); temp.insert(temp.begin(), "Table"); ret.push_back(temp); for(auto& it : t){ vector <string> te; te.push_back(it); for(auto& x : names){ te.push_back(to_string(m[it][x])); } ret.push_back(te); } sort(ret.begin() + 1, ret.end(), cmp); return ret; } }; main(){ Solution ob; vector<vector<string>> v = {{"Amal","3","Paratha"},{"Bimal","10","Biryni"},{"Amal","3","Fried Chicken"},{"Raktim","5","Water"},{"Raktim","5","Paratha"},{"Deepak"," 3","Paratha"}}; print_vector(ob.displayTable(v)); }
Input
{{"Amal","3","Paratha"},{"Bimal","10","Biryni"},{"Amal","3","Fried Chicken"},{"Raktim","5","Water"},{"Raktim","5","Paratha"},{"Deepak"," 3","Paratha"}}
Output
[[Table, Biryni, Fried Chicken, Paratha, Water, ],[3, 0, 1, 2, 0, ],[5, 0, 0, 1, 1, ],[10, 1, 0, 0, 0, ],]
- Related Articles
- Apply Discount Every n Orders in C++
- Write a SQL query to count the number of duplicate TRANSACTION_ID in an ORDERS DB2 table
- Display all fields of a table in MySQL?
- Write a DB2 query to find out all the duplicate INVOICE_ID in ORDERS DB2 table?
- Write the DB2 SQL query to find the third highest ORDER_TOTAL in a ORDERS DB2 table
- Count of matrices (of different orders) with given number of elements in C++
- How to display a table body in HTML
- How to find and display the Multiplication Table in C#?
- Write the syntax to declare a scrollable cursor on the ORDERS DB2 table.
- Display result of a table into a temp table with MySQL?
- How do we display a table cell in HTML
- Display table records from a stored procedure in MySQL
- Display sum in last row of table using MySQL?
- How will you create a new TRIGGER on the ORDERS DB2 table? Give the syntax of TRIGGER
- MySQL query to display structure of a table
