Shortening this code

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 Tinix  Icon User is offline

  • D.I.C Head

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

Shortening this code

Posted 20 September 2012 - 07:01 AM

Hello Everybody!
I've got a homework from my teacher to make shortest possible program that takes integer i as input, then there are i integers on the input that are to be read and print the sum of the non-negative ones. The catch is that the program should be as short as possible. Ideally under 100 characters(not including whitespace chars). I've got 108 characters, is there any way to make this one shorter? Code I've got so far:
#include <iostream>

int main()
{
	int i,j,k,l=0;
	std::cin>>i;
	for(j = 0; j < i; j++)
	{
		std::cin >> k;
		if(k>0)l+=k;
	}
	std::cout << l;
}


Thanks in advance!

Is This A Good Question/Topic? 1
  • +

Replies To: Shortening this code

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: Shortening this code

Posted 20 September 2012 - 07:11 AM

A couple of things you can initialize j prior to your loop then you wouldn't have to initialize it in the loop.
	
	int i,k,l=j=0;
	std::cin>>i;
	for(; j < i; j++)
	


Jim

This post has been edited by jimblumberg: 20 September 2012 - 07:14 AM

Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is online

  • Head MFIC
  • member icon

Reputation: 5061
  • View blog
  • Posts: 26,432
  • Joined: 10-May 07

Re: Shortening this code

Posted 20 September 2012 - 07:13 AM

I would write the above code into a function defined in a custom header file, include the header file, & just call the function inside of main.
Was This Post Helpful? 0
  • +
  • -

#4 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 - 07:28 AM

View Postjimblumberg, on 20 September 2012 - 04:11 PM, said:

A couple of things you can initialize j prior to your loop then you wouldn't have to initialize it in the loop.
	
	int i,k,l=j=0;
	std::cin>>i;
	for(; j < i; j++)
	


Jim

This gives undeclared identifier error and also the header file solution would not work because it would still be 100+ chars.
Was This Post Helpful? 0
  • +
  • -

#5 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 - 07:38 AM

Jim, for that to work, l and j must be defined earlier. You cannot do that in a declaration statement.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: Shortening this code

Posted 20 September 2012 - 07:43 AM

Okay, how about eliminating j altogether?
	int i,k,l=0;
	std::cin>>i;

	while(i--)


Jim
Was This Post Helpful? 1
  • +
  • -

#7 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 - 07:47 AM

I like this challenge of shortening the code. Let me try
Spoiler

Anybody who can beat me?

AARGH, Jim beat me to it :cry2:

This post has been edited by aresh: 20 September 2012 - 07:50 AM

Was This Post Helpful? 0
  • +
  • -

#8 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1616
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: Shortening this code

Posted 20 September 2012 - 08:11 AM

untested but the idea is sound. unless the the comma operator doesn't define the order of evaluation...then this could do some very unexpected things.
Spoiler

This post has been edited by ishkabible: 20 September 2012 - 06:30 PM

Was This Post Helpful? 0
  • +
  • -

#9 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:20 AM

ish, it doesn't even compile.
|7|error: lvalue required as left operand of assignment|

Also, learn to use code tags when posting code :P

This post has been edited by aresh: 20 September 2012 - 08:22 AM

Was This Post Helpful? 0
  • +
  • -

#10 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 - 08:23 AM

View Postjimblumberg, on 20 September 2012 - 04:43 PM, said:

Okay, how about eliminating j altogether?
	int i,k,l=0;
	std::cin>>i;

	while(i--)


Jim

This helped, I'm at 101 characters now. I' going to try to squish it a bit more.
Was This Post Helpful? 0
  • +
  • -

#11 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:25 AM

Well, as I demonstrated, you can remove the if statement to replace it by ?: since it will bring the number of characters below 100.
Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: Shortening this code

Posted 20 September 2012 - 08:27 AM

Does using namespace std; and the #include count towards the number of characters?

Jim
Was This Post Helpful? 0
  • +
  • -

#13 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 - 08:32 AM

View Postjimblumberg, on 20 September 2012 - 05:27 PM, said:

Does using namespace std; and the #include count towards the number of characters?

Jim

Unfortunately yes.
@Aresh: Both VS and GCC throw compiler error at yours solution. I tried that one before.
Was This Post Helpful? 0
  • +
  • -

#14 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:37 AM

What the hell? It won't let me leave the second condition blank! Well, then it must be modified to
k<0?:l+=k;

Slight change of position, and it works :)
Was This Post Helpful? 0
  • +
  • -

#15 Skydiver  Icon User is offline

  • Code herder
  • member icon

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

Re: Shortening this code

Posted 20 September 2012 - 08:48 AM

It may work for GCC, but not for MSVC.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2