11 Replies - 915 Views - Last Post: 25 August 2011 - 05:34 PM Rate Topic: -----

#1 benboi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-August 11

Asterisks visual in C++. Please Help

Posted 20 August 2011 - 05:39 AM

Hi!, im a newbie in programming.

how can i make this is C++?
* * * * * *
_* * * * *
__* * * *
___* * *
____* *
_____*
_____*
____* *
___* * *
__* * * *
_* * * * *
* * * * * *



AND THIS

_____*
____* *
___* * *
__* * * *
_* * * * *
* * * * * *
* * * * * *
_* * * * *
__* * * *
___* * *
____* *
_____*


Im willing to learn this! and i want to learn this :(

This post has been edited by NickDMax: 20 August 2011 - 06:39 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Asterisks visual in C++. Please Help

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1575
  • View blog
  • Posts: 2,997
  • Joined: 30-May 10

Re: Asterisks visual in C++. Please Help

Posted 20 August 2011 - 05:51 AM

So learn!

for ( rowNumber = 0 ; rowNumber < ? ; rowNumber++ ) {
  for ( numSpaces = 0 ; numSpaces < ? ; numspaces++ ) {
    cout << " ";
  }
  for ( numStars = 0 ; numStars < ? ; numStars++ ) {
    cout << "* ";
  }
  cout << endl;
}


The limiting value for the inner loops is some function of the row number.
Can you spot it?
Can you see enough to at least ATTEMPT printing say an upper triangle?
Was This Post Helpful? 1
  • +
  • -

#3 benboi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-August 11

Re: Asterisks visual in C++. Please Help

Posted 20 August 2011 - 05:56 AM

Hi, I'm newbie in programming language in C++

Posted Image


How can i illustrate this in C++?
Was This Post Helpful? 0
  • +
  • -

#4 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1575
  • View blog
  • Posts: 2,997
  • Joined: 30-May 10

Re: Asterisks visual in C++. Please Help

Posted 20 August 2011 - 06:10 AM

You can read your other thread and start writing some code that say counts from 1 to 10.
http://www.dreaminco...-c-please-help/

What you're NOT going to get is an answer on a plate.
Was This Post Helpful? 1
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: Asterisks visual in C++. Please Help

Posted 20 August 2011 - 06:42 AM

The way things work here is that you attempt to the program - if you are not successful you post what you have and we see if we can help you get it working. You need to show some effort.

Salem_c has already been more than generous with a code snippet. Just figure out what goes in the question marks and you have a basic algorithm.
Was This Post Helpful? 0
  • +
  • -

#6 benboi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-August 11

Re: Asterisks visual in C++. Please Help

Posted 25 August 2011 - 02:45 AM

View PostNickDMax, on 20 August 2011 - 06:42 AM, said:

The way things work here is that you attempt to the program - if you are not successful you post what you have and we see if we can help you get it working. You need to show some effort.

Salem_c has already been more than generous with a code snippet. Just figure out what goes in the question marks and you have a basic algorithm.




i try my best but this is only what i can make.

#include <iostream>
using namespace std;

void main()
{
	int a = 1, b = 6;
	for(int i = 1;i <= 7;i++)
	{	
		for(int k = b; k >= 1; k--)
				{
					cout<<" ";
				}
				b--;
			for(int j = a;j >= 1; j--)
			{
				cout<<"* ";
			}
			a++;
			cout<<endl;
	}	
	system("PAUSE");
}


Mod Edit:
:code:!!!
Was This Post Helpful? 0
  • +
  • -

#7 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Asterisks visual in C++. Please Help

Posted 25 August 2011 - 05:43 AM

Here's what the compiler says about your code
DIC.cpp:4:1: error: 'main' must return 'int'
void main()
^
1 error generated.



You have the bottom of the left hand figure.
Show us a real try at producing the top.
The ideas are the same so if you wrote the code you are showing us you should be able to make a good attempt within 10 or 20 tries.
Was This Post Helpful? 0
  • +
  • -

#8 benboi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-August 11

Re: Asterisks visual in C++. Please Help

Posted 25 August 2011 - 09:08 AM

View Postjanotte, on 25 August 2011 - 05:43 AM, said:

Here's what the compiler says about your code
DIC.cpp:4:1: error: 'main' must return 'int'
void main()
^
1 error generated.



You have the bottom of the left hand figure.
Show us a real try at producing the top.
The ideas are the same so if you wrote the code you are showing us you should be able to make a good attempt within 10 or 20 tries.





But in that code i can only make pyramind, i need algorithm on how to make that like hourglass or diamond
Was This Post Helpful? 0
  • +
  • -

#9 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1575
  • View blog
  • Posts: 2,997
  • Joined: 30-May 10

Re: Asterisks visual in C++. Please Help

Posted 25 August 2011 - 09:30 AM

If you've managed to produce say
   *
  * *
 * * *



Then to make the whole diamond, you basically do the same again, only backwards.
Was This Post Helpful? 1
  • +
  • -

#10 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Asterisks visual in C++. Please Help

Posted 25 August 2011 - 10:04 AM

Just to say the same stuff in a different way.

A diamond is two pyramids.
An hourglass is two pyramids.

You know how to make a pyramid with the pointy end up.
Now use the same idea with some changes to make a pyramid with the pointy end down.

Now you know how to make both the shapes you need.

Then glue the two pyramids together to make whatever shapes you want to make.
Was This Post Helpful? 1
  • +
  • -

#11 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: Asterisks visual in C++. Please Help

Posted 25 August 2011 - 10:06 AM

So you have got part done... now look at how it works and ask youself "how can I flip this upside down?"

problem solving in programming tends to be done with two approaches:

Bottom-up: these are the functions I have available to me, how can I fit them together to solve the problem.

Top-down: Here is the problem how to break it up into smaller pieces that are easier to solve.

Often we switch from one to the other as we break problems up. For example your program can be solved easily if you can find a way to print triangles up and down. Looking at the problem from this point of view is "top-down"

to form the triangle we need to break up a triangle into some kind of loop where each row prints the right number of stars. (top-down)

To print a row: Well we can use cout, " ", and "* ", print the number of spaces needed to indent, then print the "* "'s (bottom-up)

So my point is: Try to break the problem up into little steps that you can do. Write that, compile it, run it. Then work on the next level, write compile. Computer programs are not written all at once. They are built layer by layer, problem by problem.
Was This Post Helpful? 1
  • +
  • -

#12 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Asterisks visual in C++. Please Help

Posted 25 August 2011 - 05:34 PM

Looking at the code you posted, there are two continuations to solve the hour glass figure, problem:

1) Consider the hour glass figure two separate tasks - a top part triangle, and a bottom part triangle. You have one part working. Now repeat that code, with the changes you need, to make the other triangle. They are, after all, quite similar in the logic you need.

2) Consider the hour glass figure as one big task, and you have half of it working now. Increase the row number by two times it's current size, and in the inner for loop, add an if statement:

//int numOfStars = number of stars that should be printed on the row
if(row <= totalRows/2)
  decrease numOfStars by 2  
else
  increase numOfStars by 2



That's not the only way to do this exercise, but it is one way. If you are stymied by it still, work it out by hand with paper and pencil, several times - it will come to you. ;)

Note: I would replace the i and k variable names, with row and col (for column). Although I like i,j,k for simple counters, when you have to debug a program dealing with rows and cols, it's just more intuitive to use row and col for your loop counters.

You're hearing this from a hypocrite, because my own programs have nested loops that go from i through p, ;) but that's best done, after you know clearly what you're doing in all those nested loops.

I hope you're carefully reading the replies you're getting above this post - they're excellent!

This post has been edited by Adak: 25 August 2011 - 05:42 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1