Click here to Skip to main content
14,768,200 members
Please Sign up or sign in to vote.
2.20/5 (3 votes)
See more:
Hi .. Hope You Guys Are At Good State Of Health . !!
I'm Student Of Computer Sciences[BS(CS)] In current Semester I'm enrolled a course called Introduction To Programming. I'm about average in it ..
But whenever teacher gives a question regarding printing asterisks in different manner by using loops (in the test) I'm puzzled .. I don't know what to do ..
Some reading stuff, articles, suggestions would be appreciated..

Senior Software Engineers Please Suggest Some Thing .. !?
#include "stdafx.h"
#include <iostream>

using namespace std;

int main ()
	int M, row = 0, column = 0, space = 0;
	cout << "Please Enter The Number Of Rows to Print Stars in Descending Order: "; cin >> M;
	while (row < M)
		while (column <= row)
			cout << "*";
			while (space <= M - 1)
				cout << " ";
		cout << '\n';
	return 0;
Updated 11-Jan-13 22:05pm
Sergey Alexandrovich Kryukov 11-Jan-13 17:52pm
As a very first step, stop confusing other. Please. Out of your 3 "answers", only one was the real answer. Please understand that "Add your solution here" input box is reserved for messages posted to provide some kind of help, in response to some member's question in this forum, not for comments or clarifications of your questions. Please also understand that such misplaced posts can hardly help you, but some members will give you down-votes or abuse reports, something you don't want.

You can add comments, use "Improve question", reply to existing comment. Note that members receive notifications on posts related to their posts.

Thank you for understanding,
Sergey Alexandrovich Kryukov 11-Jan-13 17:55pm
As to your assignment, you really need to do it by yourself. However, if you stuck, you can show your work and explain the problem. This way, you can get some help.

So the problem is what kind of loops you can use etc?

If so, for starters, have a look at these:
- Control Structures[^]
- Lesson 3: Loops[^]
Usman Hunjra 11-Jan-13 14:19pm
While OR For L O O P ..
Usman Hunjra 11-Jan-13 14:21pm
Some More Material .. ?!?!? The Links U shared I already visited them ..
Intermediate Level Stuff Required .. !!!
Wendelius 11-Jan-13 14:45pm
It's quite hard to answer without knowing anything more about your needs. Few links you could visit:
- Intermediate C++ [^]
- More C++ Idioms[^]
Abhishek Pant 11-Jan-13 15:11pm
+5 good links
Wendelius 11-Jan-13 15:16pm
Thanks :)
fjdiewornncalwe 11-Jan-13 14:51pm
+5. I'd say it's just what the OP needs.
Wendelius 11-Jan-13 15:03pm
Thanks :)
Usman Hunjra 11-Jan-13 15:41pm
I want to print stars in such a way ..
. . . . *
. . . * *
. . * * *
. * * * *
* * * * *

Here M is the number of lines to print for instance M = 5
I'm gona mad about thinking this .. I'm fully frustrated know ..
Usman Hunjra 11-Jan-13 15:43pm
What if i use three loops i.e row, column and third one is space ..
Wendelius 11-Jan-13 16:08pm
I'd suggest you post the code you have so far.
Sergey Alexandrovich Kryukov 27-Jan-13 22:18pm
Makes sense, a 5.
Don't think in "code" first, try to describe the problem in your own words first.
Imagine, you had to explain the problem to your grand mother. How would you explain?

Maybe, you start at the basics:

  1. I have to write a computer program that prints on a computer display some N rows and columns that show some triangle of the form
  2. the number of rows and columns (N) may vary for different squares (in the example, N is set to 5)
  3. since computer displays are row-wise filled (like writing text on type writer), the program writes the image row by row, starting at the top
  4. I use a dot (.) to move forward one character in the line and an asteriks (*) to draw a fraction of the triangle
  5. the lines are printed as follows:

    1. the first line prints 4 dots and 1 asteriks
    2. the second line prints 3 dots and 2 asteriks
    3. ...
    4. the second last line prints 1 dot and 4 asteriks
    5. the last line prints 0 dots and 5 asteriks
  6. trying to replace the number 5 by N, this can be described as

    1. line 1: N-1 dots, 1 asteriks
    2. line 2: N-2 dots, 2 asteriks
    3. ...
    4. line N-1: 1 dot, N-1 asteriks
    5. line N: 0 dots, N asteriks

Now you are ready to translate into something like code, called pseudo code (since it is not a particular language like C++, you have the freedom to write in your native language):
1. get size of the image which is the number of rows to print and store the value in N
2. print N lines, one line after the other, in the following way (line number 1...N):
   1. print so many dots: N minus linenumber 
      (e.g. line 1: N-1, which results for N = 5: 5-1 = 4)
   2. print so many asteriks: linenumber (e.g. line 1: 1)

From this pseudo code, you now have to find a match to your computer language, e.g. C++.

  • get size of image:
    cout <<"prompt to the user..."; int n; cin >>n;
  • print N lines:
    for(int row = 1; row <= n; ++row) { PrintLineOneBased(n, row); }
    or int row = 1; while(row <= n) { PrintLineOneBased(n, row); row++; }
    or be using the more commonly used zero-based counting
    for(int row = 0; row < n; ++row) { PrintLineZeroBased(n, row); }
    or the equivalent while loop
    int row = 0; while(row < n) { PrintLineZeroBased(n, row); row++; }
  • PrintLineZeroBased(int n, int line):
    PrintSpacesZeroBased(n, line); PrintAsteriksZeroBased(n, line);
  • PrintSpacesZeroBased(int n, int line):
    /* print n-(line+1) dots */
    for(int col = 0; col < n-(line+1); col++) cout <<'.';
  • PrintAsteriksZeroBased(int n, int line):
    /* print (line+1) asteriks */
    for(int col = 0; col < (line+1); col++) cout <<'*';

Admitteldy, the step from one-based to zero based is mean ;-)
You could have defined the OneBased methods likewise and carefully set the loop init and conditions.
Or, since you know that many programming languages are zero based by nature, you coukd have stated the pseudo code already in such a manner, e.g.
1. get size of the image which is the number of rows to print and store the value in N
2. print N lines, one line after the other, in the following way (line number 0...(N-1)):
   1. print so many dots: N minus (linenumber plus 1)
      (e.g. line 0: N-1, which results for N = 5: 5-(0+1) = 4)
   2. print so many asteriks: linenumber plus 1 (e.g. line 0: (0+1))

Key items for designing and implementing:
1) state the problem in your words
2) write pseudo code
3) divide into functions
4) carefully check the loop conditions (init, execute-while condition, increment)


PS: This is a general approach: for tiny problems like this as well as for far more complex ones. This is called: "top-down approach" - you start at the top-most problem and divide it into smaller ones.

PPS: If you have to understand what your initial code does, do a walk-through: list all variables on a piece of paper and go through step-by-step and write manually down what is does (variable values, output). Add comments to the code to tell what the lines do, e.g. /* loop over all lines 0..(N-1) */, etc. You will see that that code is broken and you have to decide if you want to fix the broken code or if you implement your own from scratch. This is BTW a daily situation of every software developer: "fix it or dump it". What drives that decision? Commercial and quality considerations (as well as if I understand the code and the domain well enough to either fix or re-write): can I afford to re-write the code, and can I test the code to give a better confidence level of what I do? You are responsible for the outcome of that work (during the studies as well as professional), so it's also your reputation that depends on that.
Usman Hunjra 12-Jan-13 8:32am
Well Explained ..
I'm exactly looking for this type of stuff.
Sir Thank You So Much for your precious suggestion.
Andreas Gieriet 12-Jan-13 9:03am
You are welcome.
Thank you for accepting my solution!
Good luck!
sja63 14-Jan-13 8:33am
You can also visit the master's homepage:
Sergey Alexandrovich Kryukov 27-Jan-13 22:18pm
Good point, a 5.
Andreas Gieriet 28-Jan-13 3:35am
Thanks for your 5!
I don't want to post a solution, but maybe push you in the right direction.

1. Create a function print_stars() taking a parameter with the max number of asterisks.
2. Think of other lines as from 1 to (max)
3. For each line (n) between 1 and (max).
4. Write spaces (max - n)
5. Write starts (n)

Use a for loop to do the counting.
Typically, for starts at 0 rather than 1, so normally you'd iterate from i = 0, while (i < n).

From that, you should be able to proceed with a good C reference.
Try this out

for(int i=1; i<5; i++)
         for(int a=4; a>i; a--)

         for(int j=1; j<=i; j++)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900