• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

pompidu

Member
New to mySql 5.2 and can access the workbench fine but don't understand this error when trying to access in command line. not sure if I am doing it right.
Edit: Not sure where to look for it in documentation.

bLRmKkY.jpg
 

Chris R

Member
So basically I have this

JRTmxPK.png


And the idea is you choose the filter button at the top and the grid is repopulated with rows containing dates only from this year.


You need to rebind the datagridview with a data source that has been filtered.

The way I do it (no idea if this is the best way, but it works and doesn't seem to be slow) is to create a method that does the data binding and has an optional parameter that contains my filtering criteria. If the criteria is present filter the data on that before binding, if it isn't just use the default.
 

usea

Member

Feep

Banned
Hmmm. Interesting question.

I have a bunch of objects I need to sort and then assign continuous numbers, 1 through 300. It's the draw order for a scene.

I can (and will have to) do a boolean check of one object against another, but doing so is fairly computationally expensive. Moreover, if two objects aren't near each other, it doesn't really matter which one goes first.

How should I go about ordering this? I feel like this should be a relatively simple problem, but I'm having trouble coming up with a good approach.
 

arit

Member
Hmmm. Interesting question.

I have a bunch of objects I need to sort and then assign continuous numbers, 1 through 300. It's the draw order for a scene.

I can (and will have to) do a boolean check of one object against another, but doing so is fairly computationally expensive. Moreover, if two objects aren't near each other, it doesn't really matter which one goes first.

How should I go about ordering this? I feel like this should be a relatively simple problem, but I'm having trouble coming up with a good approach.


A just quick thought... you could first sort them into sectors(e.g. four quadrants on the screen) and then make your checks within those sectors, though in the worst case you would still have n^2 checks + sorting.

EDIT: Or you could use a quadtree.
 
Hmmm. Interesting question.

I have a bunch of objects I need to sort and then assign continuous numbers, 1 through 300. It's the draw order for a scene.

I can (and will have to) do a boolean check of one object against another, but doing so is fairly computationally expensive. Moreover, if two objects aren't near each other, it doesn't really matter which one goes first.

How should I go about ordering this? I feel like this should be a relatively simple problem, but I'm having trouble coming up with a good approach.

From what I understand of your game, don't go this route, it will only give you trouble. Doesn't seem like a proper solution for the problem you are having. Most solutions will probably feel hacky and prone to errors as soon as the camera angle changes or you want multilayered levels.

Why not use an orthographic projection and draw your sprites as billboards? Let the hardware do the sorting for you.
 

Feep

Banned
From what I understand of your game, don't go this route, it will only give you trouble. Doesn't seem like a proper solution for the problem you are having. Most solutions will probably feel hacky and prone to errors as soon as the camera angle changes or you want multilayered levels.

Why not use an orthographic projection and draw your sprites as billboards? Let the hardware do the sorting for you.
I do use an orthographic projection, and I do draw my sprites as billboards. However, since the camera is tilted, and the sprites always face the camera, they exist in a weird diagonal path in actual 3-D space compared to the 3-D geometry and models.

The camera angle does *not* change in TCAE, and multi-layered levels will be few and far between.

I think I'm going to end up using a mergesort between a presorted list of buildings (they never move) and a sorted list of soldiers (quick to do), and only have to do the building/soldier merging once through. It's once per frame, though, I hope it isn't too bad...
 
Hmmm. Interesting question.

I have a bunch of objects I need to sort and then assign continuous numbers, 1 through 300. It's the draw order for a scene.

I can (and will have to) do a boolean check of one object against another, but doing so is fairly computationally expensive. Moreover, if two objects aren't near each other, it doesn't really matter which one goes first.

How should I go about ordering this? I feel like this should be a relatively simple problem, but I'm having trouble coming up with a good approach.
I didn't spend the time to do the CS, but I'm pretty sure that this is going to reduce to sorting the whole thing, OR letting the hardware do it for you as the previous poster mentioned.

I think the unknown relationships between the objects is going to get in the way of clever solutions.

e: The buildings are presorted? That sounds potentially exploitable, although the mergesort may already be as good as you can get...
 

Feep

Banned
I didn't spend the time to do the CS, but I'm pretty sure that this is going to reduce to sorting the whole thing, OR letting the hardware do it for you as the previous poster mentioned.

I think the unknown relationships between the objects is going to get in the way of clever solutions.

e: The buildings are presorted? That sounds potentially exploitable, although the mergesort may already be as good as you can get...
I proved by example that I can't just ignore certain comparisons...if I do, it messes up the merge operation. So I think you're right.

Oh well, if one half (actually more like three fourths) is presorted, all the better. I'll run some speed tests and see how it goes!

Thanks, guys!
 
You've probably tried this, but I'll note it just the same:

it might make sense to leverage your sorted list from the previous frame as a starting point for the next one. Going to depend on amount of movement...
 
I do use an orthographic projection, and I do draw my sprites as billboards. However, since the camera is tilted, and the sprites always face the camera, they exist in a weird diagonal path in actual 3-D space compared to the 3-D geometry and models.

You are using 3d models for you characters right? Maybe you can project your background image into the 3D geometry using this trick http://www.youtube.com/watch?v=PPaH8oIJd7g

Otherwise, bubblesort is faster for a small number of elements.
 

Feep

Banned
You are using 3d models for you characters right? Maybe you can project your background image into the 3D geometry using this trick http://www.youtube.com/watch?v=PPaH8oIJd7g

Otherwise, bubblesort is faster for a small number of elements.
That's actually a cool trick, but I have no idea how they did it, and I can't really mock up 3-D environment geometry anyway...no cash for it.

It's cool though! I know Bubble Sort could work, but it's hyper slow. Gonna roll with Merge Sort unless I hit problems.
 
That's actually a cool trick, but I have no idea how they did it, and I can't really mock up 3-D environment geometry anyway...no cash for it.

It's cool though! I know Bubble Sort could work, but it's hyper slow. Gonna roll with Merge Sort unless I hit problems.

I assume you have less than 1000 sprites, in that case bubblesort is probably faster.
 

Feep

Banned
I assume you have less than 1000 sprites, in that case bubblesort is probably faster.
Really? Bubble is faster than merge for lists of under 1000? Even if the comparison cost is high?

(It's going to be anywhere from 150-300 sprites, usually, depending on the mission)
 
Really? Bubble is faster than merge for lists of under 1000? Even if the comparison cost is high?

(It's going to be anywhere from 150-300 sprites, usually, depending on the mission)

It depends on the implementation and other factors but don't underestimate the cost of recursion and merging arrays.

Worst case, bubblesort is dead easy to implement. Use that one and if the performance is not good enough then implement merge.
 

leroidys

Member
Seemingly simple question but...

Is there an easy way to swap two predefined arrays in C? Like...

Code:
char a[12][5];
char b[12][5];

swap(char *a[12][5], char *b[12][5])
{
    char *temp[12][5] = a;
    a = b;
    b = temp;
}

The compiler seems to not let you mess with arrays the same way that you can grab a pointer to something like char **str or whatever.
 
Seemingly simple question but...

Is there an easy way to swap two predefined arrays in C? Like...

Code:
char a[12][5];
char b[12][5];

swap(char *a[12][5], char *b[12][5])
{
    char *temp[12][5] = a;
    a = b;
    b = temp;
}

The compiler seems to not let you mess with arrays the same way that you can grab a pointer to something like char **str or whatever.

What do you mean by predefined array? It's been a while since I've done stuff in C, but would the following work?

Code:
//void swap(char **a, char **b)
//{
//    char **temp = a;
//    a = b;
//    b = temp;
//}

There is a nicer way to do this with pointers without involving a temp variable, but I can't think of it right now.

edit: The above code doesn't work, I just tried it out.
 
Really? Bubble is faster than merge for lists of under 1000? Even if the comparison cost is high?

(It's going to be anywhere from 150-300 sprites, usually, depending on the mission)

How would you do merge sort in C#? Just code it yourself, or is there a good library you know of?
 

leroidys

Member
What do you mean by predefined array? It's been a while since I've done stuff in C, but would the following work?

Code:
//void swap(char **a, char **b)
//{
//    char **temp = a;
//    a = b;
//    b = temp;
//}

There is a nicer way to do this with pointers without involving a temp variable, but I can't think of it right now.

edit: The above code doesn't work, I just tried it out.

Yeah I need a fixed array basically a matrix so char ** won't work :/
 

tokkun

Member
Really? Bubble is faster than merge for lists of under 1000? Even if the comparison cost is high?

(It's going to be anywhere from 150-300 sprites, usually, depending on the mission)

If comparison cost is high, then mergestort will be better.

However, since you mentioned that the two lists may already be mostly presorted, you might want to consider Insertion Sort, which has O(n) complexity on a presorted list.
 

Water

Member
Hmmm. Interesting question.

I have a bunch of objects I need to sort and then assign continuous numbers, 1 through 300. It's the draw order for a scene.

I can (and will have to) do a boolean check of one object against another, but doing so is fairly computationally expensive. Moreover, if two objects aren't near each other, it doesn't really matter which one goes first.

How should I go about ordering this? I feel like this should be a relatively simple problem, but I'm having trouble coming up with a good approach.
Need more info. What is your reason for sorting a mere 300 billboards in the first place? Are they drawn with some super-expensive fragment shaders, or...?

The other guys seem to know what your project is, but I don't. At least knowing which parts of the scene are dynamic and which are static, and how the camera can move, would be useful.
 

Water

Member
Yeah I need a fixed array basically a matrix so char ** won't work :/
You most likely don't need a fixed array. Higher dimension arrays can always be simulated with lower dimension arrays, like so:
Code:
char* foo = malloc(12*5); // 12x5 array
foo[x + 12*y] = 'X'; // inserting into (x, y)
free(foo);

That's more or less what the compiler is doing behind the scenes anyway when you use a fixed array.

Whenever you have one layer of pointer indirection in front of your objects, swapping the objects is a matter of swapping the pointers. If you just have the objects (like fixed arrays), you have to swap all the data inside piece by piece.
 

Sharp

Member
Seemingly simple question but...

Is there an easy way to swap two predefined arrays in C? Like...

Code:
char a[12][5];
char b[12][5];

swap(char *a[12][5], char *b[12][5])
{
    char *temp[12][5] = a;
    a = b;
    b = temp;
}

The compiler seems to not let you mess with arrays the same way that you can grab a pointer to something like char **str or whatever.
No.

The trick is realizing that multidimensional arrays don't work quite the same as regular arrays:
Code:
include <stdlib.h>
#include <stdio.h>

void main() {
  int index;
  int stack_allocated_array_2d[12][5];
  int **dynamic_array_2d;

  dynamic_array_2d = (int **) malloc (12 * sizeof(int *));

  if (!dynamic_array_2d)
    exit(1);

  for (index = 12; index--; ) {
    dynamic_array_2d[index] = (int *) malloc (5 * sizeof(int));
    if (!dynamic_array_2d[index])
      exit(1);
  }

  printf("%d\n", ((void *) (dynamic_array_2d[0]) == ((void *) (dynamic_array_2d))));
  printf("%d\n", ((void *) (stack_allocated_array_2d[0]) == ((void *) (stack_allocated_array_2d))));

  for (index = 12; index--; ) {
    free (dynamic_array_2d[index]);
  }
  free (dynamic_array_2d);
}

The deeper issue is that when you define a variable, it gets compiled out as a position on the stack. Every reference to that variable is now going to be a reference to some constant (unless you use alloca) offset from the stack pointer. As such, you can't swap it because the program wouldn't know where to look for it--it's a fixed offset, there's no address where it's stored that you can change. You could swap pointers to multidimensional arrays though, if you made them lvalues.
 

Feep

Banned
If comparison cost is high, then mergestort will be better.

However, since you mentioned that the two lists may already be mostly presorted, you might want to consider Insertion Sort, which has O(n) complexity on a presorted list.

Need more info. What is your reason for sorting a mere 300 billboards in the first place? Are they drawn with some super-expensive fragment shaders, or...?

The other guys seem to know what your project is, but I don't. At least knowing which parts of the scene are dynamic and which are static, and how the camera can move, would be useful.
I have a bunch of dynamic objects (bullets and soldiers) and a bunch of static objects (structures). The structures (3/4ths or so of all objects) exist in a separate list to start, which is presorted. I'm sorting for draw order, which is to say, distance to camera, but it gets a little complicated if the 3-D geometry corresponding to the structure is complex.

Gonna implement stuff today. Wish me luck!
 

Feep

Banned
DID IT! Any arbitrary number of soldiers and structures, correctly sorted from back to front, and all shadows falling correctly on all surfaces.

This is really the culmination of a LOT of research, asking for help, and weeks of experimenting. Doing what I'm doing is extremely weird, so I'm really happy it worked out.

Early performance tests were fine. I ended up using a generic sort on the Soldiers (just implemented IComparable<> and let .Sort() do its thing), the structures are presorted in the scene, and then a single merge pass without recursion.

WOOOOOOOOOO

Here's what it looks like in the editor:

And here's the illusion magic:
 

_Isaac

Member
Are you guys familiar with n-tier architecture or just enterprise application architecture? I feel like I'm always doing it wrong.
 

missile

Member
If comparison cost is high, then mergestort will be better.

However, since you mentioned that the two lists may already be mostly presorted, you might want to consider Insertion Sort, which has O(n) complexity on a presorted list.
This.

And there are more.
Many sorting algorithms like Bubble sort, Shell sort, Tree sort, and Radix/
Bucket sort do also have an O(n) time on a nearly sorted list, with the
remarkable exception of Quick sort. See Knuth 'Sorting and Searching' page
106, 84, 422, 170, and 114, respectively.
 

Lucius86

Banned
I am an utterly new programmer who has decided to make a basic Windows Phone app for my own intrigue. I am using Visual Studio Express 2012 and C#.

Using some of the basic tutorials, I have created a settings page for my app where it saves to the local device the settings and displays them in the GUI XAML code. For example:

Text="{Binding PensionSetting, Mode=TwoWay, Source={StaticResource appSettings}}"

is connected to my Public string 'Pension Setting' in a specific .cs file.

But I know very little of C#, so wouldn't mind a guiding help for this new process I want to implement - I want to call this string value to then perform a mathematical function in another *.cs file.

I know how to convert string to integer (based on other code I have looked at), and know how to do this using elements in the associated XAML page. For e.g.:

private void Button_Click(object sender, RoutedEventArgs e)
{
AnswerBlock.Text = Convert.ToString(Convert.ToInt32(value_TextBox.Text) +
Convert.ToInt32(value2_TextBox.Text));
}


Pressing the button adds up and displays those two text box values. I want one of those text boxes to actually be a setting that I have saved, based off this example by Microsoft.

Anyone kind enough to provide me with pointers on how this can be done? Thanks in advance!
 

Datwheezy

Unconfirmed Member
DID IT! Any arbitrary number of soldiers and structures, correctly sorted from back to front, and all shadows falling correctly on all surfaces.

This is really the culmination of a LOT of research, asking for help, and weeks of experimenting. Doing what I'm doing is extremely weird, so I'm really happy it worked out.

Early performance tests were fine. I ended up using a generic sort on the Soldiers (just implemented IComparable<> and let .Sort() do its thing), the structures are presorted in the scene, and then a single merge pass without recursion.

WOOOOOOOOOO

Here's what it looks like in the editor:


And here's the illusion magic:

uFzY1qk.jpg
 

pompidu

Member
I'm starting to use mySql (ive taken a class using oracle before) and was wondering if companies prefer to use it through command line or workbench? Is there a big difference between using the two? Sorry if it sounds like a dumb question.
 
I'm starting to use mySql (ive taken a class using oracle before) and was wondering if companies prefer to use it through command line or workbench? Is there a big difference between using the two? Sorry if it sounds like a dumb question.

Varies on the company. I don't think there is a standard.
 
Is there a way to access a specific node in an already established linked list (C)? Or would I have to write a separate function for that?

What I ultimately want to do is go through a list to a specific index given by the user, insert a new node there (and put in info also given by the user) and then have that point to the rest of the list, with their data intact. I think this isn't too bad if accessing a node is similar to arrays or something.

My plan was to go to the node before the index and then make a new one, and have that point to the rest.
 

tokkun

Member
Is there a way to access a specific node in an already established linked list (C)? Or would I have to write a separate function for that?

What I ultimately want to do is go through a list to a specific index given by the user, insert a new node there (and put in info also given by the user) and then have that point to the rest of the list, with their data intact. I think this isn't too bad if accessing a node is similar to arrays or something.

My plan was to go to the node before the index and then make a new one, and have that point to the rest.

There is no "built-in" linked list in C, so there's no specific answer to this question. It depends on the implementation you are using.

For example, in std::list from the C++ STL, you can use find() and insert() to accomplish what you want without having to write your own method. However, linked lists are not intended to easily and efficiently support random access operations, so if you find yourself frequently needing to read data at random positions, then you should consider whether a linked list is really the right data structure to be using.
 
There is no "built-in" linked list in C, so there's no specific answer to this question. It depends on the implementation you are using.

For example, in std::list from the C++ STL, you can use find() and insert() to accomplish what you want without having to write your own method. However, linked lists are not intended to easily and efficiently support random access operations, so if you find yourself frequently needing to read data at random positions, then you should consider whether a linked list is really the right data structure to be using.

:/ I figured as much. Problem is I need to pass the info later on to a function that'll only take the list, so I cant switch structures. I think Ill try to write my own method and see where I get.

Thanks though.
 
In Visual Basic 2010, how do I have all the items in a listbox multiply to form one number?

Like if I have 5,7, and 2 in the listbox, how do I make it so that they all multiply with each other to get 70?
 

pompidu

Member
In Visual Basic 2010, how do I have all the items in a listbox multiply to form one number?

Like if I have 5,7, and 2 in the listbox, how do I make it so that they all multiply with each other to get 70?

Not sure if this is would work, found it on a website. Havent used vb in a long time.
Replace "MyList" with the name of your list.

Code:
Dim MyListSum as Double
MyListSum = 0
    For n = 0 to MyList.ListCount - 1    ' index is zero based
         MyListSum = MyListSum * Val(MyList.Item(n))
    Next n
 

Water

Member
:/ I figured as much. Problem is I need to pass the info later on to a function that'll only take the list, so I cant switch structures.
That restriction does not stop you from switching data structures. It means if you do switch to another structure, you have to convert to list before calling that function.

Without knowing something about the actual problem at hand, it's hard to say whether that would make sense. If this is classwork, probably not. Otherwise it would make sense pretty often. Linked lists are very rarely efficient structures for a given problem.
 
That restriction does not stop you from switching data structures. It means if you do switch to another structure, you have to convert to list before calling that function.

Without knowing something about the actual problem at hand, it's hard to say whether that would make sense. If this is classwork, probably not. Otherwise it would make sense pretty often. Linked lists are very rarely efficient structures for a given problem.

Its classwork the prof didnt finish but said will show up in some form on the exam (he wants us to know lists). I think Im getting somewhere though:

Code:
insert(int info, int x, node *list){
		int_node *new = malloc(sizeof(node));
		new->data = info;
		node *temp = malloc(sizeof(node));
		temp = NULL;
		int i;
		for(i = 0; i < (x-2); i++){
			temp = list->next;
		}
		new->next = temp->next;
		temp->next = new;
		return list;
}

So the goal is just to take some info given by the user and insert it at a position x in the list (x is like the index, and the list is also given by the user). What I tried to do is have a new node equal the node that comes before the index, and then make the node with the info given point to what the other node points to (cause I assume it will still point to the rest of the list, not sure though). This doesnt seem to work though, it compiles but then when i run it, it doesnt given an output lol.

Oh and the professor made the struct for us:

Code:
typedef struct node {
  int data;
  struct node *next;
} node;
 
There is a bug here. Can you see what it is?



What happens if x = 1?

1) Maybe set the next field of temp to NULL instead? Otherwise no, I don't?

2) Yeah I'm an idiot. But I think I would have to deal with that separately because I cant think of another algorithm that would fit with x = 1.
 

tokkun

Member
1) Maybe set the next field of temp to NULL instead? Otherwise no, I don't?

When you call malloc, that allocates some memory and returns a pointer to that memory, which you store in 'temp'.

You then immediately store NULL in 'temp', overwriting the pointer to this memory. The allocated memory is then stranded forever; because you have lost the pointer to it, there is no way it can be free'd. This is what's known as a memory leak. Every time your function is called, it loses some memory. Call it enough times and your program will run out of memory and crash.

You don't need to allocate any memory for 'temp' in your function. Just declare it and assign NULL to it.
 
When you call malloc, that allocates some memory and returns a pointer to that memory, which you store in 'temp'.

You then immediately store NULL in 'temp', overwriting the pointer to this memory. The allocated memory is then stranded forever; because you have lost the pointer to it, there is no way it can be free'd. This is what's known as a memory leak. Every time your function is called, it loses some memory. Call it enough times and your program will run out of memory and crash.

You don't need to allocate any memory for 'temp' in your function. Just declare it and assign NULL to it.

Ok I got it. This definitely helped as my final solution needed multiple declarations. Thank you sir.
 

Tamanon

Banned
I've been working in Google App Engine on some Python junk. Basic form validation that I'll need for later on.

Was bashing my head against a wall at why my program wouldn't run or even display on my localhost. Turns out there was an extra closing parenthesis about 80 characters offscreen on one line.:/ I was constantly reworking code to see if it was the code itself. Should've just used IDLE's "Check Module" function much earlier.

BTW, love Google App Engine. Going to make a lot of use out of that one.
 

squidyj

Member
Its classwork the prof didnt finish but said will show up in some form on the exam (he wants us to know lists). I think Im getting somewhere though:

Code:
insert(int info, int x, node *list){
		int_node *new = malloc(sizeof(node));
		new->data = info;
		node *temp = malloc(sizeof(node));
		temp = NULL;
		int i;
		for(i = 0; i < (x-2); i++){
			temp = list->next;
		}
		new->next = temp->next;
		temp->next = new;
		return list;
}

So the goal is just to take some info given by the user and insert it at a position x in the list (x is like the index, and the list is also given by the user). What I tried to do is have a new node equal the node that comes before the index, and then make the node with the info given point to what the other node points to (cause I assume it will still point to the rest of the list, not sure though). This doesnt seem to work though, it compiles but then when i run it, it doesnt given an output lol.

Oh and the professor made the struct for us:

Code:
typedef struct node {
  int data;
  struct node *next;
} node;

in your loop you're repeatedly assigning the same pointer to temp. It doesn't matter how many times it loops it's going to point to the same thing at the end. Not to mention I don't see why you're bothering to return your initial list pointer as you don't change anything about it, this is due to the fact that under the current implementation your node is always inserted after the first node.

In fact you don't even need that local temp pointer, you can use the list pointer from your arguments directly because it is just a copy of the list pointer you pass (copied onto the stack)
 
I've got a C++ question.. I'm doing a small Tower Defense game in C++ with SFML and I'm working on a class that animates a sf::Shape (the base class for rectangle shapes, circle shapes etc.) which is an abstract class (only has pure virtual functions). My problem right now is that I want to have the Animation class to use that sf::Shape base class as its member but it won't let me store a sf::Shape as a simple member variable because it's abstract. Now my idea was to store it as a pointer internally and return the reference (sf::Shape& shape) on the getter and setter functions. That compiled, but on run-time it crashed, complaining about a pure virtual function call I believe. How do I solve this?
Here's what the class header looks like:

Code:
class animation {
public:
	animation(sf::Vector2f start, sf::Vector2f end, sf::Shape* shape, float speed);
	sf::Shape& getShape(void);
	bool isFinished(void);
	void animate(void);
private:
	sf::Vector2f start;
	sf::Vector2f end;
	sf::Vector2f position;
	sf::Shape* shape;
	sf::Vector2f direction;
	float speed;
	float increment;
	bool finished;
};

The function call it complains about is this:
Code:
window.draw(animation.getShape());
Still pretty new to C++, so I appreciate any help.
 
Top Bottom