Multiple Choice Selections involving Vectors - stdvector

Does anyone know how it is possible to create a multiple choice when dealing with elements in vectors? The purpose of my code is to output a series of vector elements and then the User choose a card which is defined by class card. This is my code so far:
while(true){
cout << players[0] << endl;
cout << "What to Do: ";
string input; cin >> input;
if (input == "?") {
for(int i = 1, len = players.size(); i < len; ++i) cout << players[i] << endl;
}
if (input == "quit") break;
card hold;
switch(input){
case "a" : hold = players.back(); players[6] = players[0]; players[0] = hold;
case "b" : hold = players.back(); players[6] = players[1]; players[1] = hold;
case "c" : hold = players.back(); players[6] = players[2]; players[2] = hold;
case "d" : hold = players.back(); players[6] = players[3]; players[3] = hold;
case "e" : hold = players.back(); players[6] = players[4]; players[4] = hold;
case "f" : hold = players.back(); players[6] = players[5]; players[5] = hold;
case "g" : hold = players.back(); players[6] = hold;
}

Related

Expansion of periodic mesh

I have send this trough the mailing list but it seems not to be fully working at the moment so I am posting it here instead. The issue I am having is that I am trying to expand a periodic mesh and have it returned in a new c3t3 structure instead of having to save a .mesh file and load it afterwards. For this I have tried modifying the File_medit.h found in the Periodic_3_mesh_3/IO folder but am having a few issues. First of all, the tetrahedrals seem to not be properly added when I call the build_triangulation function although the finite_cell vector has all collected values in it. I am having a similar issue with the border_facets, although in this case I am also not sure what data I am supposed to place in surface_patch_id and if perhaps this is causing the issues. I have included the code I am using bellow. Any help is appreciated.
Regards, Kim
/* ==========================================================================
* Based on CGAL's File_medit.h. See CGAL Periodic_3_mesh_3/IO.
* ==========================================================================*/
#ifndef CGAL_PERIODIC_REPLICATOR_H
#define CGAL_PERIODIC_REPLICATOR_H
#include <CGAL/license/Periodic_3_mesh_3.h>
#include <CGAL/Mesh_3/io_signature.h>
#include <CGAL/Mesh_3/tet_soup_to_c3t3.h>
//
#include <CGAL/array.h>
#include <CGAL/assertions.h>
#include <CGAL/IO/File_medit.h>
#include <boost/unordered_map.hpp>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <cstring>
#include <sstream>
#include <cstring>
#include <map>
namespace CGAL {
namespace periodic_replicator {
/**
* \brief expands a periodic mesh to the c3t3 variable
* \param c3t3_periodic the mesh
* \param occurrence_count the number of copies that are printed
* \param distinguish_copies if set to `true`, each copy is assigned a unique color.
* Otherwise, all domains are drawn with subdomain index-based colors.
* \param rebind if set to `true`, labels of cells are rebinded into [1..nb_of_labels]
* \param c3t3 the output mesh
*/
template <class C3T3_Periodic, class C3T3, bool rebind, bool no_patch> ////template <class C3T3_Periodic>
void replicate_periodic_mesh(const C3T3_Periodic& c3t3_periodic,
int occurrence_count[3],
const bool distinguish_copies,
C3T3& c3t3)
{
#ifdef CGAL_MESH_3_IO_VERBOSE
std::cerr << "Output to medit:\n";
#endif
CGAL_precondition(c3t3_periodic.triangulation().is_1_cover());
typedef CGAL::Mesh_3::Medit_pmap_generator<C3T3_Periodic, rebind, no_patch> Generator;
typedef typename Generator::Cell_pmap Cell_pmap;
typedef typename Generator::Facet_pmap Facet_pmap;
typedef typename Generator::Facet_pmap_twice Facet_pmap_twice;
typedef typename Generator::Vertex_pmap Vertex_pmap;
//Generator().print_twice();
Cell_pmap cell_pmap(c3t3_periodic);
Facet_pmap facet_pmap(c3t3_periodic, cell_pmap);
Facet_pmap_twice facet_pmap_twice(c3t3_periodic, cell_pmap);
Vertex_pmap vertex_pmap(c3t3_periodic, cell_pmap, facet_pmap);
// const Facet_index_property_map_twice& facet_twice_pmap = Facet_index_property_map_twice(),
const bool print_each_facet_twice = false;//Generator().print_twice();
CGAL_precondition(occurrence_count[0] >= 1 && occurrence_count[1] >= 1 && occurrence_count[2] >= 1);
// Periodic typedef
typedef typename C3T3_Periodic::Triangulation Triangulation_periodic;
typedef Triangulation_periodic Tr_periodic;
typedef typename Tr_periodic::Bare_point Bare_point;
typedef typename Tr_periodic::Weighted_point Weighted_point;
typedef typename C3T3_Periodic::Vertex_handle Vertex_handle;
typedef typename C3T3_Periodic::Cell_handle Cell_handle;
typedef typename Tr_periodic::Vertex_iterator Vertex_iterator;
typedef typename C3T3_Periodic::Facet_iterator Facet_iterator;
typedef typename C3T3_Periodic::Cell_iterator Cell_iterator;
typedef typename Tr_periodic::Offset Offset;
const Triangulation_periodic& tr_periodic = c3t3_periodic.triangulation();
// Non-periodic typedef
typedef typename C3T3::Triangulation Triangulation;
typedef Triangulation Tr;
typedef typename Tr::Point Point_3;
typedef std::array<int, 3> Facet; // 3 = id
typedef std::array<int, 5> Tet_with_ref; // first 4 = id, fifth = reference
//
std::vector<Point_3> points;
//std::map<Facet, typename Tr::Cell::Surface_patch_index> border_facets_;
std::map<Facet, typename C3T3::Surface_patch_index> border_facets;
std::vector<Tet_with_ref> finite_cells;
//
int number_of_vertices = static_cast<int>(tr_periodic.number_of_vertices());
int number_of_facets = static_cast<int>(c3t3_periodic.number_of_facets());
int number_of_cells = static_cast<int>(c3t3_periodic.number_of_cells());
// number of reproductions over the different axes
int Ox_rn = ceil(occurrence_count[0]);
int Oy_rn = ceil(occurrence_count[1]);
int Oz_rn = ceil(occurrence_count[2]);
int occ_mult = Ox_rn * Oy_rn * Oz_rn;
#ifdef CGAL_PERIODIC_3_MESH_3_VERBOSE
std::cerr << "Expanding periodic mesh... " << std::endl;
std::cerr << "occurrences over each axis: "
<< Ox_rn << " " << Oy_rn << " " << Oz_rn << std::endl;
std::cerr << number_of_vertices << " vertices" << std::endl;
std::cerr << number_of_facets << " facets" << std::endl;
std::cerr << number_of_cells << " cells" << std::endl;
#endif
std::ofstream os("output_control.mesh");
os << std::setprecision(17);
// Vertices
int medit_number_of_vertices = (Ox_rn + 1) * (Oy_rn + 1) * (Oz_rn + 1) * number_of_vertices;
os << "MeshVersionFormatted 1\n"
<< "Dimension 3\n"
<< "Vertices\n" << medit_number_of_vertices << std::endl;
// Build the set of points that is needed to draw all the elements.
// On each axis, we repeat n+1 times the point, where 'n' is the number of
// instances of the mesh that will be printed over that axis. This is because
// a cell 'c' might have point(c,i) that is equal to v with an offset 2
boost::unordered_map<Vertex_handle, int> V;
int inum = 1; // '1' because medit ids start at 1
for(int i=0; i<=Oz_rn; ++i) {
for(int j=0; j<=Oy_rn; ++j) {
for(int k=0; k<=Ox_rn; ++k) {
for(Vertex_iterator vit = tr_periodic.vertices_begin(); vit != tr_periodic.vertices_end(); ++vit) {
if(i == 0 && j == 0 && k == 0)
V[vit] = inum++;
const Offset off(k, j, i);
const Weighted_point& p = tr_periodic.point(vit);
const Bare_point bp = tr_periodic.construct_point(p, off);
int id;
if(i >= 1 || j >= 1 || k >= 1)
id = 7;
else
id = tr_periodic.off_to_int(off);
os << CGAL::to_double(bp.x()) << ' '
<< CGAL::to_double(bp.y()) << ' '
<< CGAL::to_double(bp.z()) << ' ';
if(!distinguish_copies || occ_mult == 1)
os << get(vertex_pmap, vit) << '\n';
else
os << 32 * id + 1 << '\n';
points.push_back(Point_3(CGAL::to_double(bp.x()),
CGAL::to_double(bp.y()),
CGAL::to_double(bp.z())));
}
}
}
}
// Triangles
int medit_number_of_triangles = occ_mult * number_of_facets;
if(print_each_facet_twice)
medit_number_of_triangles *= 2;
os << "Triangles\n" << medit_number_of_triangles << std::endl;
for(int i=0; i<Oz_rn; ++i) {
for(int j=0; j<Oy_rn; ++j) {
for(int k=0; k<Ox_rn; ++k) {
const Offset off(k, j, i);
for(Facet_iterator fit = c3t3_periodic.facets_begin(); fit != c3t3_periodic.facets_end(); ++fit) {
typename Tr::Cell::Surface_patch_index surface_patch_id;
Facet facet;
for(int l=0; l<4; ++l) {
if(l == fit->second)
continue;
Cell_handle c = fit->first;
Vertex_handle v = c->vertex(l);
const Offset combined_off = tr_periodic.combine_offsets(
off, tr_periodic.int_to_off(c->offset(l)));
const int vector_offset = combined_off.x() +
combined_off.y() * (Ox_rn + 1) +
combined_off.z() * (Ox_rn + 1) * (Oy_rn + 1);
const int id = vector_offset * number_of_vertices + V[v];
CGAL_assertion(1 <= id && id <= medit_number_of_vertices);
os << id << " ";
//facet[l] = id;
int temp_ = id;
facet[l] = temp_;
}
// For multiple copies, color to distinguish copies rather than to distinguish subdomains
//int temptest;
if(!distinguish_copies || occ_mult == 1){
os << get(facet_pmap, *fit) << '\n';
} else {
os << 1 + k + 3*j + 9*i << '\n';
}
surface_patch_id.first = 0;
surface_patch_id.second = 1;
border_facets.insert(std::make_pair(facet, surface_patch_id));
// Print triangle again if needed
if(print_each_facet_twice) {
for(int l=0; l<4; ++l) {
if(l == fit->second)
continue;
Cell_handle c = fit->first;
Vertex_handle v = c->vertex(l);
const Offset combined_off = tr_periodic.combine_offsets(
off, tr_periodic.int_to_off(c->offset(l)));
const int vector_offset = combined_off.x() +
combined_off.y() * (Ox_rn + 1) +
combined_off.z() * (Ox_rn + 1) * (Oy_rn + 1);
const int id = vector_offset * number_of_vertices + V[v];
CGAL_assertion(1 <= id && id <= medit_number_of_vertices);
os << id << " ";
int temp_ = id;
facet[l] = temp_;
}
if(!distinguish_copies || occ_mult == 1)
os << get(facet_pmap_twice, *fit) << '\n';
else
os << 1 + k + 3*j + 9*i << '\n';
//surface_patch_id = ???;
//border_facets.insert(std::make_pair(facet, surface_patch_id));
}
}
}
}
}
// Tetrahedra
os << "Tetrahedra\n" << occ_mult * number_of_cells << std::endl;
for(int i=0; i<Oz_rn; ++i) {
for(int j=0; j<Oy_rn; ++j) {
for(int k=0; k<Ox_rn; ++k) {
const Offset off(k, j, i);
for(Cell_iterator cit = c3t3_periodic.cells_begin(); cit !=c3t3_periodic.cells_end(); ++cit) {
Tet_with_ref t;
for(int l=0; l<4; ++l) {
const Offset combined_off = tr_periodic.combine_offsets(
off, tr_periodic.int_to_off(cit->offset(l)));
const int vector_offset = combined_off.x() +
combined_off.y() * (Ox_rn + 1) +
combined_off.z() * (Ox_rn + 1) * (Oy_rn + 1);
const int id = vector_offset * number_of_vertices + V[cit->vertex(l)];
CGAL_assertion(1 <= id && id <= medit_number_of_vertices);
os << id << " ";
t[l] = id - 1;
}
// For multiple copies, color to distinguish copies rather than to distinguish subdomains
if(!distinguish_copies || occ_mult == 1) {
os << get(cell_pmap, cit) << '\n';
t[4] = get(cell_pmap, cit);
} else {
os << 1 + k + 3*j + 9*i << '\n';
t[4] = 1 + k + 3*j + 9*i;
}
finite_cells.push_back(t);
}
}
}
}
os << "End" << std::endl;
//
//std::vector<typename Tr::Vertex_handle> vertices(0);
std::vector<typename Tr::Vertex_handle> vertices(points.size() + 1);
bool is_well_built = build_triangulation<Tr, true>(c3t3.triangulation(), points, finite_cells, border_facets, vertices, true/*verbose*/, false/*replace_domain*/);
// Check
std::cout << points.size() << " points" << std::endl;
std::cout << border_facets.size() << " border facets" << std::endl;
std::cout << finite_cells.size() << " cells" << std::endl;
std::cout << c3t3.number_of_facets() << " border facets expanded c3t3" << std::endl;
std::cout << c3t3.number_of_cells() << " cells expanded c3t3" << std::endl;
}
#ifdef CGAL_MESH_3_IO_VERBOSE
std::cerr << "done.\n";
#endif
} // namespace periodic_replicator
} // namespace CGAL
#endif // CGAL_PERIODIC_REPLICATOR_H

I get garbage value on this jagged array

I get garbage value on this jagged array, that takes input size from another array's last value which must be -111(if not ten add one index to put in -111).
i know i have missed some cout statements but dont know why i get garbage vals
'''
int jaggedArr(int** arr2, int r, int c)
{
int* numbers = nullptr;
numbers = new int[r]; /// array to store no of columns
int** jagArr = new int* [r]; /// jagged array
for (int i = 0; i < r; i++)
{
int tempNum;
for (int j = 0; j < c; j++)
{ //store size of cols in arr4 to put int new array (arr3)
if (arr2[i][j] == -111)
{
tempNum = j;
numbers[i] = tempNum;//if -111 is present then dont change size just copy
}
else if (arr2[i][j] != -111)
{
tempNum = j + 1;
numbers[i] =tempNum;//else if -111 is not present then dont change size just copy
}
tempNum = 0;
}
}
for (int i = 0; i < r; i++)
{
jagArr[i] = new int[numbers[i]];
}
for (int i = 0; i < r; i++)
{
for (int j = 0; j < 10; j++)//remove 10
{
jagArr[i] = new int[numbers[i]];
}
}
cout << "Showing all the Inputed data in a matrix form" << endl;
for (int i = 0; i < r; i++) {
for (int j = 0; j < numbers[i]; j++)
{
//if (jagArr[i][j] >= 0 && jagArr[i][j] <= 9)
//{
// cout << jagArr[i][j] << " |";
//}
//else if (jagArr[i][j] == -111)
//{
// cout << jagArr[i][j] << "|";
//}
//else
//{
// cout << jagArr[i][j] << " |";
//}
cout << jagArr[i][j];
}
cout << "\n";
}
return **arr2;
}
'''

How to generate binary tree dot file for Graphviz from C++

I have implemented a Binary Search Tree in C++ which support dynamically creating and deleting nodes. To visualize the tree, I firstly tried displaying edges with / and \. However, this gives really awful visualization, as the position of / and \ needs to be calculated precisely. The current figures are as follows:
So I found a tool called Graphviz. The raw language supported by Graphviz is dot language, which I am not familiar with.
I read the documentation and found the dot language easy to write and read, but I still want to use my C++ code to generate the tree as this contains much content such as inserting according to user's input.
Is there any chance to use some function to generate the dot file?
The code of my binary tree:
//BTNode.h
#include <iostream>
using namespace std;
template<class T>
struct BTNode{
BTNode(){
lChild = rChild = NULL;
}
BTNode(const T& x){
element = x;
lChild = rChild = NULL;
}
BTNode(const T& x, BTNode<T>* l, BTNode<T>* r){
element = x;
lChild = l;
rChild = r;
}
T element;
int digit, row;
BTNode<T>* lChild, *rChild;
};
//BSTree.h
#include"ResultCode.h"
#include "BTNode.h"
#include "seqqueue.h"
#include <math.h>
template <class T>
class BSTree
{
public:
BSTree(){ root = NULL; }
ResultCode SearchByRecursion(T& x)const;
ResultCode Insert(T& x);
ResultCode Remove(T& x);
void InOrder(void(*Visit)(T& x));
ResultCode SearchByIteration(T& x);
void GradeOrder(void(*Visit)(T &x), BTNode<T> *t, int height);
BTNode<T>* root;
void printSpace(int num);
int GetHeight();
int GetHeight(BTNode<T> *t);
int **A;
private:
ResultCode SearchByRecursion(BTNode<T> *p, T& x)const;
void InOrder(void(*Visit)(T& x), BTNode<T> *t);
};
template <class T>
ResultCode BSTree<T>::SearchByRecursion(T &x)const{
return SearchByRecursion(root, x);
}
template <class T>
ResultCode BSTree<T>::SearchByRecursion(BTNode<T> *p, T& x)const{
if (!p) return NotPresent;
else if (x < p->element) return SearchByRecursion(p->lChild, x);
else if (x > p->element) return SearchByRecursion(p->rChild, x);
else{
x = p->element;
return Success;
}
}
template <class T>
ResultCode BSTree<T>::SearchByIteration(T& x){
BTNode<T> *p = root;
while (p)
if (x < p->element) p = p->lChild;
else if (x > p->element) p = p->rChild;
else{
x = p->element;
return Success;
}
return NotPresent;
}
template<class T>
ResultCode BSTree<T>::Insert(T& x){
BTNode<T> *p = root, *q = NULL;
while (p){
q = p;
if (x < p->element) p = p->lChild;
else if (x > p->element) p = p->rChild;
else { x = p->element; return Duplicate; }
}
p = new BTNode<T>(x);
if (!root) root = p;
else if (x < q->element) q->lChild = p;
else q->rChild = p;
return Success;
}
template <class T>
ResultCode BSTree<T>::Remove(T& x){
BTNode<T> *c, *s, *r, *p = root, *q = NULL;
while (p && p->element != x){
q = p;
if (x < p->element) p = p->lChild;
else p = p->rChild;
}
if (!p) return NotPresent;
x = p->element;
if (p->lChild&&p->rChild)
{
s = p->rChild;
r = p;
while (s->lChild){
r = s; s = s->lChild;
}
p->element = s->element;
p = s; q = r;
}
if (p->lChild)
c = p->lChild;
else c = p->rChild;
if (p == root)
root = c;
else if (p == q->lChild)
q->lChild = c;
else q->rChild = c;
delete p;
return Success;
}
template <class T>
void BSTree<T>::InOrder(void(*Visit)(T &x)){
InOrder(Visit, root);
}
template <class T>
void BSTree<T>::InOrder(void(*Visit)(T &x), BTNode<T> *t){
if (t){
InOrder(Visit, t->lChild);
Visit(t->element);
InOrder(Visit, t->rChild);
}
}
template <class T>
void BSTree<T>::GradeOrder(void(*Visit)(T &x), BTNode<T> *t, int height)
{
A = new int*[height];
for (int i = 0; i < height; i++){
A[i] = new int[(int)pow(2, height) - 1];
}
for (int i = 0; i < height; i++)
for (int j = 0; j < (int)pow(2, height) - 1; j++){
A[i][j] = -1;
}
SeqQueue<BTNode<T>*> OrderQueue(10);
BTNode<T> * loc = t;
loc->row = 0;
loc->digit = 0;
if (loc){
OrderQueue.EnQueue(loc);
A[loc->row][loc->digit] = loc->element;
}
while (!OrderQueue.IsEmpty())
{
OrderQueue.Front(loc);
OrderQueue.DeQueue();
if (loc->lChild)
{
A[(loc->row) + 1][2 * (loc->digit)] = loc->lChild->element;
loc->lChild->row = (loc->row) + 1;
(loc->lChild->digit) = (loc->digit) * 2;
OrderQueue.EnQueue(loc->lChild);
}
if (loc->rChild)
{
A[(loc->row) + 1][2 * (loc->digit) + 1] = loc->rChild->element;
loc->rChild->row = (loc->row) + 1;
(loc->rChild->digit) = (loc->digit) * 2 + 1;
OrderQueue.EnQueue(loc->rChild);
}
}
cout << endl;
int total = (int)(pow(2, height)) - 1;
for (int i = 0; i < height; i++){
if (i != 0){
cout << endl;
}
int space1 = (total / (int)(pow(2, i + 1)));
int space2;
if (i == 0){
space2 = 0;
}
else{
space2 = (total - 2 * space1 - (int)pow(2, i)) / (int)(pow(2, i) - 1);
}
printSpace(space1);
for (int j = 0; j < (int)pow(2, i); j++){
if (A[i][j] != -1){
cout << A[i][j];
}
else{
cout << " ";
}
printSpace(space2);
}
printSpace(space1);
cout << endl;
}
}
template <class T>
void BSTree<T>::printSpace(int num){
for (int i = 0; i < num; i++){
cout << " ";
}
}
template<class T>
int BSTree<T>::GetHeight()
{
return GetHeight(root);
}
template<class T>
int BSTree<T>::GetHeight(BTNode<T> *t)
{
if (!t)return 0;
if ((!t->lChild) && (!t->rChild)) return 1;
int lHeight = GetHeight(t->lChild);
int rHeight = GetHeight(t->rChild);
return (lHeight > rHeight ? lHeight : rHeight) + 1;
}
template <class T>
void Visit(T& x){
cout << x << " ";
}
//main.cpp
#include <iostream>
#include "BSTree4.h"
#include<Windows.h>
int getDigit(int x);
int main(){
BSTree<int> bt;
int number;
// char choice;
cout << "Welcome to BSTree System, to begin with, you need to create a tree!(Press enter to continue...)" << endl;
getchar();
cout << "Please enter the size of the Binary Search Tree:";
cin >> number;
int *ToBeInserted = new int[number];
cout << "Enter the number of each Node(size:" << number << "):";
for (int i = 0; i < number; i++){
cin >> ToBeInserted[i];
}
cout << "OK,now the tree will be created!" << endl;
for (int i = 0; i < number; i++){
cout << "Inserting Node " << i;
for (int k = 0; k < 3; k++){
cout << ".";
//Sleep(200);
}
showResultCode(bt.Insert(ToBeInserted[i]));
//Sleep(500);
}
cout << "Done." << endl;
//Sleep(500);
int height = bt.GetHeight();
bt.GradeOrder(Visit, bt.root,height);
int a;
cout << "please enter the number to search:";
cin>>a;
showResultCode(bt.SearchByRecursion(a));
bt.GradeOrder(Visit, bt.root,height);
if (bt.SearchByRecursion(a) == 7){
cout << "Now delete the number" << "..." << endl;
showResultCode(bt.Remove(a));
bt.GetHeight();
cout << "Deleted!Now the tree is:" << endl;
bt.GradeOrder(Visit, bt.root, height);
bt.InOrder(Visit);
cout << endl;
}
return 0;
}
//resultcode.h
#include<iostream>
using namespace std;
enum ResultCode{ NoMemory, OutOfBounds, Underflow, Overflow, Failure,
NotPresent, Duplicate, Success };
void showResultCode(ResultCode result)
{
int r = (int)result;
switch (result)
{
case 0:cout << "NoMemory" << endl; break;
case 1:cout << "OutOfBounds" << endl; break;
case 2:cout << "Underflow" << endl; break;
case 3:cout << "Overflow" << endl; break;
case 4:cout << "Failure" << endl; break;
case 5:cout << "NotPresent" << endl; break;
case 6:cout << "Duplicate" << endl; break;
case 7:cout << "Success" << endl; break;
default: cout << "Exception occured:unknown resultcode" << endl;
}
}
Update: I have solved the problem myself, check the answer below.
The key elements in dot language file in this problem are nodes and edges. Basically the dot file structure for a binary tree would be like the following:
digraph g {
//all the nodes
node0[label="<f0>|<f1> value |<f2>"]
node1[label="<f0>|<f1> value |<f2>"]
node2[label="<f0>|<f1> value |<f2>"]
...
//all the edges
"node0":f2->"node4":f1;
"node0":f0->"node1":f1;
"node1":f0->"node2":f1;
"node1":f2->"node3":f1;
...
}
The following output of the dot file can be used to understand the structure:
Explanation for the dot file:
For the node part node0[label="<f0>|<f1> value |<f2>"] means the node called node0 has three parts: <f0> is the left part, <f1> is the middle part with a value, <f2> is the right part. This just corresponds to leftchild, value and rightchild in a binary node.
For the edges part, "node0":f2->"node4":f1; means the right part of node0(i.e.<f2>) points to the middle part of node4 (i.e. <f1>).
Therefore, the way to generate the dot file is simply through a traverse of a binary tree. Any method is fine. (BFS,DFS...) All we need is to add the code to write the nodes and corresponding edges into file when we do the traverse. I personally used BFS with level order traverse of a binary tree to implement which is shown below as a function called showTree.
void showTree(BSTree<int> &bst,int total,int *Inserted){
char filename[] = "D:\\a.gv"; // filename
ofstream fout(filename);
fout << "digraph g{" << endl;
fout << "node [shape = record,height = .1];" << endl;
SeqQueue<BTNode<int>*> OrderQueue(1000);
BTNode<int> * loc = bst.root;
loc->row = 0;
loc->digit = 0;
int num = 0;
if (loc){
OrderQueue.EnQueue(loc);
loc->ID = num++;
fout << " node" << loc->ID << "[label = \"<f0> |<f1>" << loc->element << "|<f2>\"];" << endl;
}
while (!OrderQueue.IsEmpty())
{
OrderQueue.Front(loc);
OrderQueue.DeQueue();
if (loc->lChild)
{
loc->lChild->row = (loc->row) + 1;
(loc->lChild->digit) = (loc->digit) * 2;
OrderQueue.EnQueue(loc->lChild);
loc->lChild ->ID= (num++);
fout << " node" << loc->lChild->ID << "[label = \"<f0> |<f1>" << loc->lChild->element << "|<f2>\"];" << endl;
//cout << loc->ID;
}
if (loc->rChild)
{
loc->rChild->row = (loc->row) + 1;
(loc->rChild->digit) = (loc->digit) * 2 + 1;
OrderQueue.EnQueue(loc->rChild);
loc->rChild->ID = (num++);
fout << " node" << loc->rChild->ID << "[label = \"<f0> |<f1>" << loc->rChild->element << "|<f2>\"];" << endl;
//cout << loc->ID;
}
}
//begin to draw!
SeqQueue<BTNode<int>*> OrderQueue2(1000);
BTNode<int> * loc2 = bst.root;
loc2->row = 0;
loc2->digit = 0;
if (loc2){
OrderQueue2.EnQueue(loc2);
}
while (!OrderQueue2.IsEmpty())
{
OrderQueue2.Front(loc2);
OrderQueue2.DeQueue();
if (loc2->lChild)
{
loc2->lChild->row = (loc2->row) + 1;
(loc2->lChild->digit) = (loc2->digit) * 2;
OrderQueue2.EnQueue(loc2->lChild);
cout << "\"node" << loc2->ID << "\":f0->\"node" << loc2->lChild->ID << "\":f1;" << endl;
cout << loc2->lChild->element << endl;
fout << "\"node" << loc2->ID << "\":f0->\"node" << loc2->lChild->ID << "\":f1;" << endl;
}
if (loc2->rChild)
{
loc2->rChild->row = (loc2->row) + 1;
(loc2->rChild->digit) = (loc2->digit) * 2 + 1;
OrderQueue2.EnQueue(loc2->rChild);
cout << "\"node" << loc2->ID << "\":f2->\"node" << loc2->rChild->ID << "\":f1;" << endl;
cout << loc2->rChild->element << endl;
fout << "\"node" << loc2->ID << "\":f2->\"node" << loc2->rChild->ID << "\":f1;" << endl;
}
}
fout << "}" << endl;
}
And the final output:

how to return longest words from an array?

given 2 arrays wrds[] , chars[] as an input to a function such that
wrds[] = [ "abc" , "baa" , "caan" , "an" , "banc" ]
chars[] = [ "a" , "a" , "n" , "c" , "b"]
Function should return the longest word from words[] which can be constructed from the chars in chars[] array.
for above example - "caan" , "banc" should be returned
Note: Once a character in chars[] array is used, it cant be used again.
eg: words[] = [ "aat" ]
characters[] = [ "a" , "t" ]
then word "aat" can't be constructed, since we've only 1 "a" in chars[].
There are kinds of anwers online but they are not written in Objective C.Can anyone help me solve this question in OC?
First, walk through the word array, one word at a time, throwing out all the words that can't be formed from the second array. To do that, for each word, walk through the characters of the word, throwing out that character from the second array. If we come to a character that's not in the second array, that word can't be formed from those characters.
Now we have an array consisting solely of the words that can be formed from those characters. Now sort that array by word length, longest first. Now start walking the array, looking at the length of each word. When that length value changes, stop; you have found all the longest words.
// Program do to do the same in C++
#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;
using std::vector;
vector<char*> match(char** words, int size, char* chars, map<char,int> &second)
{
vector<char*> res;
std::map<char,int> mapi = second;
int currsize = 0;
for(int i = 0; i < size ; i++){
char* wo;
wo = words[i];
int s= 0;
for( s=0; wo[s] != '\0'; s++){
}
if(s < currsize) {
//No need to iterate if already found a bigger word
//continue to see if the next word if bigger of the same size as currsize
continue;
}
// iterate through the map to see if all the letters present in the first array
bool found = true;
for(int j = 0; j <s ; j++){
map<char, int>::iterator it = mapi.find(wo[j]);
if(it == mapi.end()) {
found= false;
break;
}
}
if(!found) {
continue;
}
if(s > currsize) {
//remove the past res as found a bigger one
res.clear();
}
//Store this word in the vector as it is one of the biggest word so far
res.push_back(wo);
currsize = s;
}
return res;
}
int main()
{
map<char, int> leters;
char* words[5] = {"adc", "baa", "caan", "daanns", "banc"};
char ch1[]= {'a', 'a', 'n', 'c', 'b'};
int chsize = sizeof(ch1);
// put the ch1 chars in a map
for(int i = 0; i < chsize; i++) {
map<char,int>::iterator it =leters.find(ch1[i]);
if(it != leters.end()) {
it->second = it->second+1;
} else {
leters.insert(make_pair(ch1[i], 1));
}
}
char* chars = ch1;
vector<char*>v = match(words, 5, ch1, leters);
for(vector<char*>::iterator it = v.begin(); it != v.end(); it++) {
// it will print the result
cout << *it << endl;
}
return 0;
}

Retrieving and manipulating data from file

The following code is supposed to retrieve the data related to the players info, sort it out and then rewrite the file now organized. Going to give an example of the files.
Original layout:
3
2 2
John 33 M 5
Anna 20 F 2
Rody 23 M 1
What it has to look like after the code:
3
2 2
Rody 23 M 1
Anna 20 F 2
John 33 M 5
I made the following code:
vector<string> playerScoresFromFile(const string filename) //Gets each one of those lines with the name, ..., and score of the person
{
int dim = filename[7] - '0'; // char to integer
vector<string> vec;
string line;
ifstream fin (filename.c_str());
for (int i = 0; i < dim + 1; i++)
{
getline(fin, line);
}
while(! fin.eof())
{
getline(fin, line);
vec.push_back(line);
}
return vec;
}
vector< vector<int> > readBoardFromFile(const string filename) //gets the board from the file (first 3 numbers)
{
int dim = filename[7] - '0'; // char to integer
string line;
vector< vector<int> >vec(dim, vector<int>(dim));
ifstream fin (filename.c_str());
int i = 0;
int j, k;
while(i < dim)
{
getline(fin, line);
int sizeOfLine = line.length();
if (line[0] == '\0')
{
break;
}
else
{
for (j = 0, k = 0; j < (sizeOfLine / 3); j++, k += 3)
{
string elementOfVectorStr = (line.substr(k,3));
int elementOfVectorInt = stringToInt(elementOfVectorStr);
if (abs(elementOfVectorInt) > 100) // when the element is a " ", the corresponding integer is always
{ // a very large number, positive or negative
elementOfVectorInt = 0;
}
vec[i][j] = elementOfVectorInt;
}
}
i++;
}
return vec;
}
vector<string> sortPlayersByTime (vector<string> &vec) // Creates a substring of the string extracted by "playerScoresFromFile" and analyses the times (Which are the last numbers to the right)
{
vector<int> timesInt(vec.size());
for (size_t i = 0; i < vec.size(); i++)
{
string str = vec[i];
timesInt[i] = stringToInt(str.substr(26));
}
for (size_t i = 0; i < vec.size() - 1; i++)
{
if(timesInt[i] > timesInt[i+1])
{
swap(vec[i], vec[i+1]);
}
}
return vec;
}
bool isOrdered (const vector<string> vec) //Checks if the vector is ordered
{
vector<int> timesInt(vec.size());
for (size_t i = 0; i < vec.size(); i++)
{
string str = vec[i];
timesInt[i] = stringToInt(str.substr(26));
}
for (size_t i = 0; i < vec.size() - 1; i++)
{
if(timesInt[i] > timesInt[i+1])
{
return false;
}
}
return true;
}
void writeBoardToFile(vector< vector<int> >&vec, string filename) //Rewrites the board to the file (Those first 3 numbers of the file)
{
ofstream fout(filename.c_str());
for (size_t i = 0; i < vec.size(); i++)
{
for (size_t j = 0; j < vec.size(); j++)
{
if(vec[i][j] != 0)
{
fout << setw(3) << vec[i][j];
}
else
{
fout << setw(3) << " ";
}
}
fout << endl;
}
fout << endl;
}
void vec_to_file(vector<string> vec, string filename) //Rewrites the vector to the file
{
ofstream fout(filename, ios::app);
for (int i = 0; i < vec.size(); i++)
{
fout << vec[i] <<endl;
}
}
void displayFile (string filename) //Displays the final board to check if it worked
{
vector<string> vec;
string line;
ifstream myfile (filename);
while ( ! myfile.eof() )
{
getline (myfile, line);
vec.push_back(line);
}
for (size_t i = 0; i < vec.size(); i++)
{
cout << vec[i] <<endl;
}
}
int main()
{
vector< vector<int> > vec = readBoardFromFile("puzzle_2x2_001.txt");
vector<string> vecxz = playerScoresFromFile("puzzle_2x2_001.txt");
writeBoardToFile(vec, "puzzle_2x2_001.txt"); //Writes the board to the file
while (! isOrdered(vecxz)) //This loop should run while they haven't been sorted out, but the program crashes here and I have no idea why.
{
sortPlayersByTime(vecxz);
}
//vec_to_file(vecxy, "puzzle_2x2_001.txt"); //Should write the vector to the file upon sorting them out successfully.
cin.get();
}
My problem is the program crashes everytime it gets to the while(! isOrdered(vecxz)) loop but I have no idea why. Can anyone give me a hand? I'd be thankful :)