Write a program that prompts the user for an odd positive integer, n, greater than 1, and then
prints the following hourglass figure which has a height of n text lines:
In this example: n = 9
********* (please refer to the attachment for the figure)
* *
* *
* *
*
* *
* *
* *
*********
Your program should check that the user enters a valid number for n and print an error message
if not. Make sure to use the right method. Your output should look exactly like the figure above:
symmetric and left-justified, with all stars * and spaces in the locations as shown. It should be
scaled appropriately for the given value of n. Please make sure that your program works for all
valid cases (e.g. check for n=3). It is recommended to break the entire job into small tasks to
handle each region of the figure. Make sure to use loops.

Edited 6 Years Ago by faisal.alawar: n/a

#include <iostream>

using namespace std;
int main()
{
	int n;
	cout<<"            *********************************"<<endl;
	cout<<"            *************Problem3************"<<endl;
	cout<<"            *********************************"<<endl<<endl<<endl;

	cout<<"        Please type and odd integer greater than 1"<<endl;
	
	cin>>n;

	if(n%2==0)
	{


		cout<<"YOU DID NOT TYPE AN ODD INTEGER !!!"<<endl;
	}
	else
	{
		if(n==1)
		{
			cout<<"YOU NEED AN ODD INTEGER GREATER THAN 1 !!!"<<endl;
		}
		else
		{
			for(int i=(n-1)/2;i>0;i--)
			{
				for(int j=(n-1)/2;j>i;j--)
				cout<<" ";
				for(int k=0;k<i;k++)
				cout<<" *";
				cout<<"\n";
			}
			for(int i=0;i<(n-1)/2+1;i++)
			{
				for(int j=(n-1)/2+1;j>i;j--)
				cout<<" ";
				for(int k=0;k<i;k++)
				cout<<" *";
				cout<<"\n";
			}
		
		}

	}

i got to something but i still have some final touches please can someone help me!!!!

Edited 6 Years Ago by Nick Evan: Fixed code-tags

>i got to something but i still have some final
>touches please can someone help me!!!!

Wow, how lazy can you get? You're basically done. Even a braindead code monkey could finish this program off using trial and error. The first and last rows are super easy, you can subtract one line from the two triangles (because they step on each other), and the second triangle needs to be shifted left by one space. I'd guestimate 15 minutes of work, tops.

Edit: Ah, you also need to print spaces instead of asterisks between the first and last character of each triangle row. That's not difficult either.

Edited 6 Years Ago by Narue: n/a

> You're basically done.
Seems unlikely. More like someone else was done; then it was "found" and reposted here for us to tinker with.

As you say, anyone who could write that off their own bat would have no trouble finishing it.

ok thank you a lot i thaught that this website was usefull but it turned out that all its members are arrogant and wont help a beginner in c++, so thank you

>i thaught that this website was usefull
It is...for people who are interested in learning and ask real questions. All you did was post code hoping someone would solve all your problems for you.

>but it turned out that all its members are arrogant
I strongly recommend that you don't generalize like that. You're certain to offend the members who aren't arrogant. You'll probably also offend the members who are arrogant, but feel that refusing to help a lazy student doesn't warrant that particular label.

>and wont help a beginner in c++
I've helped countless beginners in C++. A lot of them weren't even worth the effort, but I did it anyway. If you had asked a smart question, I probably would have helped you too. But you didn't, so I didn't. C'est la vie. :)

>so thank you
My pleasure.

#

include <stdio.h>

int main()
{
	int n;
	cout<<"            *********************************"<<endl;
	cout<<"            *************Problem3************"<<endl;
	cout<<"            *********************************"<<endl<<endl<<endl;

	cout<<"        Please type and odd integer greater than 1"<<endl;
	
	cin>>n;

	if(n%2==0)
	{


		cout<<"YOU DID NOT TYPE AN ODD INTEGER !!!"<<endl;
	}
	else
	{
		if(n==1)
		{
			cout<<"YOU NEED AN ODD INTEGER GREATER THAN 1 !!!"<<endl;
		}
		else
		{
			for(int i=(n-1)/2;i>0;i--)
			{
				for(int j=(n-1)/2;j>i;j--)
				cout<<" ";
				for(int k=0;k<i;k++)
				cout<<" *";
				cout<<"\n";
			}
			for(int i=0;i<(n-1)/2+1;i++)
			{
				for(int j=(n-1)/2+1;j>i;j--)
				cout<<" ";
				for(int k=0;k<i;k++)
				cout<<" *";
				cout<<"\n";
			}

half of triangle is done... will have to repeat the same again for another part

Edited 6 Years Ago by Nick Evan: Added code-tags

for(int i=0;i<n;i++)
			{
			if(i==0||i==n-1)//for top  and bottom line
			for(int j=0;j<n;j++)
			cout<<"*";

			else if(i>(n-1)/2)//for lower triangle
			{ for(int j=0;j<(n-i-1);j++)
				cout<<" ";
			  cout<<"*";
			  for(j=0;j<(i-(n+1)/2)*2+1;j++)
				cout<<" ";
			  cout<<"*";

			}
			else if(i!=0&&i!=n-1)//for upper triangle
			{  for(int j=0;j<i;j++)
				cout<<" ";
			   cout<<"*";
			   for(j=0;j<n-(i+1)*2;j++)
				cout<<" ";
			   if(j!=0)
			   cout<<"*";
			}
			cout<<endl;
			}

Use this at the place of you else part where you start looping
Enjoy this

This article has been dead for over six months. Start a new discussion instead.