954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Solve this guys

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.

Attachments untitled.JPG 3.53KB
faisal.alawar
Newbie Poster
3 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

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

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
#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!!!!

faisal.alawar
Newbie Poster
3 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

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

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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

faisal.alawar
Newbie Poster
3 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

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

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
New_members_chased_away_this_month ++;
Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

#

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

Rahul.menon
Light Poster
29 posts since Sep 2010
Reputation Points: 10
Solved Threads: 1
 
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

prvnkmr449
Junior Poster
106 posts since Sep 2010
Reputation Points: 3
Solved Threads: 16
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: