2
\$\begingroup\$

Expected Input: Enter the number of rows: 5

Expected Output:

        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

I have solved this problem in this way->

//Write a program to build a pyramid with uses of alphabets
#include <stdio.h>
int
main(void){

//Put variables here
int space,num_of_rows,p=1,t=0;
char alphabet[100]={'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'};

printf("Enter the number of rows : ");
//Getting the input from the user
scanf("%d",&num_of_rows);

//Handling total row issues
for(int i=0; i<num_of_rows; i++){


//Handling space issues
for(space=0; space<num_of_rows-i; space++)
    printf(" ");

    //Handling alphabets
    for(int j=1; j<=p; j++){

            //To construct the first row and first position of each of the row as A
            if(i==0 || j==1){

                    alphabet[t]='A';

            }
            //To construct the last position of each of the row as A
            else if(j==p){

                    alphabet[t]='A';


            }
            //Handling the middle position issues
            else if(i+1>=j){

                    alphabet[t]++;

                }

            else{
                    alphabet[t]--;


                }
            printf("%c",alphabet[t]);



    }
    printf("\n");
    p+=2;

   }
 }

Now what I want to accomplish from you: How can I simplify my solution?. Will I face any problem for some conditions? Is it possible to solve this problem without using an array? Then what will be the strategy?. Thank you in advance.

\$\endgroup\$
1
  • \$\begingroup\$ If you wish to improve the algorithm, then Google "Pascal's Triangle". (You just print letters instead of digits.) \$\endgroup\$ – Lundin Sep 23 '20 at 13:32
6
\$\begingroup\$

General Observations

The code works as long as the user puts in correct limited values.

The indentation is a little off.

You can decrease the vertical spacing quite a bit, most of the blank lines aren't necessary.

Answers

Will I face any problem for some conditions?

If a user enters a negative number or a number greater than 100 this program will have problems with indexing. Always check user input for correctness and possible error conditions. The program will actually have problems if a user enters a number greater than 52 because there are only 26 letters in the alphabet.

How can I simplify my solution?.

You could break the program up into functions, it's not quite necessary with a program this simple, but it is getting close. Two obviously separate functions that could be implemented right now are get_and_validate_user_input() and print_pyramid(). While functions might seem to make the program more complex and longer each function would be a simpler implementation and building block of the overall implementation.

Is it possible to solve this problem without using an array? Then what will be the strategy?.

Yes. It would be possible to treat the numbers as a value in a range between 'A' and 'Z'.

One Declaration Per Line

This line in the code could cause problems in maintenance in a larger program

int space,num_of_rows,p=1,t=0;

Break it up into 3 lines and always initialize variables in C.

int space = 0;
int num_of_rows = 0;
int p = 1;
int t = 0;
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.