1. Post #241
    Gold Member
    Octave's Avatar
    January 2009
    2,530 Posts
    Instead of reversing the string at the end you could construct it in the opposite order. Also, I'm not sure you can declare static variables inside a method C++, although it has been a while since I last used it.
    I tested it, it does work. But I agree with ROBO_DONUT, this is a silly place to use recursion, although I assume he was doing it for a school assignment and needed to use recursion to do it.
    Reply With Quote Edit / Delete Mac United States Show Events

  2. Post #242
    Follow me on GitHub!
    Ziks's Avatar
    June 2011
    2,101 Posts
    It's a function, not a method.
    C# dev here, you might be able to understand why I made the mistake
    Reply With Quote Edit / Delete Windows 7 United Kingdom Show Events

  3. Post #243
    Gold Member

    March 2005
    3,028 Posts
    Oh, I didn't read that using recursion is a requirement. Nor did I know this was an assignment. I wouldn't have posted complete code if I knew that were the case.
    Well it's not the answer he needs, anyway, so I'll leave it.
    Reply With Quote Edit / Delete Windows 7 Show Events

  4. Post #244
    Leonmyster's Avatar
    June 2009
    536 Posts
    Okay, fixed some things, this works but the output is reversed, looking for a string reversal function now...
    string convertToBinary(int n)
    {
            static string buf;
            if (n == 0) return buf;
            buf.append((n % 2) ? "1" : "0");
            return convertToBinary(n/2);
            
    }
    Thanks a lot, I'll try to look for the reversal myself.

    This is indeed an assignment, and the use of recursion is required here.
    Reply With Quote Edit / Delete Windows 7 United States Show Events Friendly Friendly x 1 (list)

  5. Post #245
    Gold Member
    esalaka's Avatar
    July 2007
    10,241 Posts
    Also, I'm not sure you can declare static variables inside a method C++, although it has been a while since I last used it.
    Static storage duration. If you define a static variable outside of a function, it has static linking.
    Reply With Quote Edit / Delete Windows 7 Finland Show Events

  6. Post #246
    Gold Member
    TH3_L33T's Avatar
    June 2006
    1,367 Posts
    So I have my player using a vector2 but when it moves diagonal and but the speed seems to increase only when it goes diagonal... Anyone know how to fix the problem so it stays the same speed always.
    Reply With Quote Edit / Delete Windows 7 United States Show Events

  7. Post #247
    Gold Member

    March 2005
    3,028 Posts
    sqrt(2)/2
    Reply With Quote Edit / Delete Windows 7 Show Events

  8. Post #248
    Gold Member
    Z_guy's Avatar
    July 2005
    594 Posts
    So I have my player using a vector2 but when it moves diagonal and but the speed seems to increase only when it goes diagonal... Anyone know how to fix the problem so it stays the same speed always.
    Normalize the vector and then multiply it by a scalar (representing the speed you want it to have).
    Reply With Quote Edit / Delete Windows 7 Sweden Show Events

  9. Post #249
    RUBY OVERLORD
    swift and shift's Avatar
    November 2011
    2,115 Posts
    Avoid recursion when it isn't beneficial. If you use it in places where there aren't small upper bounds on the depth of the recursion, you risk a stack overflow. Not to mention the function call overhead.
    I find that recursion is often a really nice way of breaking up a problem.

    gcc can do tail call optimization anyway, so deliberately avoiding recursion is premature optimization
    Reply With Quote Edit / Delete Mac Australia Show Events

  10. Post #250
    I love my country, but I'm ashamed of my government.
    wakeboarderCWB's Avatar
    February 2010
    699 Posts
    Just trying to draw a simple ball in Java, but it's not drawing. Here is my Ball class:
    Code:
    import java.awt.geom.*;
    import java.awt.*;
    import javax.swing.*;
    
    public class Ball extends JPanel{
    	int width, height;
    	double ballX, ballY;
    	Ellipse2D ball = new Ellipse2D.Double(ballX, ballY, width, height);
    	
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		Graphics2D g2 = (Graphics2D) g;
    		
    		g2.fill(ball);
    	}
    	
    	public void setSizes(int w, int h){
    		width = w;
    		height = h;
    	}
    	
    	public void setCoords(double x, double y){
    		ballX = x;
    		ballY = y;
    	}
    		
    }
    Here is my main class:
    Code:
    import javax.swing.*;
    
    public class Main{
    	public static void main(String[] args){
    		JFrame frame = new JFrame("Pong");
    		Ball ball = new Ball();
    		
    		frame.setSize(600, 400);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setLocationRelativeTo(null);
    		frame.setLayout(null);
    		frame.setVisible(true);
    		frame.add(ball);
    		
    		ball.setSizes(50, 50);
    		ball.setCoords(20, 20);
    	}
    }
    The frame pops up, but the ball doesn't.
    Reply With Quote Edit / Delete Windows 7 United States Show Events

  11. Post #251
    Gold Member

    March 2005
    3,028 Posts
    I find that recursion is often a really nice way of breaking up a problem.

    gcc can do tail call optimization anyway, so deliberately avoiding recursion is premature optimization
    It's not an issue of deliberately avoiding recursion. In this specific case the problem is actually stated more clearly as a loop (at least in my opinion). Writing it recursively does not make the code significantly more terse/clear.

    Additionally, depending on a specific compiler optimization becomes an issue of portability when compiling code without that feature is likely to result in a binary that is unstable. That one function could use well over 1kB in the unoptimized recursive form (on the order of tens or so bytes for register storage, args, local vars, return pointer, all multiplied by the 32 bits the function has to iterate through), which is absurd for a function that just produces a string of 32 characters. This function might be OK because the n in O(n) stack space is small (just 32), but it could quickly become unacceptable if n was much larger. IMHO, it is only acceptable to write recursive functions when you can assume practical limits on the depth of the recursion -- like tree traversal, which is O(log n) (assuming it is balanced).
    Reply With Quote Edit / Delete Windows 7 Show Events

  12. Post #252
    Leonmyster's Avatar
    June 2009
    536 Posts
    So I figured out how to solve it without using strings. I still appreciate all your help.

    #include <iostream>
    #include <string>
    using namespace std;
    
    int convertToBinary(int);
    
    int main()
    {
    	int n;
    	cout << "Enter non-zero integer or 0 to stop: ";
    	cin >> n;
    	while (n != 0)
    	{
    		int binary = convertToBinary(n);
    		cout << "The binary of integer " << n << " is " << binary << ".\n";
    
    		cout << "Enter non-zero integer or 0 to stop: ";
    		cin >> n;
    	}
    	cout << "Bye!\n";
    }
    
    int convertToBinary(int n)
    {
    
    	if (n == 0)
    	return 0;
    	return 10*convertToBinary(n/2) + n%2;
             
    }
    
    Reply With Quote Edit / Delete Windows 7 United States Show Events Funny Funny x 1 (list)

  13. Post #253
    RUBY OVERLORD
    swift and shift's Avatar
    November 2011
    2,115 Posts
    So I figured out how to solve it without using strings. I still appreciate all your help.

    #include <iostream>
    #include <string>
    using namespace std;
    
    int convertToBinary(int);
    
    int main()
    {
    	int n;
    	cout << "Enter non-zero integer or 0 to stop: ";
    	cin >> n;
    	while (n != 0)
    	{
    		int binary = convertToBinary(n);
    		cout << "The binary of integer " << n << " is " << binary << ".\n";
    
    		cout << "Enter non-zero integer or 0 to stop: ";
    		cin >> n;
    	}
    	cout << "Bye!\n";
    }
    
    int convertToBinary(int n)
    {
    
    	if (n == 0)
    	return 0;
    	return 10*convertToBinary(n/2) + n%2;
             
    }
    
    an int can store a max of 11 'binary' bits
    Reply With Quote Edit / Delete Mac Australia Show Events

  14. Post #254
    Gold Member
    Smashmaster's Avatar
    April 2005
    1,549 Posts
    God, C programmers are weird.

    Code:
    public static String toBinary(int i)
    {
        String out = "";
    
        for (int b=0; b<32; b++)
        {
            if ((i & (1<<b)) == 0) out += '0';
            else out += '1';
        }
    
        return out;
    }
    EDIT: Oh, so it has to recurse? I should probably read before I code.
    Reply With Quote Edit / Delete Windows 7 United States Show Events

  15. Post #255
    Gold Member
    Lord Ned's Avatar
    April 2006
    3,702 Posts
    I'm looking to render objects with multiple sub-meshes.

    Should I create a single vertex-array for the model, and then just store multiple index-lists for the sub-meshes? Or should I store a single vertex array and a single index-list and then just store the start/stop for the sub-mesh? (I could see this creating a problem if the index list wasn't carefully built)

    I'm looking for more generalities of rendering 'models' that support multiple sub objects (with multiple sub-groups) than anything language specific.

    I just don't get how complicated rendering works.
    Reply With Quote Edit / Delete Windows 7 United States Show Events

  16. Post #256
    Gold Member
    TH3_L33T's Avatar
    June 2006
    1,367 Posts
    I found this image I know it is for website design but what I like is that thing on the top right is there anything like that for visual studio? I could of sworn I have seen some pictures of it in an older thread but can't remember where, would help me a ton!
    Reply With Quote Edit / Delete Windows 7 United States Show Events

  17. Post #257
    Gold Member
    mechanarchy's Avatar
    September 2009
    1,214 Posts
    I found this image I know it is for website design but what I like is that thing on the top right is there anything like that for visual studio? I could of sworn I have seen some pictures of it in an older thread but can't remember where, would help me a ton!
    I don't use visual studio myself but I've heard it said that MetalScroll is the visual studio equivalent for the code minimap feature in Sublime Text.
    Reply With Quote Edit / Delete Linux Australia Show Events

  18. Post #258
    Gold Member
    robmaister12's Avatar
    January 2008
    4,969 Posts
    Productivity Power Tools also has that type of a scrollbar.
    Reply With Quote Edit / Delete Windows 7 United States Show Events

  19. Post #259

    January 2012
    421 Posts
    RockScroll only works on 2005 and 2008 apparently, you must get the adding that trobmaister12 just linked to.

    It crashed VS the first time it opened, now it seems to be working, however I disabled everything cept the mapping scrollbar.
    Reply With Quote Edit / Delete Windows 7 Canada Show Events

  20. Post #260
    RUBY OVERLORD
    swift and shift's Avatar
    November 2011
    2,115 Posts
    God, C programmers are weird.

    Code:
    public static String toBinary(int i)
    {
        String out = "";
    
        for (int b=0; b<32; b++)
        {
            if ((i & (1<<b)) == 0) out += '0';
            else out += '1';
        }
    
        return out;
    }
    EDIT: Oh, so it has to recurse? I should probably read before I code.
    god C# programmers are weird

    Reply With Quote Edit / Delete Mac Australia Show Events Dumb Dumb x 1Winner Winner x 1 (list)

  21. Post #261
    Gold Member
    marcin1337's Avatar
    May 2006
    893 Posts
    So I have to make a program which uses MySQL,

    You input into the program your login and password, it needs to check if that user exists in a MySQL database.

    Anyone got any pointers on how to do this safely?
    Reply With Quote Edit / Delete Windows 7 United States Show Events

  22. Post #262
    Tamschi's Avatar
    December 2009
    3,604 Posts
    So I have to make a program which uses MySQL,

    You input into the program your login and password, it needs to check if that user exists in a MySQL database.

    Anyone got any pointers on how to do this safely?
    Minimum security would be store hashes instead of passwords.

    You can also read up on salting, wich is a commonly used technique to improve the security of hashed passwords.
    Reply With Quote Edit / Delete Windows 7 Germany Show Events Informative Informative x 1Agree Agree x 1 (list)

  23. Post #263
    Philly c's Avatar
    February 2008
    561 Posts
    I'm looking to render objects with multiple sub-meshes.

    Should I create a single vertex-array for the model, and then just store multiple index-lists for the sub-meshes? Or should I store a single vertex array and a single index-list and then just store the start/stop for the sub-mesh? (I could see this creating a problem if the index list wasn't carefully built)

    I'm looking for more generalities of rendering 'models' that support multiple sub objects (with multiple sub-groups) than anything language specific.

    I just don't get how complicated rendering works.
    I'm no expert but any of those things work. (Sub) models are really just a convention in your code, when it comes down to it, you're sending a command to draw a range of data. If it's convenient to put the whole thing in 1 vertex buffer and 1 index buffer, then go for it. If you're asking whether you can render an entire model of sub models in one go, then not really. You're almost definitely going to need to change something between draw calls otherwise they may as well be one model.
    Reply With Quote Edit / Delete Windows 7 United Kingdom Show Events Useful Useful x 1 (list)

  24. Post #264
    Gold Member
    Lord Ned's Avatar
    April 2006
    3,702 Posts
    I'm no expert but any of those things work. (Sub) models are really just a convention in your code, when it comes down to it, you're sending a command to draw a range of data. If it's convenient to put the whole thing in 1 vertex buffer and 1 index buffer, then go for it. If you're asking whether you can render an entire model of sub models in one go, then not really. You're almost definitely going to need to change something between draw calls otherwise they may as well be one model.
    Well my current theory is this:
    Each object in 3ds Max exports as an individual object to the SMD file format. This object counts as a 'bone', so the mesh can be keyframe-moved and you don't need to spend your time making a bone, rigging it and animating the bone. This is just a uh 'feature' of the SMD format I guess, so it stays. I think it'd be more work to find a way to merge all of the meshes anyways.

    Then within each object, each texture gets exported as a different sub-mesh (the object id stays the same, but the material on the triangle changes). Because of this each object can have sub-meshes. The theory in the end is that you'll draw a model, and lookup it's material, draw the sub-mesh, etc.

    I don't know if the final vertex count would be drastically different from storing individual vertex buffers - I imagine most of the time a vertex comes to a point where it switches texture it's probably got a different normal anyways so they're really two vertexes (that just happen to overlap).

    At this point it's easier to store an individual vertex list per sub-mesh, because that's how I read the SMD's. I guess I'll move forward with that and see how that goes.
    Reply With Quote Edit / Delete Windows 7 United States Show Events

  25. Post #265
    Gold Member
    marcin1337's Avatar
    May 2006
    893 Posts
    Minimum security would be store hashes instead of passwords.

    You can also read up on salting, wich is a commonly used technique to improve the security of hashed passwords.
    How safe would it be if I did it like this;

    in C#

    Client connects to SQLDatabase;
    Using SQLDatabase;
    Select login,password,( information about client ,IMPORTANT and private ) from Register
    where login = sLogin and password = sPassword;

    if recieve( Empty set (0.00 sec) )
    Please Retry

    else
    it will use the info recieved
    Reply With Quote Edit / Delete Windows 7 United States Show Events

  26. Post #266
    Tamschi's Avatar
    December 2009
    3,604 Posts
    How safe would it be if I did it like this;

    in C#

    Client connects to SQLDatabase;
    Using SQLDatabase;
    Select login,password,( information about client ,IMPORTANT and private ) from Register
    where login = sLogin and password = sPassword;

    if recieve( Empty set (0.00 sec) )
    Please Retry

    else
    it will use the info recieved
    That works for the login (if sPassword is the hash).

    If you want to protect the stored data, you can encrypt it with a key that is unique for every user and can't be derived from any information in the database.

    This is used in the industry to prodect stored data against employees and can be used to secure data where the database can be accessed by SQL.
    Encryption also helps with SQL injection and other attacks where the database is queried. (Sony didn't use encryption for most data, so passwords and personal data were immediately available to the hackers once they knew about the exploit and the table names.)
    Reply With Quote Edit / Delete Windows 7 Germany Show Events Friendly Friendly x 1Agree Agree x 1 (list)

  27. Post #267
    Gold Member

    March 2005
    3,028 Posts
    Make sure your whole authentication system runs through SSL. Without a secure transmission line, you can't do any real authentication because you can't trust that your Javascript has made it to the client without interception.

    Make sure you only store/transmit passwords as salted hashes. An unsalted hash is as good as the password itself. If you use unsalted hashes, an attacker can grab the hash and use it in any other site which uses unsalted hashes without modification.
    Reply With Quote Edit / Delete Windows 7 Show Events

  28. Post #268
    Gold Member
    Armandur's Avatar
    March 2009
    666 Posts
    Is anyone here interested in making some graphics for the game I'm working on?
    It's a version of an old viking game called Hnefatafl.

    Something like this, but from a top-down perspective:

    (Not my game)


    Resources

    A blank stone tile
    One tile called the throne. The kings starting position.
    A corner tile which the "King" is trying to get to.
    A tile to mark the starting positions of whites pieces.
    A tile to mark blacks starting position
    A black piece
    A white piece
    Whites king piece

    Currently I just use some simple grey tiles and Fleur-de-li's in different colors for the pieces :(
    Reply With Quote Edit / Delete Windows 7 Sweden Show Events

  29. Post #269
    dajoh's Avatar
    March 2011
    625 Posts
    So I have to make a program which uses MySQL,

    You input into the program your login and password, it needs to check if that user exists in a MySQL database.

    Anyone got any pointers on how to do this safely?
    If you do it like this, you can't release it to the public, the MySQL database login information would be accessable to everybody.
    Reply With Quote Edit / Delete Windows 7 Sweden Show Events Informative Informative x 1Agree Agree x 1 (list)

  30. Post #270
    Iskuri's Avatar
    January 2009
    340 Posts
    Hey guys, I'm writing a piece of code in the Processing thing that runs on Java that holds down certain letter on the keyboard using the "Robot" class. The issue I am having is that, while the keys can get pressed, and held down, the keyRelease function DOES NOT WORK, it just causes the button to be held down. I've searched online and cannot find help. This is the last piece of a fairly exciting project so I'd like it working. Here is my code that isn't working:

    void draw()
    {

    while (mainSerial.available() > 0)
    {
    int byteData = mainSerial.read();
    //ADD CONTROL COOOOODE
    //println("RUNNING");


    /*if(byteData == 127)
    {
    println("A has been Pressed!");
    }*/


    //println(byteData);


    String stringConvert = binary(byteData,8);

    //println(stringConvert);

    if(stringConvert.charAt(0) == '0')
    {
    controller.keyPress(KeyEvent.VK_Z);

    //println("BOOBEES");
    }
    else
    {
    controller.keyRelease(KeyEvent.VK_Z);
    }


    Thanks guys
    Reply With Quote Edit / Delete Windows 7 United Kingdom Show Events

  31. Post #271
    Wyzard's Avatar
    June 2008
    1,243 Posts
    Make sure you only store/transmit passwords as salted hashes. An unsalted hash is as good as the password itself.
    So is a salted hash, if that's what you're transmitting for authentication purposes. The point of hashing is that an attacker who steals a copy of the database can't learn the secret string that he must send as part of a login, because he can't reverse the hash function. If your site expects the user to send the hash (salted or not) rather than the real password, the attacker does know that, and can send it.

    Store salted hashes in the database. Transmit actual passwords (protected by SSL) over the network, to be hashed on the server and then compared against the stored value.
    Reply With Quote Edit / Delete Linux United States Show Events

  32. Post #272
    Gold Member

    March 2005
    3,028 Posts
    You can hash a hash, though, so you could do challenge-response auth without ever transmitting the plaintext password, which was sort of my point. i.e. you store the salted hash of the password in the database, send the client a unique per-session salt, have the client compute the password hash with the static site-specific salt, then again with the challenge per-session salt, and send that back to auth. No plaintext in the database, no plaintext transmitted, and an intercepted hash wouldn't be useful because it's tied to that one session.

    The weak link, then, becomes the client-side Javascript, which could be intercepted/modified without SSL. And if you're using SSL, plaintext isn't much of a concern anyway.

    So I don't really know what my point is. I'd probably just do it anyway and layer security on top of security because I'm irrational.
    Reply With Quote Edit / Delete Windows 7 Show Events Funny Funny x 1 (list)

  33. Post #273
    Gold Member
    Blueridge's Avatar
    July 2008
    2,320 Posts
    What's a clear way of removing multiple elements in an array?

    For a school assignment I have to make a class that holds an array of integers, and add methods that add to it, remove etc. Kinda like an ArrayList. The problem, however, is they want me to make a removeAll(int val) method that removes all occurrences of val from the list. For example, removeAll(4) would change

    {1, 2, 4, 6, 3, 4, 4, 6, 5, 6, 4} to
    {1, 2, 6, 3, 6, 5, 6, 0, 0, 0, 0}.

    For my regular remove() method, I'm using Apache Common's Lang ArrayUtils class to remove the element. Removing all elements with it doesn't work fully, for some reason. My current plan of attack is to find out how many times val appears in the array, and then call ArrayUtils.remove() that many times. But it gives me weird results, for instance shrinking the array. Any other way to attack this? I can't use ArrayList supposedly, and I doubt they'll check if I am or not, but it would be nice to figure out how to do this the way they want me to.
    Reply With Quote Edit / Delete Windows 7 United States Show Events

  34. Post #274
    Hi.
    reevezy67's Avatar
    July 2011
    4,403 Posts
    Just bought two books, C# 4.0 Pocket Reference and SQL in 10 Minutes.
    Reply With Quote Edit / Delete Windows 7 Australia Show Events

  35. Post #275

    January 2012
    421 Posts
    What's a clear way of removing multiple elements in an array?

    For a school assignment I have to make a class that holds an array of integers, and add methods that add to it, remove etc. Kinda like an ArrayList. The problem, however, is they want me to make a removeAll(int val) method that removes all occurrences of val from the list. For example, removeAll(4) would change

    {1, 2, 4, 6, 3, 4, 4, 6, 5, 6, 4} to
    {1, 2, 6, 3, 6, 5, 6, 0, 0, 0, 0}.

    For my regular remove() method, I'm using Apache Common's Lang ArrayUtils class to remove the element. Removing all elements with it doesn't work fully, for some reason. My current plan of attack is to find out how many times val appears in the array, and then call ArrayUtils.remove() that many times. But it gives me weird results, for instance shrinking the array. Any other way to attack this? I can't use ArrayList supposedly, and I doubt they'll check if I am or not, but it would be nice to figure out how to do this the way they want me to.
    Do a loop, remove all the values that you need to. However are you sure you're supposed to turn them into 0's? What if one of your values was a 0.

    Anyways, after you remove the values, you could sort it. QuickSort is a nice one ;)
    Reply With Quote Edit / Delete Windows 7 Canada Show Events

  36. Post #276
    Gold Member
    robmaister12's Avatar
    January 2008
    4,969 Posts
    RockScroll only works on 2005 and 2008 apparently, you must get the adding that trobmaister12 just linked to.

    It crashed VS the first time it opened, now it seems to be working, however I disabled everything cept the mapping scrollbar.
    I ended up disabling half the stuff... as much as I like the new Document Well 2010 Plus tabs, it was a bit buggy on my system so I reverted to the old tabs.

    Quick Find/Triple Click are amazingly useful though. And I set the scroll bar not to map mode, but to paint over the scrollbar with changes/caret location/find results/etc.
    Reply With Quote Edit / Delete Windows 7 United States Show Events

  37. Post #277
    Gold Member
    marcin1337's Avatar
    May 2006
    893 Posts
    Thanks for the tips guys, I'm going to be in charge of making a megaupload type software for clients to check their status/ upload files.

    Need all the security I can get!

    I have like 4 months to do this though :D
    Reply With Quote Edit / Delete Windows 7 United States Show Events Funny Funny x 10 (list)

  38. Post #278
    Tamschi's Avatar
    December 2009
    3,604 Posts
    You can hash a hash, though, so you could do challenge-response auth without ever transmitting the plaintext password, which was sort of my point. i.e. you store the salted hash of the password in the database, send the client a unique per-session salt, have the client compute the password hash with the static site-specific salt, then again with the challenge per-session salt, and send that back to auth. No plaintext in the database, no plaintext transmitted, and an intercepted hash wouldn't be useful because it's tied to that one session.

    The weak link, then, becomes the client-side Javascript, which could be intercepted/modified without SSL. And if you're using SSL, plaintext isn't much of a concern anyway.

    So I don't really know what my point is. I'd probably just do it anyway and layer security on top of security because I'm irrational.
    Wuala probably hashes passwords twice, at least that's the sanest approach for clientside symmetric encryption.

    Helps with plausible deniability too
    Reply With Quote Edit / Delete Windows 7 Germany Show Events

  39. Post #279
    Gold Member
    marcin1337's Avatar
    May 2006
    893 Posts
    What is so funny about my post :D?
    Reply With Quote Edit / Delete Windows 7 United States Show Events

  40. Post #280
    RUBY OVERLORD
    swift and shift's Avatar
    November 2011
    2,115 Posts
    What is so funny about my post :D?
    have you been living under a rock
    Reply With Quote Edit / Delete Mac Australia Show Events Agree Agree x 9 (list)