Concept Behind Pyramid Program in C

Status
Not open for further replies.

Ecko

Wandering In Tecno Land
Hi Everyone
I'm a N00b C programmer
I've just started taking C seriously
Wanted to know what's the logic behind a pyramid program in C
Say a program like
Code:
   *                          1                        1
 ***                       234                     333
*****                   56789                  55555
I don't want coding of these programs but the logic behind how to code these
In simple words how to interpret the loops in different cases:grin::grin:

Suggestion will be highly appreciated
 
Last edited:
K

khattam_

Guest
for these, generally, nested loops are used..

for doing these with nested loops, you should know that the outermost loop will determine the number of lines you will have to go down to complete the pyramid. The inner loop will govern a single line of characters(or integers.. or whatever)....

Now, if the charachers in a single line are changing, then you will need to use the inner variable.. i.e. for the following pyramid:
1
12
123
1234
12345
you will need to print out 1 till n in each line.. where n=line number.. i.e. for the 1st line, you will need to print out 1 to 1 and for the 2nd, you will need to print out 1 to 2 and so on for each line.... how many lines will be decided by the outer loop.. now you will just need to use some (very little) brain....
the code to print this out will look something like:
Code:
for(i=1;i<=5;i++){
	for(j=1;j<=i;j++){
		printf("%d",j);
	}
	printf("\n");
}
if you print out i, rather than j in the above code, you will get
1
22
333
4444
55555


for printing specific characters in such order, it is easier... for eg. you will just need to change the printf() to print *.. then it will give you the output:
*
**
***
****
*****

All you need is some very basic mathematics and logic... Just play around and you will know how easy it is.... Just try printing these patterns:
Code:
54321
5432
543
54
5

Code:
12345
1234
123
12
1

Code:
    1
   12
  123
 1234
12345
 

clmlbx

Technomancer
^^thanx khattam

very usefull

was able to make programmes but was still confused in it (pyramid).........

will you pls answer to this..
*www.thinkdigit.com/forum/showthread.php?p=785505#post785505
 
OP
Ecko

Ecko

Wandering In Tecno Land
*i27.tinypic.com/2wnu7ad.gif
Guyz the loop is like shown above
Not just it contains rows & coloums but also spaces :D
It wasn't properly viewable so I uploaded an image
 
*i27.tinypic.com/2wnu7ad.gif
Guyz the loop is like shown above
Not just it contains rows & coloums but also spaces :D
It wasn't properly viewable so I uploaded an image
the basic concept is still simple.

you can use two things: either spaces, or gotoxy() function.
I use the later in C++.
for spaces, you need 2 counter variables within a for loop, one for calculating the number of spaces before text begins, and another for calculating the number of stars. Both are used to make their respective loops for the output. A third counter is nessasary for controlling the number of lines. As a post loop operation in the main for loop, you increment the space and star counter variables.

and gotoxy is quite simple.
only one for loop is needed.
Just use gotoxy(a,b) and insert (b+2) number of stars.
a is initialised with 0. It is incremented after each loop.
a is the counter for the x coordinate.
b is the counter for y coordinate.
so b starts with the number of lines you want to have in the pyramid.


here, the program builds the pyramid bottom-up. The base first, then the upper layers.

In the spaces loop stars loop methord, the pyramid can be constructed either way.
 
Last edited:

BSOD

Err.. what?
the basic concept is still simple.
you can use two things: either spaces, or gotoxy() function.
I use the later in C++.

Its better if you use a function which is a part of the standard library. gotoxy() is not a part of it.

The basic idea behind the programs is to make you understand loops. Otherwise, there was nothing stopping you from writing a simple cout statement, was there?
There are three things that you need to concentrate on -- spaces, whatever you have to print and the new lines. The rest will fall into place.
 
Its better if you use a function which is a part of the standard library. gotoxy() is not a part of it.

The basic idea behind the programs is to make you understand loops. Otherwise, there was nothing stopping you from writing a simple cout statement, was there?
There are three things that you need to concentrate on -- spaces, whatever you have to print and the new lines. The rest will fall into place.
well, as far as I remember, gotoxy() is present in Borland C++, which I was forced to use at school.
but I suppose its absent in ISO C++, the one used in GCC.
 

ray|raven

Think Zen.
You could write your own snippet, or use one thats written by someone,like this:

Code:
#include <stdio.h>    /* sprintf */
#include <string.h>    /* strcat */

/*
** gotoxy() Implementation in the *nix environment
**
** Note: I've heard the curses library can be useful 
** when trying to implement the handy DOS-only tools of 
** gotoxy() and clrscr() using move() and initscr().
** Though, there is a way to write your own gotoxy() 
** in the Linux environment. This topic isn't discussed 
** often, so I'd like to bring a few new ideas to the table.
**
** Concept: We will use two ANSI C standard functions 
** to accomplish our task. We will use specific 
** string manuvers, according to the man pages, that 
** will allow us to execute each part of the program.
*/
void gotoxy(int x, int y) {
        char essq[100];        /* String variable to hold the escape sequence */
        char xstr[100];        /* Strings to hold the x and y coordinates */
        char ystr[100];        /* Escape sequences must be built with characters */

        /*
        ** Convert the screen coordinates to strings
        */
        sprintf(xstr, "%d", x);
        sprintf(ystr, "%d", y);

        /*
        ** Build the escape sequence (vertical move)
        */
        essq[0] = '\0';
        strcat(essq, "\033[");
        strcat(essq, ystr);

        /*
        ** Described in man terminfo as vpa=\E[%p1%dd
        ** Vertical position absolute
        */
        strcat(essq, "d");

        /*
        ** Horizontal move
        ** Horizontal position absolute
        */
        strcat(essq, "\033[");
        strcat(essq, xstr);
        /* Described in man terminfo as hpa=\E[%p1%dG */
        strcat(essq, "G");

        /*
        ** Execute the escape sequence
        ** This will move the cursor to x, y
        */
        printf("%s", essq);
}

*www.cprogramming.com/snippets/show.php?tip=16&count=30&page=0
 
ha ha thanks raven ! The comments part itself has told me that curses.h is enough for my needs :D. I knew that graphics.h and conio.h can be replaced with curses.h or ncurses.h, but I knew not the commands. So thanks man !

You could write your own snippet, or use one thats written by someone,like this:

Code:
#include <stdio.h>    /* sprintf */
#include <string.h>    /* strcat */

/*
** gotoxy() Implementation in the *nix environment
**
** Note: I've heard the curses library can be useful 
** when trying to implement the handy DOS-only tools of 
** gotoxy() and clrscr() using move() and initscr().
** Though, there is a way to write your own gotoxy() 
** in the Linux environment. This topic isn't discussed 
** often, so I'd like to bring a few new ideas to the table.
**
** Concept: We will use two ANSI C standard functions 
** to accomplish our task. We will use specific 
** string manuvers, according to the man pages, that 
** will allow us to execute each part of the program.
*/
void gotoxy(int x, int y) {
        char essq[100];        /* String variable to hold the escape sequence */
        char xstr[100];        /* Strings to hold the x and y coordinates */
        char ystr[100];        /* Escape sequences must be built with characters */

        /*
        ** Convert the screen coordinates to strings
        */
        sprintf(xstr, "%d", x);
        sprintf(ystr, "%d", y);

        /*
        ** Build the escape sequence (vertical move)
        */
        essq[0] = '\0';
        strcat(essq, "\033[");
        strcat(essq, ystr);

        /*
        ** Described in man terminfo as vpa=\E[%p1%dd
        ** Vertical position absolute
        */
        strcat(essq, "d");

        /*
        ** Horizontal move
        ** Horizontal position absolute
        */
        strcat(essq, "\033[");
        strcat(essq, xstr);
        /* Described in man terminfo as hpa=\E[%p1%dG */
        strcat(essq, "G");

        /*
        ** Execute the escape sequence
        ** This will move the cursor to x, y
        */
        printf("%s", essq);
}
*www.cprogramming.com/snippets/show.php?tip=16&count=30&page=0
btw, I heard there are some rewrites of graphics.h and conio.h using curses and/or ncurses to enable us to use the same functions. Is it true ?
 
Last edited:

ray|raven

Think Zen.
There's libgraph which provides a most of graphics.h functions using a wrapper around SDL , never tried it though.

And for conio.h , curses provides the same functionality , if not more AFAIK.
 

Jove

Right off the assembly line
Hi there....there is no need of gotoxy to print pyramid like programs here...
The output you shown itself tells us what to do.
Print calculated number of spaces,say k, before starting to print * 's
and this k is decremented each time..untill we reach n lines of output.We may not get EXACT output as shown (here).
*
***
*****
******* ///some how similar.

Cheers :)
---spaces here are trimmed by the Post Reply (of this forum)....please note the logic
 
Last edited:
Status
Not open for further replies.
Top Bottom