String pattern printing challenge Write a function that will take an integer argument say n.
n ranges from 2 &lt;= n &lt;= 100

The function would not take any argument from the user.
the function should print the pattern according to the value of n.


Let's say if

n = 3 so output should be.

1*2*3
7*8*9
4*5*6  



n = 4

1*2*3*4
9*10*11*12
13*14*15*16
5*6*7*8  



n = 5

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

and so on. . .


  Without using STL Stack


void showPattern(int n)
{
    int *arr = (int*)malloc(n * sizeof(int));
    int toggle = 1;
    int k;
    int top = -1;
    for (int i = 0; i &lt; n; i++) {
        k = i*n + 1;
        if (toggle) {
            for (int j = 0; j &lt; n; j++) {
                if (k &lt; n || k%n)
                    cout &lt;&lt; k &lt;&lt; "*";
                else
                    cout &lt;&lt; k;
                k++;
            }
            cout &lt;&lt; endl;
            toggle = 0;
        }
        else {
            top++;
            arr[top] = i;
            toggle = 1;
        }
    }

    while (top != -1) {
        k = arr[top] * n + 1;
        for (int i = 0; i &lt; n; i++) {
            if (k &lt; n || k%n)
                cout &lt;&lt; k &lt;&lt; "*";
            else
                cout &lt;&lt; k;
            k++;
        }
        cout &lt;&lt; endl;
        top--;
    }

}



  Using STL Stack


void showPattern(int n)
{
    stack &lt;int&gt; stk;
    int itr, toggle = 1;

    for (int i = 0; i &lt; n; i++) {

        if (toggle) {
            itr = i*n + 1;
            for (int j = 0; j &lt; n; j++) {
                if (itr &lt; n || itr%n) {
                    cout &lt;&lt; itr &lt;&lt; "*";
                }
                else {
                    cout &lt;&lt; itr;
                }
            }
            cout &lt;&lt; endl;
            toggle = 0;
        }
        else {
            stk.push(i);
            toggle = 1;
        }
    }

    int i;
    while (!stk.empty()) {
        itr = stk.top()*n + 1;
        stk.pop();
        for (int j = 0; j &lt; n; j++) {
            if (itr &lt; n || itr%n) {
                cout &lt;&lt; itr &lt;&lt; "*";
            }
            else {
                cout &lt;&lt; itr;
            }
        }
        cout &lt;&lt; endl;
    }

}
Prefer Clear Naming over using namespace std

According to the [MSDN website]:


  Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.


A collision is when 2 different functions have the same name, the same arguement types and a
similar functionallity (this is why they have the same name). Someone developing software may want
to override a function such as std::cout, std::cin or they may want to override the functionallity
of a class such as std::vector or std::stack. Namespaces allow these constructs to be overriden.

The use of the programming statement:

using namespace std;


hides the fact that cin, cout, vector and stack are coming from the namespace std where cin, cout,
vector and stack are used in the code. This can cause confusion of where the code is actually
coming from.

As the software becomes more complex and uses more libraries this becomes a bigger problem.

For a more detailed discussion of why it is a bad idea to use using namespace std;
see this stackoverflow question and this stackoverflow question.

The Use of new is Preferred Over malloc in C++
The code contains the line:

    int *arr = (int*)malloc(n * sizeof(int));


This is a C programming language construct. In C++ it is much more accepted to use

    int *a = new int[n];


Even this is out dataed because of all the container classes, and generally using pointers is
frowned upon in C++ because it is error prone.

Declare Variables When Needed
In the older versions of the C programming language one had to put the variable declarations at
the top. The current versions of C and C++ allow variables to be declared as needed.

An example of this is to move the variable toggle to be within the for loop.

Limit the Use of std::endl
It would be better to use '\n' to insert the new lines in the loops because std::endl does a file flush
each time it is called. This makes it an expensive operation.

Use Boolean Variables
In the C programming language there is no bool variable type unless one includes stdbool.h. In the
C++ programming language it would be better to implement the variable toggle as a bool variable
rather than as an int. Then you can use true and false rather than 1 and 0 respectively.