Like Tree3Likes
  • 1 Post By AndrewHunter
  • 1 Post By quzah
  • 1 Post By quzah

need help with a for loop

This is a discussion on need help with a for loop within the C Programming forums, part of the General Programming Boards category; i need to condense this piece of code down by 10 characters and I cannot think of a way to ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    35

    need help with a for loop

    i need to condense this piece of code down by 10 characters and I cannot think of a way to do that. I cannot change any of the variable names and changing any of the loop contents will change the function of the program. Is there a way that I can repeat a pair of loops just once? Thanks to anyone who can help.

    Code:
    for (row = 0; row < height; row = row++){
    
    	for (col = 0; col < row; col = col++){
    		printf(" ");
    	}
    	for (col = 0; col < width; col = col++){
    		printf("X");
    	}
    	for (col = 0; col < width; col = col++){
    		printf(" ");
    	}
    	for (col = 0; col < width; col = col++){
    		printf("X");
    	}
    
    	printf("\n");
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Take out "col =" everywhere except where you have "col = 0". When you increment, you just do this: x++ not x = x++. The same thing goes for your "row = row++". That will save you 30 characters.


    Quzah.
    Last edited by quzah; 07-12-2011 at 06:22 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,632
    quzah's suggestion also has the benefit of ensuring the code does not exhibit undefined behaviour.....

    In addition, since the third and fourth inner loops are identifical to the first and second, simply put the first pair of loops into another loop that is executed twice.

    If the body of a loop only consists of a single statement, no need to put braces around it. The braces will need to be introduced if you ever want a second statement in the loop though - that is a compromise of maintainability.

    Having a goal of minimising (or reducing) the number of characters in source code is, frankly, silly in about 99.9% of all circumstances. The compiler will probably not reward you with smaller object code, and the linker will not reward you with smaller executable files - unless you also tweak optimisation settings.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could do even more with a bit of math:
    Code:
    for each row
        for c = 0; c < cols * 4; c++
            if c < cols OR (c < cols * 3 && c > cols * 2)
                print space
            else
                print x
    You could shorten it even more with ? :, and even more again by using single character variable names. I'm not sure what your exact exercise goal was here. Were you shortening it because you were told it could be done better by a teacher, or just to see how short you can get it, or is there some piece of reference cod you are supposed to be mimicing?


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by quzah View Post
    Were you shortening it because you were told it could be done better by a teacher, or just to see how short you can get it, or is there some piece of reference cod you are supposed to be mimicing?

    Quzah.
    mimicking
    quzah likes this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by AndrewHunter View Post
    Stupid English language. Now C I can understand...


    Quzah.
    AndrewHunter likes this.
    Hope is the first step on the road to disappointment.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    for (row = 0; row < height; row++){
    
    	for (col = 0; col < row; col++){
    		printf(" ");
    	}
    	for (col = 0; col < width; col++){
    		printf("X");
    	}
    	for (col = 0; col < width; col++){
    		printf(" ");
    	}
    	for (col = 0; col < width; col++){
    		printf("X");
    	}
    
    	printf("\n");
    }
    Reduction = 20 characters.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    Stupid English language. Now C I can understand...
    Quzah.
    Are you really sure about that?

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    Reduction = 20 characters.
    Are you really sure about that?


    Quzah.
    AndrewHunter likes this.
    Hope is the first step on the road to disappointment.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    Are you really sure about that?
    Quzah.
    LOL... Touche!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 04-05-2011, 04:40 PM
  2. Help.. newbie for loop..stuck with the loop..
    By jochen in forum C Programming
    Replies: 15
    Last Post: 10-01-2007, 01:31 AM
  3. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 02:46 AM
  4. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 03:45 AM
  5. While loop ..crating an infanat loop
    By fmchrist in forum C Programming
    Replies: 7
    Last Post: 01-14-2006, 10:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21