Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie question...

Status
Not open for further replies.

gonzilla

Programmer
Apr 10, 2001
125
US
Hi,

I am have a problem trying to do chapter review question. It's not an assignment, I was just interested in trying it. Here is the question.

Ask user for a character...let's say it's an 'E'. Print this pyramid-type output:

A
ABA
ABCBA
ABCDCBA
ABCDEDCBA

The book suggests using an outer for loop for the rows, and three inner for loops to take care of the spaces, the ascending letters and the descending letters, respectively. I'm stumped. I have tried various increments/decrements and variables only to run into a wall each time.

Can anyone help me out with this? I don't really want the code, just some information that can get me to the correct end result. My idea for the rows is:

char ch;
int row;

for (row = 0; row <= ch - 'A'; row++)

This will give me the rows and I know the starting point for the spaces is (ch - 'A') - 1, although that may not be the best way to get it.

Thanks in advance for your help.

-Tyler




 
I won't b giving a straight logic to u as this way i feel i will be depriving u off learning experience which u'll loath me for, later. So let me give u a logic design based on which u urself can frame an answer -

since its a pyramid which starts and ends at same alpha-
u'll need three loops one to take care of no. of rows (Overall Outer loop) and other two will be in this loop.
First inner loop will print from 'A' to 'A'+no. of times the outermostloop is running. and the next inner loop will start from the counter of first inner loop - 2 (minus 2) and will continue to dec. till it reaches 'A' i.e. 65.

Hope that helps,
Regards.
SwapSawe. s-)
 
I think the outer loop can be,

for( row='A'; row<=ch; row++ )

and to print proper number of spaces,

for( sp=row+1; sp<=ch; sp++ )
putc( ' ', stdout );

and I suggest two while loops to reduce
op codes.

col='A';
while( 1 ) {
putc( col, stdout );
if( col>=row ) {
while( --col>='A' )
putc( col, stdout );
break;
}
col++;
}
putc( '\n', stdout );
Hee S. Chung
heesc@netian.com
 
Hi,

I tried the code in the second e-mail. I honestly worked it out mostly before resorting to it, but I was unable to get it all of the way figured out. It helped to see how it worked when I stepped through it though. Thanks.

Also, it worked fine except for one thing. After the intial A is printed in the first row, each row after that starts with '@' character. To fix this, I just put another col++; before the newline to get the starting point back up to 'A'.

col='A';
while( 1 ) {
putc( col, stdout );
if( col>=row ) {
while( --col>='A' )
putc( col, stdout );
break;
}
col++;
}
col++;
putc( '\n', stdout );

-Tyler
 
My full source code is :

#include <stdio.h>

int main( void )
{
char row, col, sp, ch='E';

for( row='A'; row<=ch; row++ ) {

/* blanks appended first */
for( sp=row+1; sp<=ch; sp++ )
putc( ' ', stdout );

/* write characters */
col='A';
while( 1 ) {
putc( col, stdout );
if( col>=row ) {
while( --col>='A' )
putc( col, stdout );
break;
}
col++;
}
/*-----> you added col++ here <----- */
putc( '\n', stdout );
}
return 0;
}

It works fine for me. I think you sent the line
col='A' to the outside of the outer 'for' loop. Then
col++ you added is needed. But col='A' is placed
the inside of the outer 'for' loop in my source,
so col++ is not needed.

Thanks ! :) Hee S. Chung
heesc@netian.com
 
the best design but not the best performance will be a recursive function:

void print(char a, int c)
{
char x[2] = &quot;&quot;;
x[0] = a;
printf(x);
if(c>0)c--;else return;
print(x[0]+1,c)
printf(x);
}
and you will use it in a print piramidae. John Fill
1c.bmp


ivfmd@mail.md
 
This problem can be implemented using recursion
but I don't think recursion is the best design
for this problem. Your routine prints
just one row of a pyramid. My routine to do
the same thing is :

col='A';
while( 1 ) {
putc( col, stdout );
if( col>=row ) {
while( --col>='A' )
putc( col, stdout );
break;
}
col++;
}

I don't think recursive routine is more
simpler than non-recursive routine for
this case. Moreover, recursion routine
requires more stacks.

Hee S. Chung
heesc@netian.com
 
Ok, I made little changes do display all piramide:
#include<stdio.h>
void print(char a, int c, int lines)
{
char x[2] = &quot;&quot;;
int i;
x[0] = a;
if(lines)for(i = 0;i<c;i++)
{
print(x[0], i, 0);
printf(&quot;\n\r&quot;);
}
printf(x);
if(c>0)c--;else return;
print(x[0]+1, c, 0);
printf(x);
}
int main(int argc, char* argv[])
{
print('A', 2, 1);
return 0;
}
John Fill
1c.bmp


ivfmd@mail.md
 
So do you still think recursion is the best design
for this problem?

Recursion is only useful when an algorithm can be
reduced to a simple routine. And processing data
size must not be so large to avoid stack overflow.
As you know, consideration for recursion depth is
needed because stack usage is closely related to it.

For this problem, data size is not so large, but
algorithm is not simpler than two iteration loops.
Hee S. Chung
heesc@netian.com
 
As a design, my algorithm is much simplier. Overall there is much simpier to add changes and to increase the functional complexity without increasing too much the algorithm complexity. As you see, even the problem is simple, it looks recursive, even you can resolve it simple (in this simple case) without reursivity. As I sayd, is not the best performance. When you will try do make a little more complicate problems, the recursivity will simplifier much the problem. I put this algorythm to make all of you &quot;thinking recursive&quot; for a little. Is a strategy problem. But the right answer deppends on situation. For example in this situation, the two loops are recommended. John Fill
1c.bmp


ivfmd@mail.md
 
Thanks for all of the info. BTW - I did miss the fact that the col='A'; was set inside the loop. A newbie mistake. I just declared it and intitialized it with everything else...my bad. Hey John, can this recursive way be implemented interactively? I mean can I still ask the user to enter the letter and pass that character to the print() function you have? It looks like you are passing it constants:

print('A', 2, 1);

Thinking recursively will help me to understand how that all works, so thanks for the input. I think I need some practice visualizing these algorithms, do you have any book or web site suggestions? I definitely want to learn more than my 'C' class will be teaching me.

Thanks.

-Tyler
 
Yes, all implementations depend on situation.
And I have quite different thoughts about more
complicate problems. If a problem is complicated,
potential problem can be arised for recursive
routines because their reliability depends on
the stack size - We must prevent stack overflow.

When a recursive routine is converted to
non-recursive routine, its reliability depends on
the available memory and it is preferable for
stability of a program.

Evidently recursion is very useful to describe
the most simplest algorithm for some specific
cases - I agree with you on this.
Hee S. Chung
heesc@netian.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top