Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,100 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,650 people online right now. Registration is fast and FREE... Join Now!




Create Pattern

 
Reply to this topicStart new topic

Create Pattern, Need to duplicate this pattern using (++) and (--)

ahsesino
15 Oct, 2007 - 04:29 PM
Post #1

New D.I.C Head
*

Joined: 15 Oct, 2007
Posts: 10


My Contributions
CODE
#include < iomanip >
#include <iostream >
using namespace std;
int main()
{
    int plus;

    for ( plus = 0; plus < 1; plus++ )
    
        cout << "+\n";
        
    

    for ( plus = 1; plus < 13; plus++ )
    
        cout << "+";

    for ( plus = 2; plus < 12; plus++ )
    
        cout << "+";
        
}


the program should display this:
+
++
+++
++++
+++++
++++++
+++++++
++++++++
+++++++++
++++++++++
+++++++++++
++++++++++++

User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Create Pattern
15 Oct, 2007 - 04:49 PM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Okay...so what does it display? Are you having errors with the code, or does it simply not output what you want it to? You need to post you actual issues as well as the code.

And when posting code, please use the code tags (take a look at the grey text on the back of the post/reply text box, or highlight and click the # button).

-jjh
User is offlineProfile CardPM
+Quote Post

Topher84
RE: Create Pattern
16 Oct, 2007 - 02:06 AM
Post #3

D.I.C Head
Group Icon

Joined: 4 Jun, 2007
Posts: 232


Dream Kudos: 25
My Contributions
CODE

#include <iostream>

using namespace std;

const int MAX_LINE_SIZE = 12; //size of max line of "+"

int main()
{
    //go through loop for as many times as the max size of the line of +
    for(int nIndex = 0; nIndex < MAX_LINE_SIZE; nIndex++)
    {
        //go through loop and display the + for as many times as the index of the 1st loop
        for(int nIndex2 = 0; nIndex2 < nIndex; nIndex2++)
        {
            cout << "+"; //display +
        }//end for
        cout << endl; //display new line after each set of +
    }//end for
}//end main


You need to use nested for loops which is an O(n^2) algorithm which is bad for large values but good for small values of nIndex. Regardless.. this does what you are wanting
User is offlineProfile CardPM
+Quote Post

ahsesino
RE: Create Pattern
16 Oct, 2007 - 10:19 AM
Post #4

New D.I.C Head
*

Joined: 15 Oct, 2007
Posts: 10


My Contributions
QUOTE(Topher84 @ 16 Oct, 2007 - 03:06 AM) *

CODE

#include <iostream>

using namespace std;

const int MAX_LINE_SIZE = 12; //size of max line of "+"

int main()
{
    //go through loop for as many times as the max size of the line of +
    for(int nIndex = 0; nIndex < MAX_LINE_SIZE; nIndex++)
    {
        //go through loop and display the + for as many times as the index of the 1st loop
        for(int nIndex2 = 0; nIndex2 < nIndex; nIndex2++)
        {
            cout << "+"; //display +
        }//end for
        cout << endl; //display new line after each set of +
    }//end for
}//end main


You need to use nested for loops which is an O(n^2) algorithm which is bad for large values but good for small values of nIndex. Regardless.. this does what you are wanting



Thanks alot I just could'nt figure out how to create the loop witout having the rows and columns all being the same I guess I was thinking too hard or probable not thinking at all.


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 08:48PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month