C++ Pattern Help

This is a discussion on C++ Pattern Help within the C++ Programming forums, part of the General Programming Boards category; Hellow forum. I am new to the forum but would like some help. I am new to c++ studying it ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    4

    C++ Pattern Help

    Hellow forum. I am new to the forum but would like some help. I am new to c++ studying it at a Univercity(just started). But i need help with a question i got in our new program set.\

    4. Write a program that displays the following pattern

    *
    **
    ***
    ****
    *****
    ******
    *******
    ********

    Your program may use ONLY three types of output statements, one of each of the following forms:
    cout << * ;
    cout << ;
    cout << endl;

    You must use a loop to solve this problem. The type of loop you choose to use is up to you. You cannot submit a solution that does not use looping of some form.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    What have you tried, or at least what do you have in mind?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    4
    Well... so far i think i need to use 2 nested while loops....one for counting down (the outer most loop) and the other for counting across (the inner most loop).

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    So write the code for main and the loops and get it to compile and run (even if it does nothing). Then once that's working you can work on getting it to display the *'s correctly.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    4
    Code:
    #include <iostream>
    using namespace std;
    
    int main(){
    	
    	for( int i = 0; i < 10; i++ ){
    	
    		for(int x = 0; x < i+1; x++){
    			cout<<"*";
    		}
    		cout<<endl;		
    	}
    	
    	return 0;	
    }
    But the ** displays the wrong direction

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    But the ** displays the wrong direction
    The code looks okay. I went so far as to test it, and it appears to work just as your assignment described.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    If you wanted a pattern like this:
    Code:
      *
     **
    ***
    then you'd just have to print 10-i spaces before you go printing asterisks.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    >> But the ** displays the wrong direction
    Put the assignment (or the example) in code tags, not quote tags, to make sure it displays properly in the forum. I assume that you want it like dwks showed, but if not then you'll have to post the example again so that the leading spaces aren't removed by the forum software.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Put the assignment (or the example) in code tags, not quote tags, to make sure it displays properly in the forum. I assume that you want it like dwks showed, but if not then you'll have to post the example again so that the leading spaces aren't removed by the forum software.
    Just the example. Otherwise those long lines of text would run off the edge of the screen, if you had a reasonably small monitor.

    BTW, "x <= i" might be easier to read than "x < i+1".
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    4
    Quote Originally Posted by dwks View Post
    If you wanted a pattern like this:
    Code:
      *
     **
    ***
    then you'd just have to print 10-i spaces before you go printing asterisks.

    How could i do that?

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Umm . . . so you need 10-i spaces. How do you create a loop that loops 10-i times?

    The loop
    Code:
    for(x = 0; x < N; x ++)
    loops N times, so how do you think you loop 10-i times?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visitor / Decorator Pattern
    By MarkZWEERS in forum C++ Programming
    Replies: 9
    Last Post: 05-16-2009, 11:53 AM
  2. Need Help Displaying A Pattern.
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 10-05-2005, 11:01 AM
  3. Hmm.. Ai? Finding the pattern in number squences?
    By Zeusbwr in forum C++ Programming
    Replies: 8
    Last Post: 04-02-2005, 05:13 PM
  4. (pattern *) pat & pattern * pat
    By siubo in forum C Programming
    Replies: 1
    Last Post: 04-08-2003, 10:03 PM
  5. text pattern recognition
    By mtsmox in forum C++ Programming
    Replies: 5
    Last Post: 02-27-2002, 07:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21