 |
24-03-2008, 10:23 PM
|
#1 (permalink)
|
|
Wandering In Tecno Land
Join Date: Feb 2005
Location: 127.0.0.1
Posts: 724
|
Concept Behind Pyramid Program in C
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 
Suggestion will be highly appreciated
__________________
Born in Windows Die In Linux © 2009-10 All Rights Reserved.
Learn Linux : www.linoob.com (Official WebSite)
Last edited by Ecko; 25-03-2008 at 02:17 PM.
Reason: code added
|
|
|
|
Advertisements. Register and be a member of the community to get rid of them.
|
|
Advertisement
|
|
24-03-2008, 11:15 PM
|
#2 (permalink)
|
|
Fresh Stock Since 2005
Join Date: Feb 2005
Posts: 1,015
|
Re: Concept Behind Pyramid Program in C
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
__________________
http://www.khattam.info
|
|
|
24-03-2008, 11:23 PM
|
#3 (permalink)
|
|
Wise Old Owl
Join Date: Aug 2006
Location: Indore
Posts: 1,687
|
Re: Concept Behind Pyramid Program in C
^^thanx khattam
very usefull
was able to make programmes but was still confused in it (pyramid).........
will you pls answer to this..
http://www.thinkdigit.com/forum/show...505#post785505
__________________
| Athlon II X4 635 @ 2.9Ghz | | Gigabyte GA-MA785GMT-US2H | | Kingston 2x2 Gb 1333Mhz DDR3 | | WDC 500Gb Green | | Palit GTS 250 512mb | | Tagan 500W | | Samsung B2030 | | Lg DVD Writer |
|
|
|
25-03-2008, 02:10 PM
|
#4 (permalink)
|
|
Wandering In Tecno Land
Join Date: Feb 2005
Location: 127.0.0.1
Posts: 724
|
Re: Concept Behind Pyramid Program in C

Guyz the loop is like shown above
Not just it contains rows & coloums but also spaces 
It wasn't properly viewable so I uploaded an image
__________________
Born in Windows Die In Linux © 2009-10 All Rights Reserved.
Learn Linux : www.linoob.com (Official WebSite)
|
|
|
25-03-2008, 02:30 PM
|
#5 (permalink)
|
|
The Smaller Bang
Join Date: Sep 2007
Location: Gautham City
Posts: 7,489
|
Re: Concept Behind Pyramid Program in C
Quote:
Originally Posted by Ecko

Guyz the loop is like shown above
Not just it contains rows & coloums but also spaces 
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.
__________________
http://TheSmallerBang.wordpress.com
eMachines E725 - T4400 2.2GHz, 1GB, 160GB
Nokia 5130XM * T-Sonic 610 2GB
Nokia 2323C * Samsung Galaxy Y
Apple iPad 2 16GB WiFi
Last edited by MetalheadGautham; 25-03-2008 at 02:30 PM.
Reason: Automerged Doublepost
|
|
|
08-04-2008, 02:09 PM
|
#6 (permalink)
|
|
Err.. what?
Join Date: Apr 2008
Posts: 65
|
Re: Concept Behind Pyramid Program in C
Quote:
Originally Posted by MetalheadGautham
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.
|
|
|
08-04-2008, 03:13 PM
|
#7 (permalink)
|
|
The Smaller Bang
Join Date: Sep 2007
Location: Gautham City
Posts: 7,489
|
Re: Concept Behind Pyramid Program in C
Quote:
Originally Posted by BSOD
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.
__________________
http://TheSmallerBang.wordpress.com
eMachines E725 - T4400 2.2GHz, 1GB, 160GB
Nokia 5130XM * T-Sonic 610 2GB
Nokia 2323C * Samsung Galaxy Y
Apple iPad 2 16GB WiFi
|
|
|
08-04-2008, 03:22 PM
|
#8 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: Concept Behind Pyramid Program in C
That's what he meant - Its NOT present in standard libraries. Why reiterate that point?
__________________
Harsh J
www.harshj.com
|
|
|
08-04-2008, 03:39 PM
|
#9 (permalink)
|
|
The Smaller Bang
Join Date: Sep 2007
Location: Gautham City
Posts: 7,489
|
Re: Concept Behind Pyramid Program in C
Quote:
Originally Posted by QwertyManiac
That's what he meant - Its NOT present in standard libraries. Why reiterate that point?
|
I wasn't sure if it really was present in standard libraries, as I am out of touch with them.
Related: is there any similar function in GCC-Linux ?
__________________
http://TheSmallerBang.wordpress.com
eMachines E725 - T4400 2.2GHz, 1GB, 160GB
Nokia 5130XM * T-Sonic 610 2GB
Nokia 2323C * Samsung Galaxy Y
Apple iPad 2 16GB WiFi
|
|
|
08-04-2008, 03:42 PM
|
#10 (permalink)
|
|
Think Zen.
Join Date: Dec 2005
Posts: 1,498
|
Re: Concept Behind Pyramid Program in C
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);
}
http://www.cprogramming.com/snippets...ount=30&page=0
__________________
Do what you will; but not because you must. -- Zen Quote
|
|
|
08-04-2008, 03:58 PM
|
#11 (permalink)
|
|
The Smaller Bang
Join Date: Sep 2007
Location: Gautham City
Posts: 7,489
|
Re: Concept Behind Pyramid Program in C
ha ha thanks raven ! The comments part itself has told me that curses.h is enough for my needs  . 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 !
Quote:
Originally Posted by rayraven
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);
}
http://www.cprogramming.com/snippets...ount=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 ?
__________________
http://TheSmallerBang.wordpress.com
eMachines E725 - T4400 2.2GHz, 1GB, 160GB
Nokia 5130XM * T-Sonic 610 2GB
Nokia 2323C * Samsung Galaxy Y
Apple iPad 2 16GB WiFi
Last edited by MetalheadGautham; 08-04-2008 at 03:58 PM.
Reason: Automerged Doublepost
|
|
|
08-04-2008, 04:03 PM
|
#12 (permalink)
|
|
Think Zen.
Join Date: Dec 2005
Posts: 1,498
|
Re: Concept Behind Pyramid Program in C
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.
__________________
Do what you will; but not because you must. -- Zen Quote
|
|
|
21-04-2008, 12:40 PM
|
#13 (permalink)
|
|
Right Off the Assembly Line
Join Date: Jan 2008
Posts: 7
|
Re: Concept Behind Pyramid Program in C
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 by Jove; 21-04-2008 at 12:41 PM.
Reason: Trimme spaces
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|