Re: Printing Patterns using for loops




priyam.trivedi@xxxxxxxxx wrote:
I saw this in one article but I cannot understand why the coder has
used z--. I did it without z-- and it still works the same.

There's no `z--` in the code you posted, but I think I still get what
you mean (Temp--).

<snip... the pattern>

void PrintPatternThree (int PatternSize)
{
int Row = 0;
int Col = 0;
int Temp = 0; // used to hold the new value of PatternSize for
evaluating

These initialisations are superflous and hence confusing, as you
immediatelly re-initialise these variables with different values.

Temp = PatternSize;

This actually does not get used at all (apart from decrementing it).

for (Row = 1; Row <= PatternSize; Row++)
{
for (Col = 1; Col <=PatternSize; Col++)
{
if (Row <= Col)
printf ( "$" );
else
printf ( "%d", PatternSize);
} // end Col
Temp--;
printf ( "\n" );
} // end Row

Don't use `//` for comments, at least in newsgroups, as it may foul the
quoting.

return;

} /* end PrintPatternFour()

$$$$$
$$$$5
$$$55
$$555
$5555 */

I guess `Temp` is a leftover from a different way of implementing this.
E.g. `Temp` could be used instead of `Row` in a `while` loop.

int Temp = PatternSize;

while (Temp)
{
/* do stuff with Col; use Temp instead of Row (not a straight
replacement!) */;
--Temp;
}

OTH, you could also use PatternSize. C passes arguments by value, so
you wouldn't mess whatever is passed in the function call.

--
BR, Vladimir

.



Relevant Pages

  • Re: strange output
    ... int i, j, temp; ... swap(i, j); ... printf("%d %d %d", i, j, temp); ...
    (comp.lang.c)
  • Printing Patterns using for loops
    ... void PrintPatternThree (int PatternSize) ... printf; ...
    (comp.lang.c)
  • strange output
    ... int i, j, temp; ... swap(i, j); ... printf("%d %d %d", i, j, temp); ...
    (comp.lang.c)
  • Re: strange output
    ... int i, j, temp; ... printf("%d %d %d", i, j, temp); ...
    (comp.lang.c)
  • Pointer doubt
    ... p = (int*) malloc ); ... *p = temp; ... printf; ...
    (comp.lang.c)