[QUOTE=Soviet_Banter;33483895]Is that JSON? If not,use Google/Android's JSON library.[/QUOTE]
I haven't tried anything yet, I'm trying things still, I haven't looked at the JSON library yet.
A few nice guys on here last time attempted to help me with my problem with implementing a binary search tree. I've taken a few more cracks at it and come up with the following code.
Please bear in mind I know my code does not account for certain possibilities (e.g. the user only enters 1 value) and will account for those once I get the principle of it.
Tree.h
[code]
class Tree {
private:
Tree *right;
Tree *left;
Tree *root;
void insertUtil(int, Tree *);
void inorderUtil(Tree *);
public:
int data;
Tree();
Tree(int);
void insert(int);
void printInorder();
};
void Tree::insert(int inData) {
// Allows us to start insertion from the root node
if (root == 0) {
root = this;
}
insertUtil(inData, root);
};
void Tree::insertUtil(int inData, Tree *current){
if (inData < current->data) {
if (current->left == 0) {
current->left = new Tree(inData);
} else {
insertUtil(inData, current->left);
}
} else if (inData > current->data) {
if (current->right == 0) {
current->right = new Tree(inData);
} else {
insertUtil(inData, current->right);
}
} else {
// Do nothing, no duplicate data allowed
}
};
Tree::Tree() {
left = right = root = 0;
};
Tree::Tree(int inData) {
left = right = root = 0;
this->data = inData;
};
void Tree::printInorder() {
// Recurse through the tree starting at the root
inorderUtil(root);
};
void Tree::inorderUtil(Tree *current){
if (current != 0) {
inorderUtil(current->left);
std::cout << current->data << " ";
inorderUtil(current->right);
}
};
[/code]
Driver (main.cpp)
[code]
#include <iostream>
#include <cstdlib>
#include "Tree.h"
using namespace std;
int main(int argc, char *argv[]) {
// TEST OUTPUT
cout << endl;
for (int i = 1; i < argc; i++) {
cout << atoi(argv[i]) << " ";
}
// Get command line inputs, build the tree
Tree *binSearchTree = new Tree(atoi(argv[1]));
for (int i = 2; i < argc; i++) {
binSearchTree->insert(atoi(argv[i]));
}
// Output
cout << endl;
binSearchTree->printInorder();
return 0;
}
[/code]
I've rooted out a lot of minor errors and this whole shebang now complies in g++. (then again my compsci teacher used to say gcc would compile the phone book... not sure if that applies to g++)
However, when I attempt to run it in a Linux environment like so:
./a.out 1 5 6 7
Where 1 5 6 and 7 are the nodes I want to insert, I ONLY get a [B]segmentation fault[/B].
The program never even makes it to the creation of the tree, the test output I have at the top of the driver or any other operation. This makes me think I made a real boo boo.
Any suggestions?
[QUOTE=thirty9th;33484009]
-snip Tree stuff-
[/QUOTE]
Common segmentation faults:
[code]
char *p1 = NULL; // Initialized to null, which is OK,
// (but cannot be dereferenced on many systems).
char *p2; // Not initialized at all.
char *p3 = new char[20]; // Great! it's allocated,
delete [] p3; // but now it isn't anymore.
[/code]
Edit: I think it will be easier to debug it line by line, once you find the Seg-fault, then check your line against my compiled list.
I'm afraid my brain isn't functioning correctly right now and I'm having trouble narrowing down what the problem is.
For instance:
[code]
// TEST OUTPUT
cout << endl;
for (int i = 1; i < argc; i++) {
cout << atoi(argv[i]) << " ";
}
[/code]
This DOES output the 'endl' character and produces a new line.
Then I get a segmentation fault.
Have I done something horrendously wrong in this snippet? It seems as though it's not even making it to the point where I'm making use of my pointers, as you've pointed out as a possible problem.
For the purposes of testing, I am always using at least 2 arguments.
Compile with -g and run the program through gdb.
gdb ./a.out
>set args 1 5 6 7
>run
OMGWTFBBQSEGFAULT!
>bt
*prints stack trace*
[QUOTE=ROBO_DONUT;33484245]Compile with -g and run the program through gdb.
gdb ./a.out
>set args 1 5 6 7
>run
OMGWTFBBQSEGFAULT!
>bt
*prints stack trace*[/QUOTE]
Backtrace results:
[code]
#0 ...memory address... in Tree::insertUtil ()
#1 ...memory address... in Tree::insertUtil ()
#2 ...memory address... in Tree::insert ()
#3 ...memory address... in main ()
#4 ... memory address... in __libc_start_main () from /lib/libc.so.6
[/code]
Did you compile with -g?
It should give you arguments and line numbers.
[QUOTE=ROBO_DONUT;33484321]Did you compile with -g?
It should give you arguments and line numbers.[/QUOTE]
Oh that's more helpful. Don't mind my idiocy please.
Results:
[code]
Program received signal SIGSEGV, Segmentation fault.
0x08048840 in Tree::insertUtil (this=0x8049f68, inData = 7, current = 0x0) at Tree.h:29
29 if (inData < current->data)
[/code]
Yes that's enlightening... somehow it's recursing to where 'current' is NULL and of course wouldn't have data to compare against.... Any idea why that's happening?
Look at the next level up on the stack to find out how that call happened. You can type "frame 1" at the gdb prompt to move to that stack frame, so you can examine local variables there.
I can't find anything wrong with insertUtil(), however there are a few other things I notice. First of all, 'root' is not set for any of the child nodes. It's set to 'this' for the root node, but all the rest are NULL. It's really not necessary, either, as you don't really use it anywhere.
I'd also suggest using NULL instead of 0. It's cleaner, safer, and easier to read.
Try fixing those two things, maybe the insertUtil() bug is related to the root* issue.
[QUOTE=ROBO_DONUT;33484668]I'd also suggest using NULL instead of 0. It's cleaner, safer, and easier to read.[/QUOTE]
To be really correct, you have to #include <cstddef> in any translation unit that uses it, though. (In practice you don't really have to since other headers usually pull it in anyway, but one shouldn't rely on that.)
Better yet is the "nullptr" keyword in C++11, of course. :-)
[QUOTE=thirty9th;33484205]I'm afraid my brain isn't functioning correctly right now and I'm having trouble narrowing down what the problem is.
For instance:
[code]
// TEST OUTPUT
cout << endl;
for (int i = 1; i < argc; i++) {
cout << atoi(argv[i]) << " ";
}
[/code]
This DOES output the 'endl' character and produces a new line.
Then I get a segmentation fault.
Have I done something horrendously wrong in this snippet? It seems as though it's not even making it to the point where I'm making use of my pointers, as you've pointed out as a possible problem.
For the purposes of testing, I am always using at least 2 arguments.[/QUOTE]
cout is buffered by default so you won't get any output until the buffer is full or it is flushed. Passing in endl is one way to flush the buffer. cerr is an unbuffered output stream, you can use that instead for debugging purposes.
If the program crashes with data in the output buffer, you'll never see it. This is a major source of confusion for new programmers and old programmers that haven't used any sort of buffered stream in a while.
[QUOTE=artanis;33489414]cout is buffered by default so you won't get any output until the buffer is full or it is flushed.[/QUOTE]
Well, it's up to the implementation to decide when to flush. Most also flush when outputting a new-line, but of course you might/should not want to rely on that.
I'm making an application with windows forms; I have one main form and two others that the user should be able to bring up and hide/close. There's no reason either of these two other forms actually ever need to be disposed of, so I just made two member variables in the first form that contain instances of the two other forms.
The problem is, if the user goes to View>FormA, FormA opens, but if the user hits the X to close it, and then tries to reopen it again, an error occurs, saying that the object has already been disposed of. This seems rather strange to me; the concept that an object can be disposed while I still have references to it, and obviously this is not desirable behavior, so how do I fix this?
[editline]29th November 2011[/editline]
Wait nevermind, found it :P
[url]http://stackoverflow.com/questions/2865279/stop-the-form-from-closing-on-close-button-click[/url]
How would I make it so that when I dynamically add a picturebox, the event handler sets the image of the picturebox? This is what I've got:
[cpp]
PictureBox P = new PictureBox();
P.SetBounds(x, y, 15, 15);
P.BorderStyle = BorderStyle.FixedSingle;
P.BringToFront();
P.Click += new EventHandler(pictureboxClick);
Controls.Add(P);
panel1.Controls.Add(P);[/cpp]
[cpp] public void pictureboxClick(object sender, EventArgs e)
{
sender = new PictureBox().Image = imageList1.Images[0];
}[/cpp]
obviously it doesn't work.
[QUOTE=TheCloak;33492517]How would I make it so that when I dynamically add a picturebox, the event handler sets the image of the picturebox? This is what I've got:
[cpp]
PictureBox P = new PictureBox();
P.SetBounds(x, y, 15, 15);
P.BorderStyle = BorderStyle.FixedSingle;
P.BringToFront();
P.Click += new EventHandler(pictureboxClick);
Controls.Add(P);
panel1.Controls.Add(P);[/cpp]
[cpp] public void pictureboxClick(object sender, EventArgs e)
{
sender = new PictureBox().Image = imageList1.Images[0];
}[/cpp]
obviously it doesn't work.[/QUOTE]
Sender is the object that the event occured on. Assuming you want to set the picture for the picturebox that was clicked, do something like this:
[CODE]public void pictureboxClick(object sender, EventArgs e)
{
PictureBox p = (PictureBox)sender;
p.Image = imageList1.Images[0];
}[/CODE]
I got the idea of making some simple games in C# to learn more of its syntax and such, it's going to be 2D, but I don't really know what library to use. I read that the builtin Forms/Graphics stuff is not to be used for "games" since it doesn't handle movements well, and as alternatives I can only find Direct2D and DirectDraw, what's the differences? Is there any other?
XNA is what's mostly used for C# games.
In the future (not so near) I want to develop a game that I can put on a Mac, PC, Xbox (downloadable), PS3 (downloadable) and Wii (downloadable?).
I want this game to have online play.
I want PC players to be able to play with Wii players. And xbox players to be able to play with Mac players.
Is this possible?
I know that C++ must be natively compiled but there are ways to get the different operating systems to talk, right?
This game is not 3D. If that means anything.
I was going to start learning Java, but then I thought ... well maybe C++ would be better, because I need C++ for future projects as well, and it's nice to start those projects well equipped.
This is very possible. TCP and UDP are completely OS-independent and properly built most of the networking code can be shared without any modifications on every platform.
Your biggest issue will be graphics. Chances are you'll want to use OpenGL (ES) since that's the one thing that's probably easiest to share between the system. Regardless of what you choose, you'll probably have to make changes in the rendering for the Mac and certainly for the Xbox.
But playing with each other from different consoles is a simple thing if you can make graphics and whatnot sync properly.
What would be the best way to, in C, make a hash table mapping triplets of ASCII characters (ex "bar") to another string? Basically an associative array, but it needs to be fast.
[QUOTE=Octave;33498400]What would be the best way to, in C, make a hash table mapping triplets of ASCII characters (ex "bar") to another string? Basically an associative array, but it needs to be fast.[/QUOTE]
If it's only lower-case alphabetical characters, there's only 17576 possible combinations. In that case you can do a perfect hash and create an array with some integer multiple number of buckets (17576, 8788, 4394, 2197, etc.)
In this case, you're guaranteed to have constant-time access.
The perfect hashing for a triplet of lower-case alpha chars is going to be something like:
int hash = (s[0] - 0x61) + 26*(s[1] - 0x61) + 676*(s[2] - 0x61)
By the way, a big thank you to anyone who was able to help me with the earlier Binary Search Tree issues.
I was able to resolve that final issue and completed the project.
I have a C++ assignment where I need to check a Sudoku puzzle's validity, by checking each row, column, and individual 3x3 cell for duplicates.
I've been working at this all day and I can't figure it out... I feel like I haven't ever programmed before and I don't know whats going on. So I come to you, WAYWO, help me...
Have to use 2D arrays of Ints.
Post code of what you do have.
Well I had something like this but wasn't sure where to take it.
[code]
void FindErrors(ofstream& out, int array[][B_SIZE]){
for (int i = 0; i < 9; ++i){
bool nsx[9], nsy[9];
for (int j = 0; j < 9; ++j)
if (nsx[arr[i][j]])//invalid for in one dimension
nsx[arr[i][j]]++;
if (nsy[arr[j][i]])//invalid in the other dimension
nsy[arr[j][i]]++;
}
}
}
[/code]
-snop-
[QUOTE=Feihc;33500837]Well I had something like this but wasn't sure where to take it.
[code]
void FindErrors(ofstream& out, int array[][B_SIZE]){
for (int i = 0; i < 9; ++i){
bool nsx[9], nsy[9];
for (int j = 0; j < 9; ++j)
if (nsx[arr[i][j]])//invalid for in one dimension
nsx[arr[i][j]]++;
if (nsy[arr[j][i]])//invalid in the other dimension
nsy[arr[j][i]]++;
}
}
}
[/code][/QUOTE]
I'd take a different approach. I'd loop through each of the rows firstly and check for duplicate values; then loop through each of the columns and check for duplicate values, then go through the boxes. The boxes are likely to be the hardest to work out so you might want to create a new 3x3 array of ints and fill it in with values from the full grid. If you write your function nicely you could even make the 9x9 checkSudoku function call itself with the smaller 3x3 boxes.
At the moment it seems like you're trying to check everything in the two nested loops, which I doubt will work; not to mention the fact that there is no such variable "arr".
Can anyone here simplify the algorithm for generation Madelbrot? Possibly in a way i can write it in c++?
[QUOTE=pinecleandog;33504515]Can anyone here simplify the algorithm for generation Madelbrot? Possibly in a way i can write it in c++?[/QUOTE]
Pseudocode:
[cpp]for(float x = minXRange; x < maxXRange; x += xStep)
{
for(float y = minYRange; y < maxYRange; y += yStep)
{
complex Z = (x, y);
complex C = Z;
for(int = 0; i < iterations; i++)
{
Z = Z*Z + C;
if(Z.real * Z.real + Z.imaginary * Z.imaginary > maxRadius * maxRadius)
{
escaped = true;
break;
}
}
if(escaped) plot(x, y, white);
else plot(x, y, black);
}
}[/cpp]
Entirely untested and I'm very tired so I may have fucked up somewhere, but that's the basic idea of it. The variable names there should be obvious enough for you to work out what they do.
Sorry, you need to Log In to post a reply to this thread.