Hey guys!
I have an assignment for Tuesday.
The problem is:
Write a program, getting n (odd, n>=1 && n<=15) from the user and prints this shape:
For n=3

PLZ Click here to see the image .

I wrote this code, but I don't know what to do with line #13.
I appreciate your help!

#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int i,j,n;
cout << "Enter n: ";
cin >> n;
for (i=1; i<=(2*n-1) ; i++)
{
	for (j=1; j<=(n-i); j++)
		cout << " ";
	for (j=1; j<=(2*i-1); j++)
		cout << "*";
		cout << "\n";
}

getch();
}

Recommended Answers

All 4 Replies

Hey guys!
I have an assignment for Tuesday.
The problem is:
Write a program, getting n (odd, n>=1 && n<=15) from the user and prints this shape:
For n=3

PLZ Click here to see the image .

I wrote this code, but I don't know what to do with line #13.
I appreciate your help!

#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int i,j,n;
cout << "Enter n: ";
cin >> n;
for (i=1; i<=(2*n-1) ; i++)
{
	for (j=1; j<=(n-i); j++)
		cout << " ";
	for (j=1; j<=(2*i-1); j++)
		cout << "*";
		cout << "\n";
}

getch();
}

What does n represent? Here is your program with brackets. Is this what you want?

#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int i,j,n;
cout << "Enter n: ";
cin >> n;
for (i=1; i<=(2*n-1) ; i++)
{
	for (j=1; j<=(n-i); j++)
        {
		cout << " ";
        }
	for (j=1; j<=(2*i-1); j++)
        {
		cout << "*";
        }
	cout << "\n";
}

getch();
}

We have (2n-1) lines. That's whan n represents.

What does n represent? Here is your program with brackets. Is this what you want?

#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int i,j,n;
cout << "Enter n: ";
cin >> n;
for (i=1; i<=(2*n-1) ; i++)
{
	for (j=1; j<=(n-i); j++)
        {
		cout << " ";
        }
	for (j=1; j<=(2*i-1); j++)
        {
		cout << "*";
        }
	cout << "\n";
}

getch();
}

check this code

void main() 
{
   const int N=5;     //N can be any number

   for (int i = 0; i <= 2 * N; i++) 
		{
      		for (int j = 0; j <= 2 * N; j++) 
			{
         	if (i <= N) 
			{
            			if (j < N - i || j > N + i) 
					{
               					cout << ' ';
            				}
            			else 
					{
               					cout << '*';
            				}
         		}
         	else 
			{
            		if (j < i - N || j > 3 * N - i) 
					{
              			 		cout << ' ';
            				}
            		else 
					{
               					cout << '*';
            				}
         		}
      }
     cout << endl;
   }
}

We have (2n-1) lines. That's whan n represents.

Look at the code I posted (your code with brackets). How many times does line 32 execute? If you want it to execute more than once, put it inside of a loop.

Run naseerkhan861's code. It works. The program can be written a number of different ways.

As a side note, there are a lot of threads about "void main", "iostream.h", "conio.h", and the like. I had to take all that stuff out to get the program to compile. Take a look at this page:

http://www.cplusplus.com/reference/

If you want everyone to be able to run your code, regardless of operating system and compiler, use "int main" rather than "void main" and use the libraries listed here. Your code will be portable that way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.