how to make a abc pyrmid

Viewed 135

i wrote a code and i dont know how to continue from here .. i want to build an abc pyramide from start to back (a ,aba,abcba..etc) for example

a
a b a
a b c b a
a b c d c b a 

etc..

this is my code , thanks :)

void aba(int lines)
{
    //int i = 97;
    char a = 97;
    
    for (int i = 97;i <= lines + 97;i++)
    {
        printf("%c",i);
        for (int j = 97;j <= lines + 97;j++)
        {
            if (j == i)
                break;
            
            printf("%c%c", j,i);
        }

        printf("\n");
    }

void main()
{
aba(3);
}
 
3 Answers

As it is homework, I give you a hint, not the solution:

You should observe the pattern: All lines start with a.

When you numerate the lines from top to bottom starting with 0, the line 0 has an a and 0 additional characters.
line 1 starts with the a and has 1 more upcounting character (b) and then 1 downcounting character (a).
line 2 starts with the a and has 2 more upcounting character (b c) and then 2 downcounting character (b a).
...
So, line i starts with the a and has i more upcounting characters (b c d ....) and then i downcounting characters (... c b a).

Now you have to translate that into code.

void aba(int n)
{
    for (int i = 0; i < n; i++)
    {
        //print here what all lines have in common
        for (int j = 1; j <= i; j++)
        {
            //print the upcounting characters
        }
        for (int j = 1; j <= i; j++)
        {
            //print the downcounting characters
        }
    }
}

If you have problems solving a given task always try to split it into smaller jobs:

Start by printing an a for every line of your pyramid:

void aba(int lines) {
    for (int i = 0; i < lines; i++) {
        printf("%c\n", a);
    }
}

Now you want to have a pyramid with one character in the first line, 3 in the second, 5 in the third and so on. You can produce this sequence using (i * 2) + 1:

void aba(int lines) {
    for (int i = 0; i < lines; i++) {
        for (int j = 0; j < (i * 2) + 1 ; j++) {
            printf("%c", a);
        }
        printf("\n");
    }
}

The last step would be to increase the character in the first half of the line and decrease it in the last half:

void aba(int lines) {
    for (int i = 0; i < lines; i++) {
        char a = 'a';
        for (int j = 0; j < (i * 2) + 1 ; j++) {
            printf("%c", a);
            if (j <= i - 1)
                a += 1;
            else
                a -= 1;
        }
        printf("\n");
    }
}

For starters pay attention to that according to the C Standard the function main without parameters shall be declared like

int main( void )

It is not necessary that lower case letters follow each other without gaps. For example this in not correct for the EBCDIC table.

Here is a demonstrative program that shows how the required output can be obtained.

#include <stdio.h>

int main(void) 
{
    const char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
    const size_t N = sizeof( alphabet ) - 1;
    
    while ( 1 )
    {
        size_t n = 0;
        
        printf( "Enter a non-negative number not greater than %zu (0 - exit ): ", N );
        
        if ( scanf( "%zu", &n ) != 1 || n == 0 ) break;

        n %= N + 1;
        
        putchar( '\n' );
        
        for ( size_t i = 0; i < n; i++ )
        {
            const char *p = alphabet;
            
            do { printf( "%c ", *p++ ); } while ( p != alphabet + i + 1 );
            
            if ( --p != alphabet )
            {
                do  { printf( "%c ", *--p ); } while ( p != alphabet );
            }               
            
            putchar( '\n' );
        }
        
        putchar( '\n' );
    }
    
    return 0;
}

The program output might look like

Enter a non-negative number not greater than 26 (0 - exit ): 26

a 
a b a 
a b c b a 
a b c d c b a 
a b c d e d c b a 
a b c d e f e d c b a 
a b c d e f g f e d c b a 
a b c d e f g h g f e d c b a 
a b c d e f g h i h g f e d c b a 
a b c d e f g h i j i h g f e d c b a 
a b c d e f g h i j k j i h g f e d c b a 
a b c d e f g h i j k l k j i h g f e d c b a 
a b c d e f g h i j k l m l k j i h g f e d c b a 
a b c d e f g h i j k l m n m l k j i h g f e d c b a 
a b c d e f g h i j k l m n o n m l k j i h g f e d c b a 
a b c d e f g h i j k l m n o p o n m l k j i h g f e d c b a 
a b c d e f g h i j k l m n o p q p o n m l k j i h g f e d c b a 
a b c d e f g h i j k l m n o p q r q p o n m l k j i h g f e d c b a 
a b c d e f g h i j k l m n o p q r s r q p o n m l k j i h g f e d c b a 
a b c d e f g h i j k l m n o p q r s t s r q p o n m l k j i h g f e d c b a 
a b c d e f g h i j k l m n o p q r s t u t s r q p o n m l k j i h g f e d c b a 
a b c d e f g h i j k l m n o p q r s t u v u t s r q p o n m l k j i h g f e d c b a 
a b c d e f g h i j k l m n o p q r s t u v w v u t s r q p o n m l k j i h g f e d c b a 
a b c d e f g h i j k l m n o p q r s t u v w x w v u t s r q p o n m l k j i h g f e d c b a 
a b c d e f g h i j k l m n o p q r s t u v w x y x w v u t s r q p o n m l k j i h g f e d c b a 
a b c d e f g h i j k l m n o p q r s t u v w x y z y x w v u t s r q p o n m l k j i h g f e d c b a 

Enter a non-negative number not greater than 26 (0 - exit ): 10

a 
a b a 
a b c b a 
a b c d c b a 
a b c d e d c b a 
a b c d e f e d c b a 
a b c d e f g f e d c b a 
a b c d e f g h g f e d c b a 
a b c d e f g h i h g f e d c b a 
a b c d e f g h i j i h g f e d c b a 

Enter a non-negative number not greater than 26 (0 - exit ): 4

a 
a b a 
a b c b a 
a b c d c b a 

Enter a non-negative number not greater than 26 (0 - exit ): 3

a 
a b a 
a b c b a 

Enter a non-negative number not greater than 26 (0 - exit ): 2

a 
a b a 

Enter a non-negative number not greater than 26 (0 - exit ): 1

a 

Enter a non-negative number not greater than 26 (0 - exit ): 0

The part of the program that outputs the pattern can be placed in a separate function such a way that the pattern can be outputted in any file.

Here you are.

#include <stdio.h>

FILE * display_pattern( size_t n, FILE *fp )
{
    static const char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
    static const size_t N = sizeof( alphabet ) - 1;
    
    n %= N + 1;
    
    for ( size_t i = 0; i < n; i++ )
    {
        const char *p = alphabet;
            
        do { fprintf( fp, "%c ", *p++ ); } while ( p != alphabet + i + 1 );
            
        if ( --p != alphabet )
        {
            do  { fprintf( fp, "%c ", *--p ); } while ( p != alphabet );
        }               
            
        fputc( '\n', fp );
    }
    
    return fp;
}


int main(void) 
{
    while ( 1 )
    {
        size_t n = 0;
        
        printf( "Enter a non-negative number (0 - exit ): " );
        
        if ( scanf( "%zu", &n ) != 1 || n == 0 ) break;
        
        putchar( '\n' );
        
        display_pattern( n, stdout );
        
        putchar( '\n' );
    }
    
    return 0;
}

Again the program output might look like

Enter a non-negative number (0 - exit ): 5

a 
a b a 
a b c b a 
a b c d c b a 
a b c d e d c b a 

Enter a non-negative number (0 - exit ): 4

a 
a b a 
a b c b a 
a b c d c b a 

Enter a non-negative number (0 - exit ): 3

a 
a b a 
a b c b a 

Enter a non-negative number (0 - exit ): 2

a 
a b a 

Enter a non-negative number (0 - exit ): 1

a 

Enter a non-negative number (0 - exit ): 0

If actually you want to output indeed a pyramid then the function can look as it is shown in the next demonstrative program.

#include <stdio.h>

FILE * display_pattern( size_t n, FILE *fp )
{
    static const char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
    static const size_t N = sizeof( alphabet ) - 1;
    
    n %= N + 1;
    
    for ( size_t i = 0; i < n; i++ )
    {
        const char *p = alphabet;
            
        fprintf( fp, "%*c ", ( int )( 2 * ( n - i) - 1 ), *p++ );           
        while ( p != alphabet + i + 1 ) fprintf( fp, "%c ", *p++ );
            
        if ( --p != alphabet )
        {
            do  { fprintf( fp, "%c ", *--p ); } while ( p != alphabet );
        }               
            
        fputc( '\n', fp );
    }
    
    return fp;
}


int main(void) 
{
    while ( 1 )
    {
        size_t n = 0;
        
        printf( "Enter a non-negative number (0 - exit ): " );
        
        if ( scanf( "%zu", &n ) != 1 || n == 0 ) break;
        
        putchar( '\n' );
        
        display_pattern( n, stdout );
        
        putchar( '\n' );
    }
    
    return 0;
}

Now the program output might look like

Enter a non-negative number (0 - exit ): 5

        a 
      a b a 
    a b c b a 
  a b c d c b a 
a b c d e d c b a 

Enter a non-negative number (0 - exit ): 4

      a 
    a b a 
  a b c b a 
a b c d c b a 

Enter a non-negative number (0 - exit ): 3

    a 
  a b a 
a b c b a 

Enter a non-negative number (0 - exit ): 2

  a 
a b a 

Enter a non-negative number (0 - exit ): 1

a 

Enter a non-negative number (0 - exit ): 0
Related