Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Homework
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.7K
Edited 4-Jan-13 9:15am
v2
Comments
Mohibur Rashid at 4-Jan-13 11:24am
   
we dont know your output
Andreas Gieriet at 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 at 4-Jan-13 11:31am
   
The Issue Is: Loop is running infinitely and keep on printing asterisks ..?!
Sandeep Mewara at 4-Jan-13 11:39am
   
while (j <= n)
{
cout << "*";

j--;
}
Scenic Design at 4-Jan-13 11:39am
   
yes there should be something else in 2nd while loop . . . .
I'm gOna Mad . . .
Andreas Gieriet at 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 at 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 at 5-Jan-13 4:25am
   
thank u .. i'll do it in my way ..
Andreas Gieriet at 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
0 OriginalGriff 381
1 Sergey Alexandrovich Kryukov 245
2 Marcin Kozub 225
3 Praneet Nadkar 217
4 /\jmot 189
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,614
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 4 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