Monday 24 June 2013

Pyramid Spaces. Let Us C Chapter 3, exercise E-e


Let Us C is anything but short on exercises.
Ch-3 Exercise E-E
Write a program to produce the following output
A B C D E F G F E D C B A
A B C D E F     F E D C B A
A B C D E           E D C B A
A B C D                D C B A
A B C                      C B A
A B                            B A
A                                  A

I tried writing this program but it was too hard for me. I cannot think in nested loops. I googled it and found a very short program that achieves the same purpose with a very short program HERE. If you decide to check out the link, scroll down to answer 1 which is the one I am talking about. Just remove the fourth line from that code clrscr(); which clears the screen in Turbo C.
But we are using Code::Blocks, gnu gcc default, so just remove it or replace it with system("cls") (in which case you have to #include <stdlib.h> into your code - but only if you use system("cls")); it executes the DOS 'cls' (clear screen) command.
Also the code in the link is missing #include <stdio.h> and #include <conio.h>. 
I tried figuring out the logic in the code, but got nowhere. So I wrote it from scratch as best as I could. It's much longer, but it does what it should. By the way, I was saving my code with the .cpp extension for quite a while. Now since I save them with a .c extension, it returns values (2 in case of this code) and C::B tells me
Process terminated with status 2 (0 minutes, 2 seconds)
So I am trying to get into the habit of saying return 0; at the end of my code.
Here is a sample output:


Here is the actual code:



#include <stdio.h>

main()
{
    int a,b,c,d,e,f,g,line=1,alp=65;

    for(a=0;a<7;a++)
    {
        printf("%c ",alp);
        alp=alp+1;
        for(b=0;b<1;b++)
        {
            if(line>6)
            {
                printf("  ");
                alp=alp+1;
            }
            else
            {
                printf("%c ",alp);
                alp=alp+1;
            }
            for(c=0;c<1;c++)
            {
                if(line>5)
                {
                    printf("  ");
                    alp=alp+1;
                }
                else
                {
                    printf("%c ",alp);
                    alp=alp+1;
                }
                for(d=0;d<1;d++)
                {
                    if(line>4)
                    {
                        printf("  ");
                        alp=alp+1;
                    }
                    else
                    {
                        printf("%c ",alp);
                        alp=alp+1;
                    }
                    for(e=0;e<1;e++)
                    {
                        if(line>3)
                        {
                            printf("  ");
                            alp=alp+1;
                        }
                        else
                        {
                            printf("%c ",alp);
                            alp=alp+1;
                        }
                        for(f=0;f<1;f++)
                        {
                            if(line>2)
                                {
                                    printf("  ");
                                    alp=alp+1;
                                }
                                else
                                {
                                    printf("%c ",alp);
                                    alp=alp+1;
                                }
                            for(g=0;g<1;g++)
                            {
                                if(line>1)
                                {
                                    printf("  ");
                                    alp=alp-1;
                                }
                                else
                                {
                                    printf("%c ",alp);
                                    alp=alp-1;
                                }
                            }
                            if(line>2)
                                {
                                    printf("  ");
                                    alp=alp-1;
                                }
                                else
                                {
                                    printf("%c ",alp);
                                    alp=alp-1;
                                }
                        }
                        if(line>3)
                        {
                            printf("  ");
                            alp=alp-1;
                        }
                        else
                        {
                            printf("%c ",alp);
                            alp=alp-1;
                        }
                    }
                    if(line>4)
                        {
                            printf("  ");
                            alp=alp-1;
                        }
                        else
                        {
                            printf("%c ",alp);
                            alp=alp-1;
                        }
                }
                if(line>5)
                        {
                            printf("  ");
                            alp=alp-1;
                        }
                        else
                        {
                            printf("%c ",alp);
                            alp=alp-1;
                        }
            }
            if(line>6)
                        {
                            printf("  ");
                            alp=alp-1;
                        }
                        else
                        {
                            printf("%c ",alp);
                            alp=alp-1;
                        }
        }
        printf("%c\n",alp);
        alp=65;
        line++;
    }
    return 0;
}


No comments:

Post a Comment