Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C++
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;
Posted 11-Jan-13 9:09am
Edited 11-Jan-13 22:05pm
Sergey Alexandrovich Kryukov at 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 at 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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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[^]
Scenic Design at 11-Jan-13 14:19pm
While OR For L O O P ..
Scenic Design at 11-Jan-13 14:21pm
Some More Material .. ?!?!? The Links U shared I already visited them ..
Intermediate Level Stuff Required .. !!!
Mika Wendelius at 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 at 11-Jan-13 15:11pm
+5 good links
Mika Wendelius at 11-Jan-13 15:16pm
Thanks :)
Marcus Kramer at 11-Jan-13 14:51pm
+5. I'd say it's just what the OP needs.
Mika Wendelius at 11-Jan-13 15:03pm
Thanks :)
Scenic Design at 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 ..
Scenic Design at 11-Jan-13 15:43pm
What if i use three loops i.e row, column and third one is space ..
Mika Wendelius at 11-Jan-13 16:08pm
I'd suggest you post the code you have so far.
Sergey Alexandrovich Kryukov at 27-Jan-13 22:18pm
Makes sense, a 5.
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

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 Wink | ;-)
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.
Scenic Design at 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 at 12-Jan-13 9:03am
You are welcome.
Thank you for accepting my solution!
Good luck!
Member 9586495 at 14-Jan-13 8:33am
You can also visit the master's homepage:
Sergey Alexandrovich Kryukov at 27-Jan-13 22:18pm
Good point, a 5.
Andreas Gieriet at 28-Jan-13 3:35am
Thanks for your 5!
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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)

  Print Answers RSS
0 Mathew Soji 334
1 Maciej Los 295
2 BillWoodruff 290
3 Afzaal Ahmad Zeeshan 257
4 DamithSL 225
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,045

Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 12 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100