Program in my book..

hi there i m new to this forum i dont know if ths is the right place to post a question so sorry if i posted in the wrong forum..
i have a book c primer plus 5 th edition i cannot write a program the question is ;
Question:Have a program request the user to enter an uppercase letter. Use nested loops
to produce a pyramid pattern like this:
                                         A                                         
                                       ABA
                                      ABCBA
                                    ABCDCDA
                                   ABCDEDCBA
The pattern should extend to the character entered. For example, the preceding
pattern would result from an input value of E. Hint: Use an outer loop to handle
the rows. Use three inner loops in a row, one to handle the spaces, one for
printing letters in ascending order, and one for printing letters in descending
order.


i do not understand the hint given in this question if anybody can write this program it would be really helpful thank you ...

Comments

  • The first A should be centered at the top of the pyramid of letters. Obviously there is a bug in the forum software! :icon_eek:

    These patterns are generally done with nested loops, as the book explained:

    1) for the row logic
    2) a second for loop, for the printing of each letter in that row
    3) a third could be used for the "camel back" logic, but it can be done using variables, without a third loop, as well.

    Programming is about problem solving, once you get past the issues of the language you choose to solve it with. The idea here is to sharpen the readers problem solving skills.

    So - don't ask for someone to write the program for you. Instead, work it through, by hand, without the computer, a few times. Do it very slowly, and think about what the logic is that you are using. When you understand the logic you use, at a low enough level, you know the logic that can form the core of your program's algorithm.

    You can also experiment programming a simpler versions of the above. Remember that all the printing must be done top left, to bottom right. No going back. ;)

    Accept that it will take some time before the solution comes to light for you, but practice makes that time considerably shorter, in the future.

    Still frustrated? Post your attempt and it's output, and we'll smooth it out for you.

    And welcome to the forum, psychic! :cool:
  • Just finished this little program, and imo, this is a tougher diagram to print out than I expected.

    So, some assistance from the "gallery" seems warranted. ;)

    After taking in the letter from the user, change the letter to a number, (although letters are numbers + an offset of 65, making the change, stops all future messing around with the char (letters).

    Printing a letter is just the same as printing a number, just change the %d in printf(), to a %c
    #include <stdio.h>
    
    int main(void) {
       char ch='k';
       int n=ch;
    
       printf("ch=%c or %d,    n=%c or %d\n",ch,ch',n,n');
       return 0;
    }
    
    An easy way to convert letters into numbers, where a=0, b=1, etc., is simply to subtract 'a' (or 'A' for uppercase letters), from the letter.

    So I used variables of:
     int i,j,spaces,length,r,c,stop;
     char ch;
    //And after:
    
       printf("Enter the stop letter for the pyramid of letters\n");
       scanf("%c",&ch);
      
    //Made the change over:
     stop=ch-'a'; printf("%d\n\n",stop);
       
       length=1;
       spaces=stop;
    //the outer loop - row logic
       for(r=0;r<=stop;r++) {
          i=stop;
          
          j=spaces;
         //print out the leading (left side) spaces for each row
          while(j>0) { 
             --j;
             putchar(' ');
          }
          
          for(c=0;c<work it out;c++) {
             if(c<= some value you must find out which)
                   printf("%c",c+'a');
                else {
                   --i;
                   printf("%c",i+'a');
                }
          }
          putchar('\n');
          length+=2; //each row of letters is 2 letters longer 
          spaces--;  //than the previous row
          
       }
       printf("\n");
       return 0;
    }
    

    Lots of help, but work still needs to be done.
  • i was at this program for the last 2 hrs and made it yayayaya
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
    	
    	int a,b,i,c,d;
    	printf("Input the number of rows in pyramid\n");
    	scanf("%d",&i);
    	for (a=1;a<=i;a++)
    	{
    		for (b=i-a;b>=0;b--)
    		printf(" ");
    		for (c=1;c<=a;c++)
    		printf("%d",c);
    		for(d=a-1;d>0;d--)
    		printf("%d",d);
    		printf("\n");
    		
    	}
    	getch();
    	return 0;
    }
    


    try it out and also thanks for the reply ..... :-)
  • One more thing for future purposes should i post a question in new thread or in this thread????
  • psychic wrote:
    i was at this program for the last 2 hrs and made it yayayaya

    #include<stdio.h>
    #include<conio.h>
    int main()
    {

    int a,b,i,c,d;
    printf("Input the number of rows in pyramid\n");
    scanf("%d",&i);
    for (a=1;a<=i;a++)
    {
    for (b=i-a;b>=0;b--)
    printf(" ");
    for (c=1;c<=a;c++)
    printf("%d",c);
    for(d=a-1;d>0;d--)
    printf("%d",d);
    printf("\n");

    }
    getch();
    return 0;
    }



    try it out and also thanks for the reply ..... :-)

    this is a number type i will be working on the char type also will post the ans soon:D
  • psychic wrote:
    One more thing for future purposes should i post a question in new thread or in this thread????

    If it's related to this thread then post it here, otherwise if it's a completely different topic/question then please start a new thread.
  • psychic wrote:
    this is a number type i will be working on the char type also will post the ans soon:D
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
    	int a,x,b,c,d,e=65;
    	printf("enter the no of the rows of pyramid\n");
    	scanf("%d",&a);
    	e+=a;
    	for (x=65;x<=e;x++)
    	{
    	for (b=e;b>=x;b--)
    	printf(" ");
    	for (c=65;c<=x;c++)
    	printf("%c",c);
    	for(d=x-1;d>64;d--)
    	printf("%c",d);
    	printf("\n");
    	}
    	getch();
    	return 0;
    }
    
    i was traveling so i could not post it here it is the alphabetic pyramid hope it helps who searches a program like this .....
  • Great to see your further work on this. ;)

    Some recommendations for you, on the important subject of style in coding.

    1) As you become more experienced, your brain will become trained to identify quickly, the more common idioms of the C language. It becomes important to assist in that effort, by using consistent indentation in your code.

    Every subservient line of code should be indented two to five spaces from the dominant line of code that controls it:

    You don't want this:
    	for (x=65;x<=e;x++)
    	{
            for (b=e;b>=x;b--)
    	printf(" ");
    	for (c=65;c<=x;c++)
    	printf("%c",c);
    	for(d=x-1;d>64;d--)
    	printf("%c",d);
    	printf("\n");
    	}
    

    You want this:
       for (x=65;x<=e;x++)
       {
          for (b=e;b>=x;b--)
             printf(" ");
          for (c=65;c<=x;c++)
             printf("%c",c);
          for(d=x-1;d>64;d--)
             printf("%c",d);
          printf("\n");
       }
    
    Note how the subservient printf() calls that are part of each for loop, now *LEAP* out to your eye - and how the one printf() call that is NOT dependent on a for loop controlling it, is also VERY easy to spot.

    Also, the indentation is a more moderate 3 spaces, which saves a lot of horizontal line length. In code that is heavily nested, you will tend to run out of line length and wind up with really hard code to read, if you use 8 spaces (you can set your tabs to be 3 spaces in width, and to actually insert spaces instad of a tab whenever you press the tab key, inside your IDE). I strongly suggest you make that change in your IDE. Even if it fits inside your line length in your IDE, if you ever post the code on programming forums, it will look ugly - forum line lengths are smaller than 80 char's for code, in general.

    If you are using Windows, there are MUCH better compilers than Borland, these days. I loved Borland's IDE, for DOS, but for Windows - it's much better to get a modern compiler and IDE. You'll be so glad you made that change, after a brief learning curve. Visual Express, Pelles C (my current favorite for C only), Code Blocks + compiler, etc., all are free, and offer so much more than Turbo C.

    Pelles C also has it's own forum to answer your Pelles questions - very helpful indeed! There is a learning curve, but it's totally worth it. :cool:
  • Adak wrote:
    Great to see your further work on this. ;)

    Some recommendations for you, on the important subject of style in coding.

    1) As you become more experienced, your brain will become trained to identify quickly, the more common idioms of the C language. It becomes important to assist in that effort, by using consistent indentation in your code.

    Every subservient line of code should be indented two to five spaces from the dominant line of code that controls it:

    You don't want this:
    	for (x=65;x<=e;x++)
    	{
            for (b=e;b>=x;b--)
    	printf(" ");
    	for (c=65;c<=x;c++)
    	printf("%c",c);
    	for(d=x-1;d>64;d--)
    	printf("%c",d);
    	printf("\n");
    	}
    

    You want this:
       for (x=65;x<=e;x++)
       {
          for (b=e;b>=x;b--)
             printf(" ");
          for (c=65;c<=x;c++)
             printf("%c",c);
          for(d=x-1;d>64;d--)
             printf("%c",d);
          printf("\n");
       }
    
    Note how the subservient printf() calls that are part of each for loop, now *LEAP* out to your eye - and how the one printf() call that is NOT dependent on a for loop controlling it, is also VERY easy to spot.

    Also, the indentation is a more moderate 3 spaces, which saves a lot of horizontal line length. In code that is heavily nested, you will tend to run out of line length and wind up with really hard code to read, if you use 8 spaces (you can set your tabs to be 3 spaces in width, and to actually insert spaces instad of a tab whenever you press the tab key, inside your IDE). I strongly suggest you make that change in your IDE. Even if it fits inside your line length in your IDE, if you ever post the code on programming forums, it will look ugly - forum line lengths are smaller than 80 char's for code, in general.

    If you are using Windows, there are MUCH better compilers than Borland, these days. I loved Borland's IDE, for DOS, but for Windows - it's much better to get a modern compiler and IDE. You'll be so glad you made that change, after a brief learning curve. Visual Express, Pelles C (my current favorite for C only), Code Blocks + compiler, etc., all are free, and offer so much more than Turbo C.

    Pelles C also has it's own forum to answer your Pelles questions - very helpful indeed! There is a learning curve, but it's totally worth it. :cool:
    Will keep the spaces in mind next tym ...... for the compiler i use turbo c pp
    Because it is used in my college i also use other compilers like c free5 and borland also
    I will try out the compilers u listed thanks for the advice .....
    :-)
  • It's really unfortunate many colleges still teach with 30-year-old compilers that have been long obsolete. Does anyone in the world (except maybe hobbyist) still use MS-DOS operating system??
  • I could understand the use of Turbo C in a developing country's high school. But for a college??

    That is a very poor preparation on the part of the college, for these students.
  • Its more than unfortunate, its absurd.
Sign In or Register to comment.