473,385 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Printing Patterns using for loops

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.

Please help.

Priyam
Trying to print the following pattern but...

$$$$$
$$$$5
$$$55
$$555
$5555

The Code
----------

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

Temp = PatternSize;

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

return;

} /* end PrintPatternFour()

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

Feb 28 '06 #1
1 7635

pr************@gmail.com 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

Feb 28 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Will Stuyvesant | last post by:
If you know that your source code is going to be used later by others, then I feel that code with the pattern: if some_condition: some_name = some_value else: some_name = other_value is...
5
by: Mr. B | last post by:
This is driving me NUTZ!!! I've been screwing around on this for a week now. And I have tried to find examples similar to what I have (nada). Got lots of streaming a TXT file... bah! I am...
6
by: sathyashrayan | last post by:
Dear group, Following is a exercise from a book called "Oreilly's practical C programming". I just wanted to do a couple of C programming exercise. I do have K and R book, but let me try some...
6
by: fool | last post by:
Dear group, Given a string I have to print the permutation, using some looping tricks. This is not a Home work problem. My best try as beginner is: #include<stdio.h> #include<stdlib.h> ...
6
by: Jerry Hill | last post by:
Hello all, I have a piece of code I could use some help optimizing. What I'm attempting to do is periodically grab a screenshot, and search for 2D patterns of black pixels in it. I don't care...
2
by: ed311 | last post by:
Write a program that uses for loops to prints the following patterns separately, one below the other. All asterisks (*) should be printed by a single statement of the form cout <<”*”; (b) Combine...
7
by: =?Utf-8?B?bWF2cmlja18xMDE=?= | last post by:
Hi, I would like to know more about design patterns and specifically using C#. Can any one recommend a good book? Thanks
7
by: harishram007 | last post by:
please can u help me out in the following patterns A ABA ABCBA ABCDCBA ABCDEDCBA ABCDEFEDCBA PATTERN 2: 4 4 4 4
10
by: vital | last post by:
Hi, I am designing the middle tier of a project. It has 6 classes and microsoft application data access block. The six classes are DBServices, Logger, ProjectServices ... etc. and all these...
0
it0ny
by: it0ny | last post by:
Hi guys, thanks I am fairly new to this forum so I hope I chose the right place to post this question. I try to make my program printout a deposit's report. I created a class to store the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.