Guess the code!

This is a discussion on Guess the code! within the C++ Programming forums, part of the General Programming Boards category; This one got me curious. I believe you are missing a couple of casts there. But regardless, I think it's ...

  1. #61
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    This one got me curious.

    I believe you are missing a couple of casts there. But regardless, I think it's an handy function that checks for =, <, and > between the arguments with just one return statement.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #62
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Mario F.
    I believe you are missing a couple of casts there.
    Ah, yes, the C++ board. It was originally C code.
    Quote Originally Posted by Mario F.
    But regardless, I think it's an handy function that checks for =, <, and > between the arguments with just one return statement.
    One that avoids the "obvious" implementation of a compare function commonly used with qsort, but is correct in its handling of integer overflow.
    http://groups.google.com/group/comp....da8537a?hl=en&
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #63
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    Ah! Excellent. I was wondering what possible applications it could have.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #64
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    I'll add a really simple one, but can be handy sometimes if you wanna avoid modulo operator

    what would you print at the question marks?

    Code:
    if (x & 1)
    {
      cout << "x==???" << endl;
    }
    else
    {
      cout << "x==???" << endl;
    }

  5. #65
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    Code:
    if (x & 1)
    {
      cout << "x== odd maybe" << endl;
    }
    else
    {
      cout << "x== even maybe" << endl;
    }
    It's less portable than the modulo operation.

  6. #66
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by Dave_Sinkula
    Ah, yes, the C++ board. It was originally C code.One that avoids the "obvious" implementation of a compare function commonly used with qsort
    Ah, I was wondering about the void pointers.



    I used to do the &1 trick until I got smart and realized that
    1) it doesn't work with 1's complement signed integers and
    2) the compiler optimizes %2 to it anyway where it works.
    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

  7. #67
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    you cannot say in general that compilers optimize that. Theres lots of compilers for embedded platforms where no attention is put in optimizing things at all.
    it's non portable though indeed

  8. #68
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    Speaking of which,

    (From a book someone lent me a few days ago)

    Which is friendlier and why?
    Code:
    Class X {
    public:
        X(): a(1), c(2) {} 
    private:
        int a;
        char b[4096];
        int c;
    };
    
    Class Y {
    public:
        Y(): a(1), c(2) {} 
    private:
        int a;
        int c;
        char b[4096];
    };
    Last edited by Mario F.; 11-10-2006 at 05:47 AM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #69
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    "Friendlier"?
    Unless int on the particular platform has very weird alignment requirements, they're pretty much exactly he same.
    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

  10. #70
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Yeah. What would the difference be?
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  11. #71
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    The general rule to ensure that the least amount of space is wasted due to alignment is to put all the things with the most restrictive alignment requirement at the beginning (eg doubles) and those with the least restrictive requirement (chars) at the end.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #72
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    Yep. But also, with a standard compiler, objects will be layed out in declaration order. X may incur in a data cache miss, due to a and c almost certainly being placed on different cache lines, separated by 4096 bytes.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  13. #73
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Okey, here comes a tough one, feel free to comment my code if it could have been written better:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <cfloat>
    #include <complex>
    using namespace std;
    
    int main() {
        double x=0, y = 1, i=0;
        do {
            x += y;
            i ++;
            y /= i;
        } while (y >= x*DBL_MIN);
        
        std::complex<double> z(-x, 0);
        double a;
        do {z = sqrt(z);} while (z.imag() > 0.00004);
        x = z.real()-1;
        y = z.imag();
        a = y/x;
        i = 1 - a*a;
        a *= 2;
        y -= x*x/2 * a;
        x -= x*x/2 * i;
        a = y/x;
        printf("%1.16lf\n", a);
        
        //Bonus part
        #define  myint  int
        myint n1, n2, n, t1, t2, t;
        t1 = n2 = 1; t2 = n1 = 0;
        for (i = 0; i < 4; i++) {
            x = floor(a);
            t = myint(x)*t1+t2;
            n = myint(x)*n1+n2;
            t2 = t1; t1 = t;
            n2 = n1; n1 = n;
            a = 1/(a-x);
        }
        printf("%d/%d = %1.9lf\n", t, n, (double)t/(double)n);
        system ("pause");
        return 0;
    }

  14. #74
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    as oppposed to obfuscating code.
    Mmm...kay.

    Code:
    int x = 25; //any value will do
    std::map <std::pair <int, int>, int> M;
    for(int m = 1; m < x; ++m)
    	for(int n = m+1; n < x; ++n)
    		if(n*n + m*m < x*x)
    			if(n*n - m*m < 2*m*n)
    				M[std::pair <int, int> (n*n - m*m, 2*m*n)] = n*n + m*m;
    			else
    				M[std::pair <int, int> (2*m*n, n*n - m*m)] = n*n + m*m;
    What does M contain? Spoiler:
    Code:
    for(std::map<std::pair <int, int>, int>::const_iterator i = M.begin(); i != M.end(); ++i)
    	std::cout << i -> first.first << " " << i -> first.second << " " << i -> second << "\n";

    Along that line,
    foo() can be passed to std::qsort() for sorting int[].
    Last edited by jafet; 11-11-2006 at 04:01 AM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  15. #75
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by jafet
    What does M contain? Spoiler:
    Ah, Pythagorean triples revisited. I thought it looked strangely familiar.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Page 5 of 7 FirstFirst 1234567 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  3. banking2
    By sweet2awy in forum C++ Programming
    Replies: 7
    Last Post: 08-01-2003, 03:01 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21