Thread: Brute Force

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Brute Force

    Since this opposes all languages It was better off placing in GD

    Anyway, am I right in saying that Brute Force Programming is more or less a polished term for "hard coding?" I mean I have not come across an incident when I have had to "force" the computer to do what I wanted.

    Or am I getting this all upside-down and back to front?
    Double Helix STL

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No, that's not right, I think.

    Hard coding refers to programming to very special cases, making the program inflexible and unmaintainable. However, that doesn't mean that there can't be some structure to it.

    Brute force programming is even worse, because in addition to being written to special cases, it's also written without any concept or structure.

    Not that there will be much difference in practice.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    http://en.wikipedia.org/wiki/Brute-force_search
    It's more to do with writing simple programs which try every possible combination of solutions until a solution (or the best solution) is found.

    For example, to count the frequency of a value in an unordered array, you would have to search every array element. This would be brute force.
    If on the other hand the array was sorted, then finding (and counting) the value becomes a much quicker operation.

    There are a whole bunch of problems - eg. http://en.wikipedia.org/wiki/P%3DNP_problem - which are in the hard to solve category, and for which there is no good answer which doesn't take universe lifetimes to compute.

    Crypto is another example where there are no answers better than brute force.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    No, I think you got it wrong, I found this definition for
    brute-force: http://cplus.about.com/od/glossar1/g/bruteforce.htm
    Hard Coding: http://en.wikipedia.org/wiki/Hard_coding

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    How about we put a simple example:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	char *szFile = "SaveOutput.dat"; /* Hardcoding */
    
    	....
    
    	return 0;
    }
    As opposed to:

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    	char *szFile;
    
    	if(argc > 1)
    	{
    		szFile = argv[1];
    	}
    
    	...
    
    	return 0;
    }
    Bruteforcing is something else. It has to do with trying every possible combination.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int x;
    
    	printf("Enter a number between 0 and 100 inclusive: ");
    	fflush(stdout);
    	scanf("%d", &x);
    
    	for(i=0;i<101;i++)
    	{
    		if(i == x)
    		{
    			printf("Your number is %d.\n", x);
    		}
    		else printf("I'm guessing your number is %d... Nope, I'm wrong.\n", i);
    	}
    
    	return 0;
    }
    That's bruteforcing.

    Otherwise, you could guess a little better. For example, start with ((HighLimit - LowLimit) / 2), etc. etc..

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Drawing terrain without any triangle optimization, frustum culling, or any other type of algorithm is an example of brute force.

    Using a quad tree or any other type of data reducing or render reducing algorithm is not brute force.

  7. #7
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    When "Brute Force" is used to apply to an algorithm, it means as suggested above, that every possible solution is considered (usually linearly) until a solution is found.
    However, when used in the context of programming (i.e. referring to coding style), it is interchangeable with "hard-coding".
    I.E. If you have to print the numbers 1 to 100 on screen and used 100 printf statements, I would say that you "hard-coded" or "brute forced" it. As you can see, brute-force used in this context is still highly similar to its "algorithm" context because, much like a brute-force algorithm, you are exclusively going through each step to reach the solution.

    Personally, I tend to use "hard-code" for the coding style and "brute force" for the algorithm style to avoid confusion, but, yes, I've heard both used for coding style many times.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Running a Brute Force Attack Simulation...
    By Junior89 in forum C++ Programming
    Replies: 31
    Last Post: 02-13-2011, 08:52 PM
  2. 2d array Brute force test
    By grimuth in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2010, 10:01 AM
  3. RFC: Brute force bitwise addition
    By AverageSoftware in forum C Programming
    Replies: 5
    Last Post: 07-21-2007, 04:13 PM
  4. Replies: 2
    Last Post: 03-21-2004, 08:21 PM
  5. Help : Brute Force String KeyGen
    By Tangy in forum C++ Programming
    Replies: 11
    Last Post: 03-11-2002, 09:01 PM