Thread: Reducing loops

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    175

    Reducing loops

    Hello,

    I got folllowing code working but need to work on optimization.

    Can anybidy help me in reducing number of loops?

    main()
    {
    int height,i,j,k;

    while ( getch() == 'c' )
    {
    printf("Enter the height of triangle\n");

    scanf("%d",&height);

    printf("Height = %d\n",height);

    k = 1;

    for ( i = 0; i < height; i++)
    {
    j = height-i-1;

    while ( j--)
    putch(' ');
    for ( j = 0; j < k ; j++)
    putch('*');
    printf("\n");
    k = k+2;
    }
    k = 1;
    for ( i = 0; i < height; i++)
    {
    j = i;

    while ( j--)
    putch(' ');
    for ( k = (((height-i)*2)-1); k ; k--)
    putch('*');
    printf("\n");
    k = k-2;
    }
    printf("\n\n\n Press 'c' to continue\n");
    }
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Please read this then edit your post accordingly.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    175

    here you go

    Code:
    main()
    {
    	int height,i,j,k;
    
    	while ( getch() == 'c' )
    	{
    		printf("Enter the height of triangle\n");
    
    		scanf("%d",&height);
    
    		printf("Height = %d\n",height);
    
    		k = 1;
    
    		for ( i = 0; i < height; i++)
    		{
    			j = height-i-1;
    			
    			while ( j--)
    				putch(' ');
    			for ( j = 0; j < k ; j++)
    				putch('*');
    			printf("\n");
    			k = k+2;
    		}
    		k = 1;
    		for ( i = 0; i < height; i++)
    		{
    			j = i;
    			
    			while ( j--)
    				putch(' ');
    			for ( k = (((height-i)*2)-1); k ; k--)
    			//for ( j = k ; j ; j--)
    				putch('*');
    			printf("\n");
    			k = k-2;
    		}
    		printf("\n\n\n Press 'c' to continue\n");
    	}
    }

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here's the first for loop rewritten to not use internal loops. See what you make of it and if you can apply its logic to the second loop.
    Code:
    for ( i = 0, k = 1; i < height; i++, k += 2)
    {
      j = height-i-1;
      printf ("%*.s", j, " ");
      /* This next line limits the height of the triangle based on the
       * number of *'s in the string.
       *  Therefore, it is not as flexible as a loop
       */
      printf ("%.*s", k, "****************************************");
      putchar('\n');
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here's an idea:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main( void )
    {
    	char *line = NULL;
    	int linesize, size, front, back;
    
    	printf("Enter the size of the pyramid: ");
    	if( scanf("%d",&size) != 1 )
    	{
    	    printf("You suck, go away.\n");
    		exit( 0 );
    	}
    
    	linesize = size * 2 + 1;
    	if( (line = malloc( sizeof( char ) * linesize )) == NULL )
    	{
    		printf("Your computer sucks, go away.\n");
    		exit( 0 );
    	}
    
    	memset( line, ' ', linesize );
    	line[linesize-1] = '\0';
    
    	for( front = back = size; size > 0; size--, front--, back++)
    	{
    		line[back] = line[front] = '*';
    		printf("%s\n", line );
    	}
    	free( line );
    	getchar( );
    	return 0;
    }
    Don't turn this in for homework please. I figured it's be an amusing work around. Enjoy.

    [edit]You don't actually need the 'front' line here. You could just use size.[/edit]

    Quzah.
    Last edited by quzah; 03-11-2003 at 08:29 PM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  2. strings and loops...
    By twomers in forum C Programming
    Replies: 5
    Last Post: 12-12-2005, 11:28 AM
  3. Evaluation of nested loops
    By Mister C in forum C Programming
    Replies: 2
    Last Post: 08-13-2004, 01:47 PM
  4. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM
  5. exiting loops with ease?
    By KingRuss in forum Game Programming
    Replies: 3
    Last Post: 09-24-2001, 08:46 PM