Hello Sir,
I have an assignment to do, my question is:--

Given the set of integers, find the sum of all positive integers in it.
Input
t – number of test cases [t < 1000]
On each of next t lines given a integer N [-1000 <= N <= 1000]

Output
One integer equals to sum of all positive integers.

Source code of program should be less than 65 characters.
white space and tabs are not counted.

my code :-

x="%d";main(b,a,t)
{
           for(scanf(x,&t);t;t--)
           for(scanf(x,&a);a>0;a=0)b+=a; printf(x,b-1);
           return 0;
}

source code is of 93 characters.
I have reduced my code upto this from 122 characters , now I am unable to reduce it further, what should I do.
I have also used atoi(gets(&t)) instead of scanf(x,&t); but time limit exceeds.
return 0 is necessary in program otherwise it is giving NZEC runtime error.
I don't know what to do more to reduce my sorce code.
Help me Sir.

Recommended Answers

All 43 Replies

>>x="%d";main(b,a,t)

Huh? Don't know what that is.

>> for(scanf(x,&a);a>0;a=0)
Odd loop -- assuming a starts out to be greater than 0, the loop will execute exactly one time. Don't you want to change "a=0" to a-- ?


I would assume the code also has to compile cleanly ( 0 errors and 0 warnings).

code is running with 0 errors and giving correct outpur as required by the question.
Warnings are not to be considered.

yes a=0 is required in that for loop, since the loop has to be executed only once.

I need help in reducing the code to 65 bytes, or if there is any other code.
Please help me.

Hmm...

yes a=0 is required in that for loop, since the loop has to be executed only once.

I need help in reducing the code to 65 bytes, or if there is any other code.
Please help me.

Please stop begging. Posting to ask for help does not need a "help me" on every post. IMAO, it doesn't even need to be said at all. This is a help forum.

Since the loop must be executed only once, by definition it is not a loop and therefore can be removed, saving many more characters.

what compiler are you using??? The code you posted certainly will not compile with any standard C compiler because there is just too much missing, such as the data types of a, b, and c. And "x="%d"" is not even inside a function.

So don't tell us the program runs -- it won't even compile let alone run.

And Walt is right -- just delete the for loop statement and leave only the printf() statement.

Thank you for your suggestions.
I am using C(gcc 4.3.2) compiler.

I have deleted the second loop which was actually not a loop and now code is:--

x="%d";main(b,a,t)
{
           for(scanf(x,&t);t;t--)
           {scanf(x,&a);if(a>0)b+=a;} printf(x,b-1);
           return 0;
}

it reduced my code to 90 bytes and I have to reduce it to 65 byte.
what should I do now.

You've been asked a couple other questions.

You've been asked a couple other questions.

Sorry sir, I did not understand your meaning.

Twice x="%d" has been mentioned. Respond.

Twice x="%d" has been mentioned. Respond.

I learned this thing during the codefest-2011 event organised by IT-BHU,and this thing is running correctly in my compiler.
C(gcc 4.3.2).
I have tried a lot to reduce the code, but now its not possible to me.

x="%d";I learnt this thing from codefest-2011 event organised by IT-BHU.

this things are compling n running in my compiler c(gcc 4.3.2)

code is reduced to 90 bytes n i ve to reduce it to 65 bytes. i ve tried every possible effort from my side, now its not possible for me.

I don't have time to work with this right now, but I was wondering if a very short recursive function could help?

Could you rewrite this so it actually makes sense? With sample output?

Given the set of integers, find the sum of all positive integers in it.
Input
t – number of test cases [t < 1000]
On each of next t lines given a integer N [-1000 <= N <= 1000]

Output
One integer equals to sum of all positive integers.

I think the problem is these instructions are difficult to understand.

Given the set of integers, find the sum of all positive integers in it.
Input
t – number of test cases [t < 1000]
On each of next t lines given a integer N [-1000 <= N <= 1000]


Output
One integer equals to sum of all positive integers.
Score

Score equals to size of source code of your program except symbols with ASCII code <= 32.

Example

Input:
4
5
-5
6
-1

Output:
11

I said rewrite, not requote. Stating the exact same thing when told it doesn't make sense in the first place is completely worthless.

And an explanation of the sample output is helpful, since the numbers entered and the number output make no sense together.

commented: Can't you add 5 and 6? +0
commented: Last I checked, 4+5+6 is 15. +5

Sorry people, it is more than I can bear. We all agree that
1. The assignment is stupid
2. The code is ugly
3. OP doesn't have a clue
yet none of the above gives a reason to teach things you've not be asked for.

If you have nothing to say on the subject, it is perfectly OK to remain silent.

commented: Yes I CAN as 6 and 5 -- but I have to understand the problem to do that. -3

Sir, you can check the link:-

http://www.spoj.pl/problems/SIZECON/

(I'm going to weigh in, even though I probably shouldn't)

OP, what you have written is not legal syntax, it doesn't compile, and either you misunderstood the solution explained to you or the person explaining it to you had no idea what they were talking about.

gives a reason to teach things you've not be asked for

I understand your point, but I don't see what has made the response to this thread outside of the norm. Information was asked for, and it was not provided.

numbers entered and the number output make no sense together

The first number entered is the count of the numbers. They don't indicate that with a prompt or anything.

> Information was asked for, and it was not provided

May I respectfully disagree. If you ever participated in those stupid contests, you'd immediately realize what the OP is asking for. That said, OP doesn't seem to do it either.

Now I am going to play an advocate. All the information is here in the thread.
For example, Twice x="%d" has been mentioned was answered even before the question was asked: Warnings are not to be considered. That is, a code should comply to K&R spec, so x shall be treated as an extern int, and a warning of char*-to-int cast is irrelevant.
Same goes for reuse of char** arguments of main as ints.

The question asked was not "how can I make it compliant", and not even "what's wrong with it", but "how to reduce it". Nobody even attempted to understand the question, except Adak.

May I respectfully disagree.

You certainly may.

Nobody even attempted to understand the question, except Adak.

I think we all attempted to understand the question. I don't think this is the type of question that any of us are used to hearing around here.

or the person explaining it to you had no idea what they were talking about.

Perhaps I should soften that statement a bit. They obviously know exactly how much you can get away with when dealing with the compiler. Whether that's a good skill to have is probably debatable.

in the first input, user will enter a number suppose t
corresponding to that number i.e., t , a set of t different numbers are taken.
if the numbers are greater than 0 then they are added.
in the example posted:-
first input is 4
corresponding to 4, a set of 4 other numbers are taken i.e,( 5 , -5 , 6 , -1 )
since in the set only 5 and 6 are greater than 0, so they are added up.
and nothing to do with -5 and -1.
so finally output is 5+6=11
and the output displayed is 11.

> Information was asked for, and it was not provided

May I respectfully disagree. If you ever participated in those stupid contests, you'd immediately realize what the OP is asking for. That said, OP doesn't seem to do it either.

Wrong. When asked for clarification, he regurgitated the requirements originally posted. Since I quoted them, I already knew them, and stated I did not understand them. To repost them was a complete waste of time and utterly worthless. They are now posted in the thread at least 4 times. By your disagreement you are stating "the more times you post the same information the clearer the information becomes. Good luck with that.

in the first input, user will enter a number suppose t
corresponding to that number i.e., t , a set of t different numbers are taken.
if the numbers are greater than 0 then they are added.
in the example posted:-
first input is 4
corresponding to 4, a set of 4 other numbers are taken i.e,( 5 , -5 , 6 , -1 )
since in the set only 5 and 6 are greater than 0, so they are added up.
and nothing to do with -5 and -1.
so finally output is 5+6=11
and the output displayed is 11.

Finally!!!! It's only taken 22 posts and 3 days!

Wrong. When asked for clarification, he regurgitated the requirements originally posted. Since I quoted them, I already knew them, and stated I did not understand them. To repost them was a complete waste of time and utterly worthless. They are now posted in the thread at least 4 times. By your disagreement you are stating "the more times you post the same information the clearer the information becomes. Good luck with that.

Let's go through the logics, because you definitely lost me.
There was a statement: Information was asked for, and it was not provided
to which I disagreed by saying: Information was provided.
Can you clarify how does it come to the more times you post the same information the clearer the information becomes.?

I think we all attempted to understand the question. I don't think this is the type of question that any of us are used to hearing around here.

I suppose I was overly harsh. I expected that problems of this kind are too well known. My apologies.

Whether that's a good skill to have is probably debatable.

Agree 99%. The remaning 1% of disagreement is that this skill is undebatably bad.
A K&R1 language definition was obsoleted just to avoid such abuses.

Adak, I thought a lot about doing it from recursion of main() function with the help of ternary operator, but I am unsuccessful in the approach.
If you have time , will you please help me.

I'll try, but I have to warn you, I don't practice this kind of odd programming, and am not sure I can do anything better than what you have done already.

A slight improvement, but not a lot.

#define X scanf("%d",&
main(){int n,s=0,t;X t);for(;t-->0;){X n);s+=n;}printf("%d",s);}
//86 chars including five spaces, required.

The 5 spaces aren't counted against your program, according to the quiz site - putting this at 81 chars long.

Thank you adak for your code and giving me time.
your code is not checking for +ve numbers.
return 0 is necessary else giving NZEC error.
after editing your code, ur code is:-

#define X scanf("%d",&
main(s,n,t)
{ 
for(X t);t-->0;)
{X n); n>0?s+=n:n;
         }
      printf("%d",s-1);
return 0;
}

is giving 90 characters.
you were telling something about recursion,if recursion helps, I dont have much idea to use recursion in this place.
If you can do something with recursion.
please reply.

Two chars less:

#define X scanf("%d",&
main(){
  int n,s=0,t;X t);
  for(;t--;){
    X n);
    n>0?s+=n:0;
  }
  printf("%d",s);
  return 0;
}

I tried some other ways of doing this. This was the best I came up with, so far.;)

Now I'm curious what they have in the smallest programs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.