Trouble with a recursive algorithm and pointers - c++

So I'm trying to make an algorithm that starts at the first "room" and then recursively goes outward and starts deleting all rooms from the outside in. A room is a struct with 4 "doors" (pointers to rooms): North, South, East, and West.
The function takes two arguments: pointer to the current room and a char (to identify the direction: North, South, East, or West).
Here is my logic for the algorithm (roomDelete):
Base Case
Start at the first room and call the function (roomDelete) on any non-NULL pointers; input to the function calls should be appropriate pointer to the room to the N/S/E/W, and appropriate char representing the direction N/S/E/W.
Check to see that all pointers (N/S/E/W) are NULL --> delete this current room.
Done/return.
Recursion
Make sure not to backtrack (travel back in the direction you came from), by using a char to hold the value opposite of the direction char.
Call the function on any non-NULL, non-backtrack pointers/directions.
Break connection to previous room.
Check to see that all pointers are NULL --> delete this current room.
Here is a simple picture on what the rooms/pointers look like:
http://i.imgur.com/btKz5JB.png
I have code that I tried to test. If I have a room (by itself), then the function works. But as soon as another room is thrown into the mix, then the function never returns/finishes. I'm not sure why. Is my logic sound? Am I missing something?
Any help is appreciated.
CODE:
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
#define NUM_DOORS 4
struct room {
struct room * north;
struct room * south;
struct room * east;
struct room * west;
} ;
int roomDelete(room *, char);
int main(void)
{
room * test_ptr = new room;
cout << "Created room at location: " << test_ptr << endl;
test_ptr->north = NULL;
test_ptr->south = NULL;
test_ptr->east = NULL;
test_ptr->west = NULL;
test_ptr->north = new room;
cout << "Created room at location: " << test_ptr->north << endl;
test_ptr->north->north = NULL;
test_ptr->north->south = test_ptr;
test_ptr->north->east = NULL;
test_ptr->north->west = NULL;
int test = roomDelete(test_ptr, '\0');
cout << test << endl;
return 0;
}
int roomDelete(room * room_ptr, char coord)
{
char coordinate[NUM_DOORS] = {'N', 'S', 'E', 'W'};
char coordinate_opposite[NUM_DOORS] = {'S', 'N', 'W', 'E'};
char coord_opp = '\0';
// call function on any remaining rooms
if(coord == '\0') // this is the beginning/initial room
{
for(int i = 0; i < NUM_DOORS; i++)
{
switch (coordinate[i])
{
case 'N':
{
if(room_ptr->north != NULL)
roomDelete(room_ptr->north, 'N');
break;
}
case 'S':
{
if(room_ptr->south != NULL)
roomDelete(room_ptr->south, 'S');
break;
}
case 'E':
{
if(room_ptr->east != NULL)
roomDelete(room_ptr->east, 'E');
break;
}
case 'W':
{
if(room_ptr->west != NULL)
roomDelete(room_ptr->west, 'W');
break;
}
default:
cout << "There was an error deallocating for the room at location: " << room_ptr << endl;
}
}
// delete the current room
if(room_ptr->north == NULL && room_ptr->south == NULL && room_ptr->east == NULL && room_ptr->west == NULL)
{
cout << "Deleting room at location: " << room_ptr << endl;
delete room_ptr;
}
else
return 2; // outward rooms have not been deleted yet
}
else // recursion
{
// this sets the value for the door that won't be handed to the delete function
for(int j = 0; j < NUM_DOORS; j++)
{
if(coord == coordinate[j])
coord_opp = coordinate_opposite[j];
}
if(coord_opp == '\0')
{
cout << "An error occurred while setting the value of the opposite coordinate.\n";
return 1;
}
// call the function on any remaining rooms
for(int k = 0; k < NUM_DOORS; k++)
{
if(coordinate[k] != coord_opp) // this is to avoid backtracking (which would cause infinite recursion)
{
switch (coordinate[k])
{
case 'N':
{
if(room_ptr->north != NULL)
roomDelete(room_ptr->north, 'N');
break;
}
case 'S':
{
if(room_ptr->south != NULL)
roomDelete(room_ptr->south, 'S');
break;
}
case 'E':
{
if(room_ptr->east != NULL)
roomDelete(room_ptr->east, 'E');
break;
}
case 'W':
{
if(room_ptr->west != NULL)
roomDelete(room_ptr->west, 'W');
break;
}
default:
cout << "There was an error deallocating for the room at location: " << room_ptr << endl;
}
}
}
// delete connection (ptr's) between current room and previous
switch(coord)
{
case 'N':
{
room_ptr->south->north = NULL;
room_ptr->south = NULL;
}
case 'S':
{
room_ptr->north->south = NULL;
room_ptr->north = NULL;
}
case 'E':
{
room_ptr->west->east = NULL;
room_ptr->west = NULL;
}
case 'W':
{
room_ptr->east->west = NULL;
room_ptr->east = NULL;
}
default:
cout << "There was a problem with severing the connection for the room at location: " << room_ptr << endl;
}
// delete current room
if(room_ptr->north == NULL && room_ptr->south == NULL && room_ptr->east == NULL && room_ptr->west == NULL)
{
cout << "Deleting room at location: " << room_ptr << endl;
delete room_ptr;
}
else
return 3; // outward rooms have not been deleted yet
}
return 0; // successful in deallocating the entire complex
}

I don't understand your algorithm, but I can tell where you are failing.
switch (coord)
{
case 'N':{
room_ptr->south->north = NULL;
room_ptr->south = NULL;
}
case 'S':{
room_ptr->north->south = NULL; // <-- Program Fails Here
room_ptr->north = NULL;
}
room_ptr->north at this moment is a null pointer and you are thus writing at location you are not allowed to.
Maybe you don't fully understand switch statements? It has so called "fall-through" behavior , i.e. it doesn't break out by itself just because it is a new case, it will just find a place where to start executing code and keep executing it until it hits "}" or finds explicitly written "break;" in it's way.

Related

Why did one linked list's end node change from NULL to another list's next node?

The first function, linkAndMove, is used for basic linking together and moving point process.
The Union function is used for finding all numbers in linked lists la and lb (without repeats)
My test example: la {1,3} lb{3,5}
But in the last when la point to NULL, and lb point to 5.
After first function linkAndMove, the list la changed to {1,3,5}
Why did la's end node change from NULL to lb's now node 5?
before first function
after first function
void linkAndMove(slink **pNode, slink **qNode, slink **finNode,
int linkFlag, int moveFlag) {
if (linkFlag == -1 || moveFlag == -1) {
cout << "ERROR! No matched logical in basic link list process." << endl;
exit(1);
}
switch (linkFlag) {
case 0:
if ((*finNode)->data != (*pNode)->data) {
(*finNode)->next = (slink *) malloc(sizeof(MemLEN));
(*finNode)->next = (*pNode);
(*finNode) = (*finNode)->next;
}
break;
case 1:
if ((*finNode)->data != (*qNode)->data) {
(*finNode)->next = (slink *) malloc(sizeof(MemLEN));
(*finNode)->next = (*qNode);
(*finNode) = (*finNode)->next;
}
break;
case 2:
break;
default:
cout << "ERROR! No matched logical in basic link list process." << endl;
exit(1);
}
switch (moveFlag) {
case 0:
(*pNode) = (*pNode)->next;
break;
case 1:
(*qNode) = (*qNode)->next;
break;
case 2:
(*pNode) = (*pNode)->next;
(*qNode) = (*qNode)->next;
break;
default:
cout << "ERROR! No matched logical in basic link list process." << endl;
exit(1);
}
}
void Union(slink *la, slink *lb, slink *lc) {
slink *pNode, *qNode;
pNode = la->next;
qNode = lb->next;
int linkFlag, moveFlag;
while (pNode != NULL || qNode != NULL) {
linkFlag = -1;
moveFlag = -1;
if (pNode == NULL) {
linkFlag = moveFlag = 1;
} else if (qNode == NULL) {
linkFlag = moveFlag = 0;
} else {
if (pNode->data > qNode->data) {
linkFlag = 1;
moveFlag = 1;
} else if (pNode->data < qNode->data) {
linkFlag = 0;
moveFlag = 0;
} else {
linkFlag = 0;
moveFlag = 2;
}
}
/*if (pNode == NULL) {
linkAndMove(NULL, &qNode, &lc, linkFlag, moveFlag);
} else*/
linkAndMove(&pNode, &qNode, &lc, linkFlag, moveFlag);
}
}
I found the reason.
Because in function linkAndMove, the pointer finNode is connected to the list la's node. In preivous codes, using node's next to connect pNode, so changed the la's end node from NULL to that node.
The solution I found is create new node for list lc, that cannot infect the orignal data list la. Codes here.
switch (linkFlag) {
case 0:
if ((*finNode)->data != (*pNode)->data) {
(*finNode)->next = initLinkNode();
(*finNode) = (*finNode)->next;
(*finNode)->data = (*pNode)->data;
}
break;
case 1:
if ((*finNode)->data != (*qNode)->data) {
(*finNode)->next = initLinkNode();
(*finNode) = (*finNode)->next;
(*finNode)->data = (*qNode)->data;
}
break;
case 2:
break;
default:
cout << "ERROR! No matched logical in basic link list process." << endl;
exit(1);
}

Display() resets the entire array C++

I tried creating a hash map storing its inputs as linked list nodes using separated chaining. The first display function gives a desirable output, but the next one resets the entire array to empty nullptr. I used the same class object so shouldn't it give the same result each time? Was it because of the destructor somehow? I thought it may be because I inserted a new item so I deleted it and the same thing still persists. My only suspect is the display() function but please point out if the problem comes from somewhere else.
Sorry if the post is so long, I want to make sure everyone can see the entire code to spot the problem.
#include <iostream>
#include <string>
#include "C:\Users\admin\source\repos\hash-library-master\sha3.cpp"
using namespace std;
const int TABLE_SIZE = 11;
struct HashNode
{
string key;
string value;
HashNode* next;
};
class HashMap {
private:
HashNode **table;
public:
//each element of table will be a root pointer to their respective chain
HashMap() {
table = new HashNode*[TABLE_SIZE];
for (int i = 0; i < TABLE_SIZE; i++)
{
table[i] = nullptr;
}
}
//hashing algorithm using SHA3 (courtesy of Stephan Brumme)
string hashFunc(string input)
{
string key;
SHA3 sha3;
key = sha3(input);
return key;
}
//insert new node
void insert(string key, string value)
{
//using hashing function to calculate hash index from string variable key
int hash = 0;
for (int a = 0; a < key.length(); ++a)
hash += key[a];
hash = hash % TABLE_SIZE;
//create new node to store data
HashNode* newNode = new HashNode;
newNode->value = value;
newNode->key = key;
newNode->next = nullptr;
//check and insert new node to front of line
if (table[hash] == nullptr)
table[hash] = newNode;
else
{
newNode->next = table[hash]->next;
table[hash]->next = newNode;
}
}
void display()
{
for (int i = 0; i < TABLE_SIZE; ++i)
{
if (table[i] == nullptr)
cout << i << " NULL" << endl;
else
{
while (table[i] != nullptr)
{
cout << i << " " << table[i]->value << "; ";
table[i] = table[i]->next;
if (table[i] == nullptr)
cout << "(end of chain)" << endl;
}
}
}
}
~HashMap() {
for (int i = 0; i < TABLE_SIZE; i++)
if (table[i] != NULL)
delete table[i];
delete[] table;
}
};
Driver
#include <iostream>
#include <string>
#include "hashMap.h"
using namespace std;
int main()
{
HashMap obj;
//test insert
obj.insert("5", "3100 Main St, Houston TX ");
obj.insert("5", "2200 Hayes Rd, Austin TX");
obj.insert("226", "1775 West Airport St, San Antonio TX");
obj.insert("273", "3322 Walnut Bend, Houston TX");
obj.insert("491", "5778 Alabama, Waco TX");
obj.insert("94", "3333 New St, Paris TX");
obj.display(); //resolved
cout << endl << endl;
//testing new hashing algorithm
string input, key;
cout << "Please enter any new address you want to store: ";
cin >> input;
key = obj.hashFunc(input); //create hash key
obj.insert(key, input);
obj.display(); //resets the array somehow
return 0;
}
Output
0 1775 West Airport St, San Antonio TX; (end of chain)
1 NULL
2 3322 Walnut Bend, Houston TX; (end of chain)
3 NULL
4 5778 Alabama, Waco TX; (end of chain)
5 NULL
6 NULL
7 NULL
8 NULL
9 3100 Main St, Houston TX ; 9 2200 Hayes Rd, Austin TX; (end of chain)
10 3333 New St, Paris TX; (end of chain)
//where display() resets
Please enter any new address you want to store: ewrewrw
0 NULL
1 ewrewrw; (end of chain)
2 NULL
3 NULL
4 NULL
5 NULL
6 NULL
7 NULL
8 NULL
9 NULL
10 NULL
Make all member functions that are not supposed to change the object itself const. This ensures that the function can be used when the object is used in a const context and will also enable the compiler to help you if you make a mistake. It will give you compilation errors if you try modifying the object in the function and will therefore complain about the line table[i] = table[i]->next; where you make changes to the object in your current code.
So start by making the function const and fix the errors. With the fixes in place it could look something like this:
void display() const // const added
{
for (int i = 0; i < TABLE_SIZE; ++i)
{
if (table[i] == nullptr)
cout << i << " NULL" << endl;
else
{
// using a temporary pointer, ptr, to go through the list
for(HashNode* ptr = table[i]; ptr != nullptr; ptr = ptr->next)
{
cout << i << " " << ptr->value << "; ";
}
cout << "(end of chain)" << endl;
}
}
}
Your display function is setting all elements of table to nullptr by looping until they become nullptr.
You should use another pointer variable for iterating to avoid this destruction.
void display()
{
for (int i = 0; i < TABLE_SIZE; ++i)
{
if (table[i] == nullptr)
cout << i << " NULL" << endl;
else
{
HashNode *p = table[i]; // another pointer variable for iterating
while (p != nullptr)
{
cout << i << " " << p->value << "; ";
p = p->next;
if (p == nullptr)
cout << "(end of chain)" << endl;
}
}
}
}

How can I traverse a Huffman Tree (via code) and print out the encodings for each letter?

I'd like to start with what I know about heaps and Huffman code.
For this project, we use a minimum heap. The top part of the upside-down tree (or root) holds the minimum element. Whenever something is added to the array, everything gets moved, so the root is always the minimum value element. Whenever an element is deleted, everything gets reconfigured with the top element holding the minimum once again. In class, we went over a (template) class called MaxHeap, which I converted into MinHeap without the template stuff.
My professor went over Huffman encoding, but I understood it best using this visual tool:
https://people.ok.ubc.ca/ylucet/DS/Huffman.html
The idea is to use a minimum heap as follows:
1. Delete two nodes
2. Create a new node with the deleted nodes as children. The frequency of this node is the summation of the two children frequencies.
3. Add this new node to the minimum heap
This process repeats until there is one node left in the heap (the root). Next, we find the encodings for each letter. To do this, travel down the tree with left movement being 0 and right movement being 1. Traveling right twice then left once would give 110 for the letter 'c' in my tree (image link can be found towards the bottom of my post).
Everything was going mostly fine until I needed to traverse from the root. I had no idea how to do this via code, so I tried googling the answers and found these two websites:
https://www.geeksforgeeks.org/huffman-coding-greedy-algo-3/
https://www.programiz.com/dsa/huffman-coding
I copied their function printCodes() into my code, but I didn't get see it work.
When I try manually going down the tree, I get two things. For example, I tried traveling left down the root and using cout to see the values. I expected to see 40, !, e, d; but when I tried I was getting gibberish number and characters (greek letters like theta, sigma, etc). It gets really weird because on line 207, yourRoot->left->freq gives me 40, but the same thing on the line 208 of code gives me a large number. When I traveled right, I got: Exception thrown: read access violation. yourRoot->right->right->letter was 0xCCCCCCCC.
To reiterate cout << yourRoot->left->freq << endl; will give me 40 the first time I call it, but the second time I get a random number. I expected the same output twice in a row. Am I supposed to keep a pointer or pointer-to-pointer to the address of yourRoot or something?
Another problem is in createHuffmanTree(), if I put return root; outside the while loop I get this error and the code doesn't run at all:
potentially uninitialized local pointer variable 'root' used
Both of these things were odd problems and I assume it has to do with the way I'm using & and * symbols. I tried using something like this:
MinHeap yourHeap = MinHeap(6);
node *item = newNode(30, 'f');
yourHeap.Insert(*item);
item = newNode(20, 'e');
yourHeap.Insert(*item);
item = newNode(20, 'd');
yourHeap.Insert(*item);
item = newNode(15, 'c');
yourHeap.Insert(*item);
item = newNode(10, 'b');
yourHeap.Insert(*item);
item = newNode(5, 'a');
yourHeap.Insert(*item);
delete item;
This works the same as the yourList[] code I have in main(), but I figured "keep it simple stupid" and avoid using pointers since I clearly have some issues with them.
I uploaded an output without any error causing code and a drawing of what I expect my tree to look like with the values I want to use (https://imgur.com/a/Vpx7Eif). If the link doesn't work, please let me know so I can fix it.
The code I have thus far is:
#include <iostream>
using namespace std;
#define MAX_TREE_HEIGHT 20
//exception is thrown if wrong input
class NoMem
{
public:
NoMem() { cout << "Heap is full\n"; }
};
class OutOfBounds
{
public:
OutOfBounds() { cout << "Heap is empty\n"; }
};
struct node
{
int freq;
char letter;
struct node *left, *right;
};
// initialize node with frequency and letter
node* newNode(int freq, char letter)
{
node *temp = new node;
temp->freq = freq;
temp->letter = letter;
temp->left = nullptr;
temp->right = nullptr;
return temp;
}
// initialize node using two nodes as children
node* newNode(node& a, node& b)
{
node *temp = new node;
temp->freq = a.freq + b.freq;
temp->letter = '!';
temp->left = &a;
temp->right = &b;
return temp;
}
class MinHeap {
public:
MinHeap(int MSize)
{
MaxSize = MSize;
heap = new node[MaxSize + 1];
Size = 0;
}
~MinHeap() { delete[] heap; }
MinHeap& Insert(node& x);
MinHeap& Delete(node& x);
void Display();
int Size;
private:
int MaxSize;
node *heap;
};
MinHeap& MinHeap::Insert(node& x)
{
if (Size == MaxSize) throw NoMem();
else
{
printf("Inserting '%c' with frequency of %d. ", x.letter, x.freq);
int i = ++Size;
while (i != 1 && x.freq < heap[i / 2].freq)
{
heap[i] = heap[i / 2];
i /= 2;
}
heap[i] = x;
Display();
return *this;
}
}
MinHeap& MinHeap::Delete(node& x)
{
if (Size == 0) throw OutOfBounds();
x.freq = heap[1].freq; // root has the smallest key
x.letter = heap[1].letter;
printf("Deleting '%c' with frequency of %d. ", x.letter, x.freq);
node y = heap[Size--]; // last element
int vacant = 1;
int child = 2; //make child = left child
while (child <= Size)
{
if (child < Size && heap[child].freq > heap[child + 1].freq) ++child;
// right child < left child
if (y.freq <= heap[child].freq) break;
heap[vacant] = heap[child]; // move smaller child
vacant = child; // new vacant
child = child * 2; // new child of vacant
}
heap[vacant] = y;
Display();
return *this;
}
void MinHeap::Display()
{
printf("Your heap contains: ");
for (int i = 1; i <= Size; i++)
printf("'%c' = %d, ", heap[i].letter, heap[i].freq);
printf("\n");
}
node* createHuffmanTree(MinHeap& yourHeap)
{
cout << "--- Creating Huffman Tree ---\n";
node left, right, *root;
while (yourHeap.Size > 1)
{
yourHeap.Delete(left);
yourHeap.Delete(right);
root = newNode(left, right);
cout << "-> New Node: freq = " << root->freq << ", letter = " << root->letter << ", left: " << root->left->letter << ", right: " << root->right->letter << endl;
yourHeap.Insert(*root);
if (yourHeap.Size < 2)
{
return root;
}
}
//return root; // potentially uninitialized local pointer variable 'root' used
}
void outputHuffmanCode(node* root, int arr[], int top)
{
// left movement is 0
if (root->left)
{
arr[top] = 0;
outputHuffmanCode(root->left, arr, top + 1);
}
// right movement is 1
if (root->right)
{
arr[top] = 1;
outputHuffmanCode(root->right, arr, top + 1);
}
// if reached leaf node, must print character as well
if (!(root->left) && !(root->right))
{
cout << "'" << root->letter << "' = ";
for (int i = 0; i < top; ++i)
cout << arr[i];
cout << endl;
}
}
int main()
{
node yourList[6];
yourList[0].freq = 5;
yourList[0].letter = 'a';
yourList[1].freq = 10;
yourList[1].letter = 'b';
yourList[2].freq = 15;
yourList[2].letter = 'c';
yourList[3].freq = 20;
yourList[3].letter = 'd';
yourList[4].freq = 20;
yourList[4].letter = 'e';
yourList[5].freq = 30;
yourList[5].letter = 'f';
cout << "Here is your list: ";
for (int i = 0; i < 6; i++)
{
cout << "'" << yourList[i].letter << "' = " << yourList[i].freq;
if (i < 5) cout << ", ";
} cout << endl;
MinHeap yourHeap(6);
yourHeap.Insert(yourList[5]);
yourHeap.Insert(yourList[4]);
yourHeap.Insert(yourList[3]);
yourHeap.Insert(yourList[2]);
yourHeap.Insert(yourList[1]);
yourHeap.Insert(yourList[0]);
/*
MinHeap yourHeap = MinHeap(6);
node *item = newNode(30, 'f');
yourHeap.Insert(*item);
item = newNode(20, 'e');
yourHeap.Insert(*item);
item = newNode(20, 'd');
yourHeap.Insert(*item);
item = newNode(15, 'c');
yourHeap.Insert(*item);
item = newNode(10, 'b');
yourHeap.Insert(*item);
item = newNode(5, 'a');
yourHeap.Insert(*item);
delete item;
*/
node *yourRoot = newNode(0, NULL);
yourRoot = createHuffmanTree(yourHeap);
// same cout twice in a row, different results
//cout << yourRoot->left->freq << endl;
//cout << yourRoot->left->freq << endl;
cout << "L0 Root: freq = " << yourRoot->freq << ", letter = " << yourRoot->letter << ", left freq: " << yourRoot->left->freq << ", right freq: " << yourRoot->right->freq << endl;
cout << "L11 Left: freq = " << yourRoot->left->freq << ", letter = " << yourRoot->left->letter << ", left: " << yourRoot->left->left->letter << ", right: " << yourRoot->left->right->letter << endl;
//cout << "R11 Left: freq = " << yourRoot->right->freq << ", letter = " << yourRoot->right->letter << ", left: \n";
//<< yourRoot->right->left->letter << ", right: " << yourRoot->right->right->letter << endl;
//int arr[MAX_TREE_HEIGHT], top = 0;
//outputHuffmanCode(yourRoot, arr, top);
system("pause");
return 0;
}
I'd like to thank whoever reads and replies to this post in advance. I think I've given as much information as I could. If I did anything that's against community rules, please let me know so I can fix my mistake(s).
In your createHuffmanTree Function, you create the node's left and right...
with root = newNode(left, right); you let the left/right member of your struct point to the address of the (temporary) node you've created in createHuffmanTree (that means in
node* newNode(node& a, node& b)
the address of a and b is always the same..
and the node goes out of scope after leaving the createHuffmanTree function - i think this causes your problem. You know what I mean?

Nested for loop being ignored after if-else chain

I am programming a MARIE assembler for one of my classes and I've ran into a logical error involving my control structure for one of my functions.
What I'm trying to accomplish is taking in all the data that was inserted into my vectors and then that data is being used to create integer opcode data for display. Yet for whatever reason my nested for loop is being ignored after my if-else chain.
The code within the nested for-loop seems to be working properly aside from this one logic error.
Please note that instructions, hexNums, secondPassData, valueZ, and symBols are my vectors.
For some clarification:
The If-Else chain is just used to read instruction words and to set basedInt to the proper decimal number for later hexadecimal conversion.
There are a few special conditions in the code below which are marked.
If there is no special condition then the code checks the valueZ vector at instructions.at(i) to see if the valueZ element is in symBols.
If it is a symBol element through character checks, it takes its hexNums position and adds it to the basedInt.
If it is not, it instead has its corresponding valueZ element converted from string to int and then added to the basedInt.
Those elements are added to the secondPassData vector.
int basedInt;
int newInt;
int pushInt;
string temp;
for(unsigned int i = 0; i < instructions.size(); ++i) //if i is less then instructions.size(), follow through with the statement
{
if(instructions.at(i) == "JNS") //sets basedInt to a decimal version of its hexidecimal opcode
{
basedInt = 0;
}
else if(instructions.at(i) == "HALT") //a special condition where the number is plugged into the secondPassData vector automatically
{
secondPassData.push_back(28672);
continue;
}
else if(instructions.at(i) == "CLEAR") //same as above
{
secondPassData.push_back(-24576);
continue;
}
else if(instructions.at(i) == "ADDL")
else if(instructions.at(i) == "ORG")
{
secondPassData.push_back(0000);
continue;
}
else if(instructions.at(i) == "HEX") //checks for the HEX psuedo-OP.
{
temp = valueZ.at(i); //converts it at position i to a string
basedInt = atoi(temp.c_str()); //converts that string to an int.
secondPassData.push_back(basedInt);//pushes into vector.
continue;
}
else if(instructions.at(i) == "DEC")
{
temp = valueZ.at(i);
basedInt = atoi(temp.c_str()); //similar function as above.
secondPassData.push_back(basedInt);
continue;
}
else
{
cout << "Beep Boop, program borked!" << endl;
return;
}
//for some reason the code below is getting ignored.
cout << i << endl;
for(unsigned int a = 0; a < instructions.size(); ++a) //works
{
cout << i << " " << a << endl;
string valFind = valueZ.at(i);
string symFind = symBols.at(a); //works
temp = valueZ.at(i);
if(symFind[0] == valFind[0])
{
newInt = hexNums.at(a);
pushInt = basedInt + newInt;
secondPassData.push_back(pushInt);
break;
}
else if(symFind[0] != valFind[0]) //works
{
temp = valueZ.at(i);
newInt = atoi(temp.c_str()); //works
pushInt= basedInt + newInt;
secondPassData.push_back(pushInt); //works
break;
}
break;
}
}
If you hit a continue in your else-if chain your main for loop will jump to its next iteration and will skip the rest of your code (in this case your nested for loop)
continue

C++ Member Function Clearing Matrix

I have had a similar issue with quite a few projects I have worked on involving classes containing arrays. I have a class that is supposed to handle a 2 dimensional matrix representing a TicTacToe game. There is an enumeration in the class for the status of the current game and one member function that has an enumeration return type. I cant seem to figure out why I can create the class set values in the matrix and as soon as I call the member function with the enumerated return type the whole array is reinitialized to 0. I think it has something to do with the constructor being called again or something along those lines but I have not been able to find anything after searching for the past few hours. Any help would be greatly appreciated.
Here is my header file containing the class information:
#ifndef TTT_H
#define TTT_H
#include <cstdlib>
#include <iostream>
using namespace std;
class TicTacToe
{
private:
enum Status{WinX, WinO, Continue, Draw};
int **board;
public:
TicTacToe();
~TicTacToe();
void PrintBoard();
bool ValidMove(int, int);
bool PlayerMove(int, int, int);
Status GameStatus(); //this one causes the problem
void Debug();
};
#endif
Here is the code for CPP file with the member function definitions:
#include "TicTacToe.h"
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <cassert>
using namespace std;
TicTacToe::TicTacToe()
{
board = new int*[3];
assert(board != 0);
for(int i=0;i<3;i++)
{
cout << "Constructor Ran again" << endl; //for testing
board[i] = new int[3];
assert(board[i] != 0);
for(int j=0;j<3;j++)
board[i][j] = 9;
}
}
TicTacToe::TicTacToe(TicTacToe &copy)
{
board = new int*[3];
assert(board != 0);
}
TicTacToe::~TicTacToe()
{
if(board)
delete[] board;
}
void TicTacToe::PrintBoard()
{
for(int i=0;i<3;++i)
{
for(int j=0;j<3;++j)
{
cout << "| ";
switch(board[i][j]){
case 0:
cout << "O ";
break;
case 1:
cout << "X ";
break;
case 9:
cout << " ";
break;
}
}
cout << "|" << endl;
cout << "------------" << endl;
}
}
bool TicTacToe::ValidMove(int row, int col)
{
bool valid = false;
if(row < 3 && col < 3)
{
if(board[row][col] == 9)
valid = true;
}
return valid;
}
bool TicTacToe::PlayerMove(int player, int row, int col)
{
bool done = false;
if(ValidMove(row,col) == true)
{
if(player == 1)
board[row][col] = 1;
else
board[row][col] = 0;
done = true;
}
return done;
}
TicTacToe::Status TicTacToe::GameStatus() //This function is the problem
{
int check, empty = 0;
bool done = false;
for(int i=0;i<3;++i)
{
for(int j=0;j<3;++j)
{
check += board[i][j];
if(board[i][j] = 9)
empty++;
}
if(check == 0)
return WinO;
else if(check == 3)
return WinX;
check = 0;
}
if(empty == 0)
return Draw;
for(int i=0;i<3;++i)
{
for(int j=0;j<3;++j)
check += board[j][i];
if(check == 0)
return WinO;
else if(check == 3)
return WinX;
check = 0;
}
check = board[0][0] + board[1][1] + board[2][2];
if(check == 0)
return WinO;
else if(check == 3)
return WinX;
check = 0;
check = board[0][2] + board[1][1] + board[2][0];
if(check == 0)
return WinO;
else if(check == 3)
return WinX;
check = 0;
return Continue;
}
void TicTacToe::Debug()
{
//cout << &board[0][0] << endl;
for(int i=0;i<3;++i)
{
for(int j=0;j<3;++j)
cout << board[i][j];
cout << endl;
}
}
Here is the driver file I am using to test:
#include "TicTacToe.h"
#include <iostream>
#include <cassert>
using namespace std;
int main()
{
int row, col;
bool valid;
enum Status{WinX, WinO, Continue, Draw};
TicTacToe * T;
T = new TicTacToe;
assert(T != 0);
cout << "There are 2 players. P1 is x P2 is o" << endl;
do
{
T->PrintBoard();
valid = false;
while(valid == false)
{
cout << "\nP1 choose a cell" ;
cin >> row >> col;
if(T->ValidMove(row, col) == true)
{
T->PlayerMove(1, row, col);
valid = true;
}
else
{
cout << "Not a valid choice" << endl;
valid = false;
}
}
T->PrintBoard();
cout << endl;
T->GameStatus(); //<<<<<this is the pain in my butt
T->PrintBoard();
valid = false;
while(valid == false)
{
cout << "\nP2 choose a cell" ;
cin >> row >> col;
if(T->ValidMove(row, col) == true)
{
T->PlayerMove(2, row, col);
valid = true;
}
else
{
cout << "Not a valid choice" << endl;
valid = false;
}
}
}
while(/*T->GameStatus() == Continue*/ 1==1);
//the call to GameStatus was commented out of the
//while statement for testing
return 0;
}
I know the code inside of the GameStatus function is far from pretty but the array is messed up before any of those lines are processed.
I left all of the other functions just to show that they work properly without issue.
Thanks in advance for any help you may be able to give.
You've got a simple typo in your code..
if(board[i][j] = 9) // will always return true (refp)
empty++;
Other remarks
When looking at your code a bit more thoroughly I see that you have a few other miss-happens, intentional or unintentional.. that I don't know:
int check is not initialized in TicTacToe::GameStatus
You are not freeing the allocated memory properly, you'll
need to free all entries in board, ie. delete board[i])
I don't like bugs, how can I get rid of the operator= vs operator== problem?
A quite common method to circumvent the problem of making a typo and writing = when you mean to compare (==) two variables is to flip the operands around (if one of them is a constant value, such as 9.
if(9 = board[i][j]) will not compile and such a bug would've never appeared in your code.
I'll have to say that I don't like writing my statements that way.. though it's a quite common method, especially in the "beginner" segment.
check is not initialized in GameStatus() .
if (board[i][j] = 9)
Isn't the above line of code resetting the array contents? You probably want to use == here, instead.
You have a serious issue in memory management. Look:
Your constructor performs 4 allocations (an array of pointers and 3 arrays of ints, to emulate a 2D arrray),
Your destructor performs 1 deallocation (= memory leak),
You have a custom destructor but you don't define (or block) operator= (you need to, see What is The Rule of Three?)
Your copy constructor is incomplete and doesn't create a "valid" object.
Basically the above is likely to cause you some memory problems. I suggest to:
Rewrite the destructor to first free all the arrays of ints, then the array of pointers,
Make the class TicTacToe uncopiable by declaring the copy constructor and the operator= as private.
Also some minor details on that matter:
board = new int*[3];
assert(board != 0);
The assertion is unnecessary. If the allocation fails, the operator new will throw an exception.
if(board)
delete[] board;
Operators delete and delete[] don't do anything if their argument is a null pointer, so that condition is redundant. Also you have designed your object with the invariant that the board exists as long as the TicTacToe object exists, so that check is totally unnecessary, right?
Keep it safe and simple!