Write a program to print the following pattern based on user input.
For eg: If N=4 the code should print the following pattern.
                                 1*2*3*4*17*18*19*20
                                 --5*6*7*14*15*16
                                 ----8*9*12*13
                                 ------10*11
Again if N=5 the code should print 
                                 1*2*3*4*5*26*27*28*29*30
                                 --6*7*8*9*22*23*24*25
                                 ----10*11*12*19*20*21
                                 ------13*14*17*18
                                 --------15*16
For N=2  the pattern will be
                                 1*2*5*6
                                 --3*4
PLEASE HELP AS SOON AS POSSIBLE.


                             ----8*9*12*13
                             ------10*11

Recommended Answers

All 5 Replies

PLEASE HELP AS SOON AS POSSIBLE.

Please read our rules as soon as possible. Thanks.

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int i,j,k,l,m=0,g,n,in,w=1,sp;
cout<<"enter input\n";
cin>>in;
g=in-1;
i=1;
n=in;
j=in;
int p=in;
while(n>0)
{
for(sp=1;sp<=w;sp++)
cout<<"-";
for(k=1;k<=j;k++,i++)
{
cout<<"*"<<i;
}
for(l=1;l<=n;l++)
{
cout<<"*"<<pow(p,2)+l-m;
}
cout<<endl;
m=m+g;
g--;
n--;
j--;
w++;
}
getch();
}
i think it would print something like that.

Aditya_13: While your willingness to help is commendable, it is ill-advised in this instance. Why? Because the OP didn't show any evidence that they had put any effort into the project themselves. There is a rule here a Daniweb that querents have to give some indication that they have tried to solve a problem themselves before they asked for help, whether through posting their code, or asking a meaningful question about the problem set that shows that they had at least thought through the problem.

SUBHENDU_1 did none of that. Instead, the OP simply did a datadump of the assignment and demanded that we provide a solution. This is called cheating in any school worth its salt, and we won't give support to such laziness. The OP will learn nothing from having the solution provided to them, other than an inflated sense of entitlement. If for no better reason than the fact that one of us may have to work with this person someday, the last thing we want is to help someone get a degree they didn't earn.

While it is sometimes a tricky line to avoid crossing - most of us here have done either too much or too little to help someone in the past - this case is clear-cut. The OP hasn't given any sign of trying to solve the problem, and probably spent more time finding this forum to beg for a handout than it would have taken to write the damn program themselves. None of the regulars here will condone that.

Further to the above Posting Pro @ Schol-R-LEA comments ...

and since ... the meaning to be helpful @ Aditya_13 ...
posted his first code here ...

please also note these comments that are also meant to be helpful to, now especially, @ Aditya_13 ...

Please take a look at the revisions and comments in this revision of the code posted, that uses some of the power tools of C++ ... power tools like over-loading operators and vector containers ... and of course ... the general tool of breaking up a problem into steps ... each step having its own function

The goal here was not to as brief as possible, but rather to make clearer the steps to a solution ...

// print_pattern.cpp //

// these are old style //
//#include<iostream.h>
//#include<conio.h> // do NOT use if you wish code to be portable //
//#include<math.h>

#include <iostream>
#include <string>
#include <vector>
#include <cmath>

using namespace std;

#define myDebug 0 // toggle zero & one ... to turn off & on //


/////////////////////////////////////////////
// some helpful student utility functions //
int takeInInt( const char* msg);
int takeInChr( const char* msg );
bool more();
/////////////////////////////////////////////


/*
If N=4 the code should print the following pattern.
                                 1*2*3*4*17*18*19*20
                                 --5*6*7*14*15*16
                                 ----8*9*12*13
                                 ------10*11
Again if N=5 the code should print
                                 1*2*3*4*5*26*27*28*29*30
                                 --6*7*8*9*22*23*24*25
                                 ----10*11*12*19*20*21
                                 ------13*14*17*18
                                 --------15*16
For N=2  the pattern will be
                                 1*2*5*6
                                 --3*4

*/

// from the pattern above, it seems that ...
// if n = 4, 4*5 = 20 numbers are to be printeed
// if n = 5, 5*6 = 30 ..........................
// if n = 2, n*(n+1) = 2*3 = 6 .................
// etc ...

int numbersToBePrinted( int n )
{
    return n*(n+1);
}

// to help print the pattern ///
ostream& operator << ( ostream& os, const vector< int >& v )
{
    if( v.size() )
        os << v[0];
    if( v.size() > 1 )
        for( size_t i = 1; i < v.size(); ++ i ) os << "*" << v[i];
    return os;
}




// also ... it seems that there are always 'n' rows
// with always 2 (middle) numbers on the LAST row)

// so could make an nxn matrix


typedef vector< vector < int > > Matrix;

void printPattern( const Matrix& a, const Matrix& b )
{
    for( size_t r = 0; r < a.size(); ++ r )
    {
        cout << string( r*2, '-' );
        cout << a[r] << '*' << b[r] << endl;
    }
}




// void main()
int main() // STANDARD  C/C++ returns an int for the main function //
{
    //clrscr();

    // this may work ... but it is hard to see what is going on here ...
    // SO ... it is NOT an example to be emulated ... //

/*
    int i,j,k,l,m=0,g,n,in,w=1,sp;
    cout<<"enter input\n";
    cin>>in;
    g=in-1;
    i=1;
    n=in;
    j=in;
    int p=in;
    while(n>0)
    {
        for(sp=1;sp<=w;sp++)
        cout<<"-";
        for(k=1;k<=j;k++,i++)
        {
         cout<<"*"<<i;
        }
        for(l=1;l<=n;l++)
        {
         cout<<"*"<<pow(p,2)+l-m;
        }
        cout<<endl;
        m=m+g;
        g--;
        n--;
        j--;
        w++;
    }
    //getch();
    */
    Matrix mat1, mat2;
    do
    {
        int n = takeInInt( "Enter n to see it's pattern: " );
        int lastN = numbersToBePrinted( n ); // always an EVEN number //
        int midN = lastN/2;

        mat1.resize(n); mat2.resize(n); // get n rows //


        // Now resize each row ...to 'right' size for that row //
        int valsPerRow  = n, i = 0;
        while( valsPerRow > 0 )
        {
            mat1[i].resize( valsPerRow );
            mat2[i].resize( valsPerRow );
            --valsPerRow, ++ i;
        }


         // now ... taking as flipped over ... start from bottom up //
        int topVal = midN;
        i = n-1;
        int j = mat1[i].size() - 1;
        while( i >= 0 )
        {
            mat1[i][j] = topVal;
            --topVal;
            --j;
            if( j < 0 )
            {
                -- i;
                j = mat1[i].size() - 1;
            }
        }
#if myDebug
        // print mat1
        for( size_t r = 0; r < mat1.size(); ++ r )
        {
            for( size_t c = 0; c < mat1[r].size(); ++ c )
                 cout << mat1[r][c] << '*';
            cout << endl;
        }
 #endif

        // now 'top dowm'  ... for mat2 ....
        topVal = lastN;
        i = 0;
        j = mat2[i].size()-1;
        while( i < n )
        {
            mat2[i][j] = topVal;
            --topVal;
            --j;
            if( j < 0 )
            {
                ++ i;
                j = mat2[i].size() - 1;
            }
        }
 #if myDebug
        // print mat2
        for( size_t r = 0; r < mat2.size(); ++ r )
        {
            for( size_t c = 0; c < mat2[r].size(); ++ c )
                 cout << mat2[r][c] << '*';
            cout << endl;
        }
 #endif

        printPattern( mat1, mat2 );

    }
    while( more() );


}




////////////////////////////////////////////////////////////
// BEGIN 3 UTILITIES helpful to students here //////////////
int takeInInt( const char* msg)
{
    int val = 0;
    while( true )
    {
        cout << msg << flush;
        if( cin >> val && cin.get() == '\n' )
            break;

        else
        {
            cout << "Valid integers only here please!\n";
            cin.clear(); // clear error flasgs
            while( cin.get() != '\n' ) ; // 'flush' cin stream //
        }
    }
    return val;
}

int takeInChr( const char* msg )
{
    cout << msg << flush;
    int reply = cin.get();
    if( reply != '\n' ) while( cin.get() != '\n' ) ; // flush... //
    return reply;
}

bool more()
{
    int reply = takeInChr( "More (y/n) ? " );
    if( reply == 'n' || reply == 'N' ) return false;
    // else ...
    return true;
}
// END 3 UTILITIES helpful to students here ////////////////
////////////////////////////////////////////////////////////

P.S.

This validation check should also be added ...

Matrix mat1, mat2;
do
{
    int n = takeInInt( "Enter n to see it's pattern: " );

    // add in this check //
    if( n < 1 )
    {
        cout << n << " < 1 is NOT valid input here ...\n";
        continue;
    }

    // .... rest of code ...
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.