Shortening this code

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 1902 Views - Last Post: 20 September 2012 - 09:53 PM Rate Topic: -----

#16 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,089
  • Joined: 08-January 12

Re: Shortening this code

Posted 20 September 2012 - 08:49 AM

Well, the changed code works in Code::Blocks, which essentially is gcc. So yeah, it works in gcc. No idea about MSVC.
Was This Post Helpful? 0
  • +
  • -

#17 Tinix  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 55
  • Joined: 07-May 11

Re: Shortening this code

Posted 20 September 2012 - 09:15 AM

Thanks for all the help guys, it is at 99 now as the compiler that mattered was thankfully gcc so it now meets the expectations.
Was This Post Helpful? 0
  • +
  • -

#18 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,449
  • Joined: 09-June 09

Re: Shortening this code

Posted 20 September 2012 - 02:18 PM

Well since the cat is out of the bag, i'll have a go at it.

#include <iostream>

int main() {
	int i, k, l = 0;
	std::cin>>i;
	while(i-- && std::cin >> k) 
		l += (k > 0) * k;
	std::cout << l;
}


Was This Post Helpful? 1
  • +
  • -

#19 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,089
  • Joined: 08-January 12

Re: Shortening this code

Posted 20 September 2012 - 02:23 PM

Your word count is the same as mine. Better luck next time :)
Was This Post Helpful? 0
  • +
  • -

#20 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3160
  • View blog
  • Posts: 9,532
  • Joined: 05-May 12

Re: Shortening this code

Posted 20 September 2012 - 02:48 PM

Even with my handicapped MSVC compiler... 98 characters :)
Spoiler

Was This Post Helpful? 0
  • +
  • -

#21 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3160
  • View blog
  • Posts: 9,532
  • Joined: 05-May 12

Re: Shortening this code

Posted 20 September 2012 - 03:11 PM

And since GCC users can claim the 1 character advantage by not having to put the 0 within the ?:, here's something to give MSVC users an advantage at 95 characters because its header files accidentally defines _STD to be std::.
Spoiler


BTW, is it legal to just use main() instead of int main() ? It throws a warning much like GCC throws a warning about the missing 0 in ?:
Was This Post Helpful? 0
  • +
  • -

#22 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,089
  • Joined: 08-January 12

Re: Shortening this code

Posted 20 September 2012 - 03:24 PM

Skydiver, in that case, I would go for Turbo C++ :P
Spoiler

So, my code stands at first place with 85 char :)
Was This Post Helpful? 1
  • +
  • -

#23 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3160
  • View blog
  • Posts: 9,532
  • Joined: 05-May 12

Re: Shortening this code

Posted 20 September 2012 - 03:32 PM

Excellent find!

And now we know why schools insist on using TC++: It can be an excellent tool for playing Code Golf. :)

I'd rather stick with GCC and MSVC.
Was This Post Helpful? 0
  • +
  • -

#24 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3160
  • View blog
  • Posts: 9,532
  • Joined: 05-May 12

Re: Shortening this code

Posted 20 September 2012 - 04:38 PM

Okay back to mostly current standards compliant code, although it was nice to abuse the compiler headers. 97 characters for MSVC (and can be made 96 for GCC):
Spoiler

Was This Post Helpful? 1
  • +
  • -

#25 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,449
  • Joined: 09-June 09

Re: Shortening this code

Posted 20 September 2012 - 07:56 PM

Quote

97 characters for MSVC

you can still get 96 will MSVC :)
#include <iostream>

int main() {
	int i, k, l = 0;
	_STD cin>>i;
	for(;i-- && _STD cin >> k;)
		l += (k > 0) * k;
	_STD cout << l;
}


Was This Post Helpful? 0
  • +
  • -

#26 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3160
  • View blog
  • Posts: 9,532
  • Joined: 05-May 12

Re: Shortening this code

Posted 20 September 2012 - 08:29 PM

95 characters without relying on MSVC. Instead rely on the C++ standard where globals are initialized to zero.

Spoiler


(Subtract 3 if abusing the MSVC headers.)

This post has been edited by Skydiver: 20 September 2012 - 08:35 PM

Was This Post Helpful? 1
  • +
  • -

#27 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,449
  • Joined: 09-June 09

Re: Shortening this code

Posted 20 September 2012 - 09:12 PM

Well that's a nice little non-local initialization trick, I think ill steal it :)

93 characters (relying on MSVC though :( )
#include <iostream>

int i, k, l;
int main() {
	_STD cin>>i;
	while(i--)
		_STD cin>>k, l += (k > 0) * k;
	_STD cout << l;
}



This thread is starting to get off topic, maybe continue it in challenges?

This post has been edited by jjl: 20 September 2012 - 09:15 PM

Was This Post Helpful? 0
  • +
  • -

#28 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,089
  • Joined: 08-January 12

Re: Shortening this code

Posted 20 September 2012 - 09:20 PM

jjl, please stop trying. Skydiver's, if used with _STD, is smaller. And as I said, my trick is still the best in this block :)
#include <fstream.h>

int i, k, l;

main() { //Yeah, TC++ rocks! It works even without a type for main :)/>
    for(cin >> i; i--; cin >> k, k < 0 ? : l += k ) ;
    cout << l;
}

And the size of my code is 77 :)

DISCLAIMER : USE YOU TC++ AT YOUR OWN RISK!

This post has been edited by aresh: 20 September 2012 - 09:21 PM

Was This Post Helpful? 0
  • +
  • -

#29 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,449
  • Joined: 09-June 09

Re: Shortening this code

Posted 20 September 2012 - 09:30 PM

awesome ... :clap:
Was This Post Helpful? 0
  • +
  • -

#30 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3160
  • View blog
  • Posts: 9,532
  • Joined: 05-May 12

Re: Shortening this code

Posted 20 September 2012 - 09:53 PM

I started a thread in the C/C++ challenges since I don't know how to split off this thread starting back at post #18.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2