Thread: doubt in C--(reducing the length of code)

  1. #16
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    instead of worrying bout size of the code, is it not better to worry about it's space and time complexity even more?
    It is not who I am inside but what I do that defines me.

  2. #17
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Code:
    int main(void)
    {
        system("main.pl");
        return 0;
    }
    And write the rest of the program in Perl. You'll be surprised to see how small you can go in there.


    Check out my sig. Guess what it does. 63 characters including newline... shortest known.


    If you want people around you to stop making sarcastic and snide remarks, go work for an embedded systems manufacturer. People will actually endorse your viewpoints and your efforts will be appreciated!
    Last edited by jafet; 10-14-2006 at 01:54 AM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  3. #18
    Registered User
    Join Date
    Oct 2006
    Posts
    16
    let me get it straight...

    This one is meant for a prg which chk the source code length...

    i have 2 question...soln for 1st one was..

    Code:
    a,t=10,c;main(){while(t--){c=1;while((a=getchar())&64)c<<=(a&7)==4|a==70;printf("%d ",c);}}
    (91 characters) which got accepted...
    but for the 2nd question
    Code:
    #define b scanf("%d",&
    t,a,n; main(){ for(b t);t--;a+=n*(n>0)) b n); printf("%d",a); return 0;}
    (84 char)this works but if i remove "return 0;" it doesnt work i want to know why thats it...

    In first case i didnt hv return 0; and it worked
    but in second if i didnt hv return 0; it is not working why is it so...

  4. #19
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    But my question is why do u want to write the whole code which in just one line, which looks so messy and which is not recommended

    ssharish2005

  5. #20
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Stupidity rules
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  6. #21
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Code:
    #define b scanf("%d",&
    t,a,n; main(){ for(b t);t--;a+=n*(n>0)) b n); printf("%d",a); return 0;}
    Why would you write code like that ? To make it difficult for everyone but you to understand it ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  7. #22
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I think he wants to show someone how "good" program can be done in one line.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  8. #23
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Perhaps. But it doesn't make him 1337.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  9. #24
    Registered User
    Join Date
    Oct 2006
    Posts
    16
    Cmon its not like that...its a contest question(http://spoj.pl challenge problem)...and they check the efficency of ur code length and obviously right answer too...thatsy...

    and btw nobody answered my question...???!!!

  10. #25
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Are they checking efficiency or code length ?

    Because your code length doesn't affect efficiency.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  11. #26
    Registered User
    Join Date
    Oct 2006
    Posts
    16
    ITS just code length....So whats the answer for my question...???why the 2nd soln requires "return 0;"

  12. #27
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    I'd say it's because most compilers default main as an int function, hence it expects it to return an integer value. The question rather should be why you didn't get a warning when you didn't have return....
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  13. #28
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    You should NOT give them that code, because removing return 0; and int main() may not be supported by every compiler. And if they compile it and it won't compile, then you're eliminated, am I right?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  14. #29
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think, it's just stupid to measure code lenght in lines/characters. So if I chose to use descriptive names, that would automatically make me lose? What they should be counting are the ;-s, how many instructions your code has - or something coding related for that matter.

    What does this mean in the code: a,t=10,c; ?

  15. #30
    Registered User
    Join Date
    Oct 2006
    Posts
    16
    @anon..

    the c file automatically takes the variables declared as integer type...

    @maxorator

    the code gets complied but gives a NZEC a non zero error code...and also the output is right...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange string behavior
    By jcafaro10 in forum C Programming
    Replies: 2
    Last Post: 04-07-2009, 07:38 PM
  2. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM