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.

## All 9 Replies

This is a trivial homework assignment and we won't help you cheat. Do it yourself.

#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!!!!

>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.

> 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.

New_members_chased_away_this_month ++;
commented: Nice +1

#

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

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

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.