10-GOTO-10 C++ Coding challenge #1

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

31 Replies - 10869 Views - Last Post: 05 November 2012 - 02:29 PM

#16 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: 10-GOTO-10 C++ Coding challenge #1

Posted 18 December 2010 - 08:51 PM

View Postishkabible, on 17 December 2010 - 09:48 PM, said:

i didn't get how it worked for a while. i was scratching my head trying to figure out how it didn't just loop forever.


If the first comparision evaluates to false then the AND expression can never be true, so exits early.
Was This Post Helpful? 0
  • +
  • -

#17 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: 10-GOTO-10 C++ Coding challenge #1

Posted 18 December 2010 - 09:06 PM

i would have to agree with Adam on this one. he did not use any conditional expresion to make that control structure. c++0x will have ranged based for loop that can do the same thing.

on another note i expanded my inline assembly solution to work with MSVC
Spoiler


edit: also i figured out how it worked Adam. you can see in my earlier solution that i used it.

This post has been edited by ishkabible: 18 December 2010 - 09:08 PM

Was This Post Helpful? 1
  • +
  • -

#18 shad0wk1  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 139
  • Joined: 01-October 10

Re: 10-GOTO-10 C++ Coding challenge #1

Posted 18 December 2010 - 09:23 PM

This might be a bit too much to me, but I want to try too.
Spoiler

This post has been edited by shad0wk1: 18 December 2010 - 09:24 PM

Was This Post Helpful? 1
  • +
  • -

#19 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: 10-GOTO-10 C++ Coding challenge #1

Posted 18 December 2010 - 10:19 PM

View PostAdamSpeight2008, on 18 December 2010 - 10:37 PM, said:

@NickDMax Rules you seemed to have forgotten yourself, as your last two used for each.

I was sure of if it was.
False = 0, True=-1 (Not 0)
False = 0, True= 1

IComparable say result is < 0 for A being less than B (it doesn't say it has to be -1). So the result could be the result of a simple subtraction A-B. The sign Function normalises this to range -1 to +1.


The for_each is not a control structure, it is a template function. i.e. it is not on the list of banned structures + :?-operator.

Though you are correct that for_each is a pretty weak solution. So is using stl::max_element but the idea of the challenge was find different ways of approaching the problem rather than the usual direct approach. So I specifically allowed stl solutions include max_element because they are not on the list of banned structures.

As for the values of a boolean expression, they change from language to language. In QBasic and old VB a boolean expression generally evaluated to 0 or ~0 which is -1 -- a property that I often exploited back in my BASIC programming days. I don't know what VB.Net does with boolean expressions or even if they are a "thing" in .NET.
Was This Post Helpful? 0
  • +
  • -

#20 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: 10-GOTO-10 C++ Coding challenge #1

Posted 18 December 2010 - 11:08 PM

Recursion who need that crutch.
Spoiler

This post has been edited by AdamSpeight2008: 18 December 2010 - 11:09 PM

Was This Post Helpful? 1
  • +
  • -

#21 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: 10-GOTO-10 C++ Coding challenge #1

Posted 19 December 2010 - 06:13 AM

I previously did my maxPair as:
Spoiler


I got the idea reading this that you could also do:
Spoiler


Which has it's own charm.

Further, I notice there was another looping structure available that hadn't been forbidden. It was even in the title! So, rather than loop by all that stack pushing and jumping, I thought old school would be fun.

What you are about to see should only be done by trained professionals. Please, don't do this at home. Ever.

Also, only works in GCC.
Spoiler

Was This Post Helpful? 2
  • +
  • -

#22 Munawwar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 162
  • View blog
  • Posts: 457
  • Joined: 20-January 10

Re: 10-GOTO-10 C++ Coding challenge #1

Posted 19 December 2010 - 06:25 AM

Hey, I thought using assembly was cheating, even though NickDMax didn't specifically state that in the question.
ishkabible you are banned :P....
Just kidding. It's great to see people try out new things. This question even sparked the curiosity of a VB programmer :P. Nice!

This post has been edited by Munawwar: 19 December 2010 - 06:40 AM

Was This Post Helpful? 1
  • +
  • -

#23 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: 10-GOTO-10 C++ Coding challenge #1

Posted 19 December 2010 - 08:07 AM

Actually I was hoping that someone WOULD use assembly. The rules only ban those specific structures and 1 operator. Anything outside of those is perfectly ok.

Though if you use goto you have to live with yourself. I am sure that baavgai had to really concentrate on actually getting his figures to type "goto" into actual code! :)

@baavgai I wasn't aware the GCC could do that. I have always been irritated that C/C++ didn't have the ability to change goto labels into addresses. I am pretty sure that this CAN be done using inline assembly in VS but I have not worked up an example. However I believe that C/C++ labels are treated like assembly labels inside of assembly language so you could do anything you wanted with them. I will check it out.
Was This Post Helpful? 0
  • +
  • -

#24 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: 10-GOTO-10 C++ Coding challenge #1

Posted 19 December 2010 - 11:10 AM

inline assembly in MSVC can jump to labels defined outside the assembly block but gcc cant. the other thing is that inline asm is not cap sensitive so the following is ambiguous.

L1:
__asm {
   jMp l1
}
l1:



also notice that i used a capital M becuase im showing that it's not caps sensitive at all. i believe this is however fine if you leave it in c/c++ code. gcc wont allow you to jump to a label inside assembly nor form outside to a label inside assembly. also gcc isn't caps sensitive either.

edit: also i believe that the following code will allow you to reference a label in MSVC but i didn't test it
void(*mylable)();
asm {
   mov eax,somelable
   mov mylable,eax
}
somelable:



edit2: you may also want to use a void pointer for this instead of a function pointer because this code will expect a return statement somewhere and will most likly cause something to crash or loop endlessly. bottom line i have no clue what this will do, it's just a hunch.

This post has been edited by ishkabible: 19 December 2010 - 11:17 AM

Was This Post Helpful? 1
  • +
  • -

#25 Munawwar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 162
  • View blog
  • Posts: 457
  • Joined: 20-January 10

Re: 10-GOTO-10 C++ Coding challenge #1

Posted 19 December 2010 - 02:55 PM

So I was thinking what would be the shortest non-STL solution (shortest code). This is the best I could get:
Spoiler

This post has been edited by Munawwar: 19 December 2010 - 02:56 PM

Was This Post Helpful? 1
  • +
  • -

#26 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: 10-GOTO-10 C++ Coding challenge #1

Posted 19 December 2010 - 03:41 PM

lol nice one. not very readable but defiantly neat!! i found out that GCC will allow you to jump to other labels defined in other asm blocks and tried to use it to rework the control flow of my Radix Sort by no such luck. i may need to use a debugger for it.
Was This Post Helpful? 0
  • +
  • -

#27 shib.paul  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 1
  • Joined: 20-December 10

Re: 10-GOTO-10 C++ Coding challenge #1

Posted 20 December 2010 - 05:23 AM

View PostNickDMax, on 15 December 2010 - 09:14 AM, said:

I was instructed to spam this forum with a link to my C++ coding challenge. Of course everyone here reads my blog (10-GOTO-10) so of course this is redundant :P

The challenge:

Quote

Problem: Write a program that will find the largest element of an arbitrary array without using any conditional structures: banned { if/if-else/switch/for/while/do/?:-operator }

bonus: Display the array sorted from smallest to largest with the same conditions.

i.e. write the function max in this little program:

int main() {
    int array[] = { 3, 1, 4, 15, 9, 2, 6, 5, 35, 8, 97, 93, 23, 84, 62, 64, 33, 83, 27, 950, 28, 841, 971, 69, 39, 937, 510 };
    cout << "Largest element is: " << max(array) << endl;
    return 0;
}


...hints...


Post all answers in SPOILER tags!
[spoiler] [code] -- paste your answer like this -- [/code] [/spoiler]


There are already two STL solutions counted get yours in before all the different avenues are explored. :)


Here is a simple solution.


Spoiler

This post has been edited by NickDMax: 20 December 2010 - 07:46 AM
Reason for edit:: added code tags and spoiler tags

Was This Post Helpful? 1
  • +
  • -

#28 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: 10-GOTO-10 C++ Coding challenge #1

Posted 20 December 2010 - 07:34 AM

Well, if asm is on the table...

I've already used gotos. Here's the last silly.
Spoiler


I kind of burnt myself putting that one together. Bloody 64bit machine. Compiled in 32 bits for simple sanity.
Was This Post Helpful? 1
  • +
  • -

#29 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 176
  • Joined: 23-September 12

Re: 10-GOTO-10 C++ Coding challenge #1

Posted 23 September 2012 - 12:43 PM

Hey folks. This is my first post, so I hope I'm not tagging it wrong. This could probably be shortened quite a bit. Also, one could certainly do better than a bubble-sort.

Spoiler

Was This Post Helpful? 0
  • +
  • -

#30 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 176
  • Joined: 23-September 12

Re: 10-GOTO-10 C++ Coding challenge #1

Posted 23 September 2012 - 01:03 PM

Ack. Every uppercase 'B' is supposed to be a lowercase one. Some formatting glitch, it appears.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3