Click here to Skip to main content
12,071,437 members (35,385 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C++
The output is not like that as I expected ?!
Suggestion Please .!!!
#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 4-Jan-13 6:17am
ctype.h1.8K
Edited 4-Jan-13 9:15am
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.)
  Permalink  
Comments
Scenic Design 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--;
}
Scenic Design 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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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

Solution 4

Assuming you need produce this pattern (a dot stands for a space)
....*
...*.
..*..
.*...
*....
the following code would do:
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
int n = 5;
for (int row = 0; row < n; ++row)
{
   for (int col = 0; col < n; ++col)
   {
      cout << ((col == row) ? '*' : '.');
   }
   cout << endl;
}

For
....*
....*
....*
....*
....*
use
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
  Permalink  
Comments
Scenic Design 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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web03 | 2.8.160208.1 | Last Updated 4 Jan 2013
Copyright © CodeProject, 1999-2016
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