Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The output is not like that as I expected ?!
Suggestion Please .!!!
C++
#include "stdafx.h"
#include <iostream>

using namespace std;

//	[P R O B L E M]
//	Print Astrisks in descending order
//      Let n as the number of lines to print "*" starting from nth position of          column. For instance n = 5 that means we've to print 5 rows of asterisks & first star will be printed on 5th position of column.

int main ()
{
	 int n;
	 cout << "Enter Value: ";
	 cin >> n;
	 int i = 0, j = n;	//	Let i = row & j = colum
	 if (j >= 0)
	 {
		while (i < n)
	 {
		 while (j <= n)
		 {
			 cout << "*";
			 
			 j--;
		 }		 
		 cout << '\n';
		 i++;
	 }
	 
	 }
	 return 0;
}
Posted
Updated 2-Dec-20 9:06am
v2
Comments
Mohibur Rashid 4-Jan-13 11:24am    
we dont know your output
Andreas Gieriet 4-Jan-13 19:28pm    
Use proper variable names: why commenting that i = row and j = col? Name the variable as such!
The output is not clear neiter: is it (. stands for a space)
....*
...*.
..*..
.*...
*....
See also my solution#4.

Cheers
Andi

Did you run it and DEBUGGED it? Just do it on pen & paper to see the issues.

Here is what is expected of enquirers:
1. TRY first what you want to do! You may find that it's not that hard.
2. Formulate what was done by you that looks like an issue/not working.

Try them and tell if you face issues.
Members will be more than happy to help like this.


To start with:
value of 'j' & 'n' are same when the print starts... leading to single star on first line (not needed as descending design was needed.)
 
Share this answer
 
Comments
Usman Hunjra 4-Jan-13 11:31am    
The Issue Is: Loop is running infinitely and keep on printing asterisks ..?!
Sandeep Mewara 4-Jan-13 11:39am    
while (j <= n)
{
cout << "*";

j--;
}
Usman Hunjra 4-Jan-13 11:39am    
yes there should be something else in 2nd while loop . . . .
I'm gOna Mad . . .
Andreas Gieriet 4-Jan-13 19:23pm    
Your inner while loop has a wrong condition.
You decrement j in the loop but how long? It should decrement from n down to 0, right?
You should set j before each inner loop execution to n and loop in the inner loop while j is larger than zero.
Cheers
Andi
Sandeep Mewara 4-Jan-13 22:21pm    
It's a homework question where the students are asked to fix the issues in the code snippet. Hence I just gave hints to the OP.

We don't encourage homework questions much where OP has not shared his efforts made and is seeking for direct solution.
The 'if' statement should check for the value of n (ie. not j) and then you need to move the definition j and set it to n inside the outer while loop.
 
Share this answer
 
v2
Assuming you need produce this pattern (a dot stands for a space)
....*
...*.
..*..
.*...
*....

the following code would do:
C++
int n = 5;
for (int row = 0; row < n; ++row)
{
   for (int col = 0; col < n; ++col)
   {
      cout << ((n-col == row+1) ? '*' : '.');
   }
   cout << endl;
}


For
*....
.*...
..*..
...*.
....*

use
C++
int n = 5;
for (int row = 0; row < n; ++row)
{
   for (int col = 0; col < n; ++col)
   {
      cout << ((col == row) ? '*' : '.');
   }
   cout << endl;
}


For
....*
....*
....*
....*
....*

use
C++
int n = 5;
for (int row = 0; row < n; ++row)
{
   for (int col = 0; col < n; ++col)
   {
      cout << ((col == n-1) ? '*' : '.');
   }
   cout << endl;
}

etc.

Cheers
Andi
 
Share this answer
 
Comments
Usman Hunjra 5-Jan-13 4:25am    
thank u .. i'll do it in my way ..
Andreas Gieriet 5-Jan-13 5:25am    
You are welcome.
Good luck!
Andi
#include <iostream>
using namespace std;
int main ()
{
int i=14,j=14;
while(i>0){
j=i;
while(j>0){
cout<<"*";
j--;
}
cout<
 
Share this answer
 
v3
Comments
Richard Deeming 3-Dec-20 5:42am    
An unformatted, unexplained code-dump is not a "solution" to this already-solved question.

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