C++11 quiz

Moderator: Project members

Post Reply
Message
Author
User avatar
botg
Site Admin
Posts: 35547
Joined: 2004-02-23 20:49
First name: Tim
Last name: Kosse

C++11 quiz

#1 Post by botg » 2012-11-08 21:05

What does below code do? Try to solve it without compiling and executing it. And before you ask, yes, it is valid code.

Code: Select all

#include <iostream>

using namespace

   std;int
    main(){for(struct
       l{int i,k;??>
       j=[:>()

    <%decltype
        (l::i)i,j;cin>>i>>j;
        return l{
            i,j};
        }
        ();
        j.k!=0;
        j={j.k,j.i%*&j.k},j.k?
cout:
        cout<<j.i<<endl)
        {??(](){
    %>

    ();}

    return 0;
}

User avatar
MoonUnitZappa
550 Permission denied
Posts: 27
Joined: 2013-03-28 01:36

Re: C++11 quiz

#2 Post by MoonUnitZappa » 2013-05-30 10:26

botg wrote:What does below code do? Try to solve it without compiling and executing it. And before you ask, yes, it is valid code.

main() { for(struct
I am no expert but there is no corresponding } to the main {
I can not compile it with code:blocks & mingw GCC

As this has been here for some months now and no replies .. how about you tell us what it does ..
or at least what you believe it does .. :?
Definition: Politics -- Latin, from poly meaning many and tics meaning blood sucking parasites -- Tom Smothers
Power .. Cutting a long cable short.

User avatar
botg
Site Admin
Posts: 35547
Joined: 2004-02-23 20:49
First name: Tim
Last name: Kosse

Re: C++11 quiz

#3 Post by botg » 2013-05-30 10:30

I am no expert but there is no corresponding } to the main {
Oh, but there is. There are different ways to spell }
I can not compile it with code:blocks & mingw GCC
Try solving this without a compiler, compiling it ruins the fun.

Anyhow, your compiler isn't C++11 capable or you're compiling with C++11 support disabled. Get a newer version of your compiler or enable C++11 support.

With GCC, pass this: -std=c++11

User avatar
MoonUnitZappa
550 Permission denied
Posts: 27
Joined: 2013-03-28 01:36

Re: C++11 quiz

#4 Post by MoonUnitZappa » 2013-05-30 11:37

botg wrote:
Try solving this without a compiler, compiling it ruins the fun.

With GCC, pass this: -std=c++11
Well ... as I have no idea what "the fun" aspect of the solution is I won't spoil "the fun" for any one else.

I have managed to compile it by passing the "-std=c++11" as a GCC (v471) compiler option but
I am not sure if the result i get is the one you intended ... PM me if you are interested to see my result.

anyway - i learned something new today thanks to this quiz so at least that is good :)

edit: does "bo" stand for "bastard operator" by any chance ;)
Definition: Politics -- Latin, from poly meaning many and tics meaning blood sucking parasites -- Tom Smothers
Power .. Cutting a long cable short.

User avatar
botg
Site Admin
Posts: 35547
Joined: 2004-02-23 20:49
First name: Tim
Last name: Kosse

Re: C++11 quiz

#5 Post by botg » 2013-05-30 12:26

MoonUnitZappa wrote:edit: does "bo" stand for "bastard operator" by any chance ;)
Maybe. Maybe not :)

Betrayal
500 Command not understood
Posts: 2
Joined: 2013-10-17 13:37

Re: C++11 quiz

#6 Post by Betrayal » 2013-10-17 13:42

Uuuuuh i can't read digraphs and trigraphs. So with help of wikipedia i replaced all di/trigraphs into usual symbols.

1. put the struct right before the loop
2. deleted the useless lambda in loop-body [](){}();
3. changed decltype(l::i) to int
4. renamed l to structType
5. renamed j to obj
6. removed *& in obj.i%*&obj.k

so, this is much more readable now:

Code: Select all

#include <iostream>
using namespace std;

int main()
{
    struct structType
    {
        int i,k;
    };

    for(
            structType obj=[]()
            {
                int i,j;
                cin>>i>>j;
                return structType {i,j};
            }();

            obj.k != 0;

            obj= {obj.k, obj.i%obj.k}, obj.k
                ? cout
                : cout<<obj.i<<endl
        )
    {

    }

    return 0;
}

next a little more restructuring.
7. renamed i and k into first and second
8. defined obj before the loop without lampda-crap

Code: Select all

#include <iostream>
using namespace std;

int main()
{
    struct structType
    {
        int first,second;
    };

    structType obj;
    cin>>obj.first>>obj.second;

    for(
           ;
            obj.second != 0
           ;
            obj = {obj.second, obj.first%obj.second},
            obj.second ? cout : cout<<obj.first<<endl
        )
    {}

    return 0;
}
so and now trying to understand.
the loop-update part sets first = second and second = first%second.
when second==0 then first will be printed and the loop ends.

so due to this algorithm i think this programm calculates the greatest common divisor for first and second with euclid's algorithm

Betrayal
500 Command not understood
Posts: 2
Joined: 2013-10-17 13:37

Re: C++11 quiz

#7 Post by Betrayal » 2013-10-17 18:53

waiting for prize :D ;)

User avatar
botg
Site Admin
Posts: 35547
Joined: 2004-02-23 20:49
First name: Tim
Last name: Kosse

Re: C++11 quiz

#8 Post by botg » 2013-10-18 06:17

100 points.

User avatar
MoonUnitZappa
550 Permission denied
Posts: 27
Joined: 2013-03-28 01:36

Re: C++11 quiz

#9 Post by MoonUnitZappa » 2014-09-12 17:33

You guys can See The Matrix .. right ??? 8) 8)

Well done though !
:beer:
Definition: Politics -- Latin, from poly meaning many and tics meaning blood sucking parasites -- Tom Smothers
Power .. Cutting a long cable short.

User avatar
boco
Contributor
Posts: 26930
Joined: 2006-05-01 03:28
Location: Germany

Re: C++11 quiz

#10 Post by boco » 2014-09-12 17:36

*Sniff sniff* Thread necro, anyone? :mrgreen:
No support requests over PM! You will NOT get any reply!!!
FTP connection problems? Please read Network Configuration.
FileZilla connection test: https://filezilla-project.org/conntest.php
FileZilla Pro support: https://customerforum.fileZilla-project.org

User avatar
botg
Site Admin
Posts: 35547
Joined: 2004-02-23 20:49
First name: Tim
Last name: Kosse

Re: C++11 quiz

#11 Post by botg » 2014-09-12 18:38

You guys can See The Matrix .. right ???
I programmed a non-existing spoon once.


Let's revive the quiz.

New challenge:

Code: Select all

template<int ...>
struct a {};

template<int d, int ...c>
struct b : b<d - 1, d - 1, c...> {};

template<int ...c>
struct b<0, c...> {
	typedef a<c...> type;
};

template<typename e, typename g, typename h, int... i>
auto l(e&& k, g&& f, h&& t, a<i...> const&) -> decltype((std::forward<e>(k)->*std::forward<g>(f))(std::get<i>(std::forward<h>(t))...)) {
	return (std::forward<e>(k)->*std::forward<g>(f))(std::get<i>(std::forward<h>(t))...);
}

template<typename e, typename g, typename h, typename c = typename b<std::tuple_size<typename std::remove_reference<h>::type>::value>::type>
auto m(e&& k, g && f, h&& j) -> decltype(l(std::forward<e>(k), std::forward<g>(f), std::forward<h>(j), c())) {
	return l(std::forward<e>(k), std::forward<g>(f), std::forward<h>(j), c());
}
This is from the FZ repository, with the variable names obfuscated. Can you guess what this does and what it is being used for? No peeking please :)

Post Reply