GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 21-Oct-2009, 04:21
new programer new programer is offline
New Member
 
Join Date: Oct 2009
Posts: 3
new programer is on a distinguished road

Star Patterns


Hello all,

I have a problem in printing a certain patteren "as showed in the attachments.

Here is my tries so far

CPP / C++ / C Code:
# include <iostream>
using namespace std;

int main ()
{ 

	// Part a

	for (int i=1; i<=5; i++)
	{ 
		for (int h=1; h<=i; h++)
			cout << "*";
		    cout << endl;
	}
cout << endl;

    // Part b

   for (int i=5; i>=1; i--)
	{ 
		for (int h=5; h>=i; h--)
			cout << "*";
		    cout << endl;
	}


   return 0;

}

Please help
  #2  
Old 21-Oct-2009, 14:03
DRACO2032 DRACO2032 is offline
New Member
 
Join Date: Oct 2009
Posts: 4
DRACO2032 is on a distinguished road

Re: Star Patterns


I think it would be a good ideas for you to do this:

CPP / C++ / C Code:
# include <iostream>
using namespace std;

int main ()
{ 

	// Part a

	for (int i=1; i<=5; i++)
	{ 
		for (int h=1; h<=i; h++)
			cout << "*"<<endl;
		    cout << endl;
	}
cout << endl;

    // Part b

   for (int i=5; i>=1; i--)
	{ 
		for (int h=5; h>=i; h--)
			cout << "*"endl;
		    cout << endl;
	}


   return 0;

}
I mean if you are trying to get a pattern. Try it other wise fo rthe best of my little knowledge (x_x)
  #3  
Old 21-Oct-2009, 18:29
new programer new programer is offline
New Member
 
Join Date: Oct 2009
Posts: 3
new programer is on a distinguished road

Re: Star Patterns


Now it does not make the shapes I wanted

... at first it did but it did two identical ones although the conditions are different

it's a tricky code for a begginer lol

any help and remarks to achieve the output attached would be highly appreciated
  #4  
Old 22-Oct-2009, 02:12
Kimmo Kimmo is offline
Regular Member
 
Join Date: Mar 2007
Location: Finland
Posts: 388
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: Star Patterns


Quote:
Originally Posted by new programer
... at first it did but it did two identical ones although the conditions are different
I don't actually see any attachments that would show what kind of shape you're trying to produce. So perhaps you should include the shape in your next post?

As for the conditions being different and the shape being same... Look at the code you have in your loops. It's identical in both. How could it possibly produce anything but identical results?
  #5  
Old 24-Oct-2009, 01:14
new programer new programer is offline
New Member
 
Join Date: Oct 2009
Posts: 3
new programer is on a distinguished road

Re: Star Patterns


Oh I'm sorry

It must have slipped off the attachments

Please look into the attached file --to this post--
Attached Images
File Type: jpg Stars.jpg (7.3 KB, 6 views)
  #6  
Old 24-Oct-2009, 06:54
Kimmo Kimmo is offline
Regular Member
 
Join Date: Mar 2007
Location: Finland
Posts: 388
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: Star Patterns


It's pretty simple. Effectively you have to play around with printing spaces and printing stars. For example, in the shape you already managed to print, you first print one star and 4 spaces, then 2 stars and 3 spaces, then 3 stars and 2 spaces etc. Or to put it another way, on the first iteration (the first run through your outer for loop) you print 1 star, on the second you print 2 stars etc. Or to put it yet another way... If the current iteration number of the inner loop is smaller or equal to the current iteration number of the outer loop, print a star.

So, as an example, let's say the outer loop iteration number is 4. You enter the inner loop, the iteration number is 1. 1 <= 4 is true, so you print a star. Once the inner loop number goes to 5, you print a space which is exactly as it should be, since the outer loop number represents also the line number you're at.

Still following? As an example, here are the first two shapes based on this logic.
CPP / C++ / C Code:
#include <iostream>

using std::cout;
using std::endl;

int main()
{
    for (int i = 1; i <= 5; i++)
    {
        for (int j = 1; j <= 5; j++)
        {
            if (j <= i)
                cout << '*';
            else
                cout << ' ';
        }
        cout << endl;
    }
    
    cout << "\n\n";
    
    for (int i = 5; i >= 1; i--)
    {
        for (int j = 1; j <= 5; j++)
        {
            if (j <= i)
                cout << '*';
            else
                cout << ' ';
        }
        cout << endl;
    }
    
    return 0;
}

The last two shapes should be pretty easy once you understand it.
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
A weird problem of namespace meili100 C++ Forum 1 12-Feb-2008 22:47
FLTK - Can't fill in my star . . . Louis11 FLTK Forum 6 01-Nov-2007 18:12
Print diamond shaped star pattern Zayne C++ Forum 7 23-Apr-2006 13:00
Diamond Shape of Star (*) Symbols Zayne C++ Forum 2 21-Apr-2006 15:29
TOP PACKAGE: 20GB Diskspace | 700GB Diskspace | Hosting star UWebSmart Web Hosting Advertisements & Offers 0 23-Jan-2006 15:23

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:54.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.