0
\$\begingroup\$

Write a function that will take an integer argument say n. n ranges from 2 <= n <= 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 < n; i++) {
 k = i*n + 1;
 if (toggle) {
 for (int j = 0; j < n; j++) {
 if (k < n || k%n)
 cout << k << "*";
 else
 cout << k;
 k++;
 }
 cout << endl;
 toggle = 0;
 }
 else {
 top++;
 arr[top] = i;
 toggle = 1;
 }
 }
 while (top != -1) {
 k = arr[top] * n + 1;
 for (int i = 0; i < n; i++) {
 if (k < n || k%n)
 cout << k << "*";
 else
 cout << k;
 k++;
 }
 cout << endl;
 top--;
 }
}

Using STL Stack

void showPattern(int n)
{
 stack <int> stk;
 int itr, toggle = 1;
 for (int i = 0; i < n; i++) {
 if (toggle) {
 itr = i*n + 1;
 for (int j = 0; j < n; j++) {
 if (itr < n || itr%n) {
 cout << itr << "*";
 }
 else {
 cout << itr;
 }
 }
 cout << 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 < n; j++) {
 if (itr < n || itr%n) {
 cout << itr << "*";
 }
 else {
 cout << itr;
 }
 }
 cout << endl;
 }
}
asked Dec 1, 2017 at 12:06
\$\endgroup\$
4
  • 1
    \$\begingroup\$ You don't need dynamic memory for this. Observe the patterns of the lines: First you print the lines 1, 3, 5, 7, ... and then ..., 6, 4, 2. You should be able to significantly improve your code with that. \$\endgroup\$ Commented Dec 1, 2017 at 13:05
  • \$\begingroup\$ It is growing like 2, 4, 6 and so on till1 ( if n is odd) or 2 (if n is even) less than n. And then decreasing from 1 (if n is even) or 2 (if n is odd) less than n. So firstly, do I need to find out whether the number is even or odd? \$\endgroup\$ Commented Dec 1, 2017 at 16:31
  • \$\begingroup\$ I don't see a difference for even or odd ns. You just print the odd lines starting at 1 until you hit some limit (n/2?) with a simple for loop and then the even values from n/2 starting at n/2 (rounded down?) until you reach 0 with a second for loop. You just need to try to not make off-by-1 errors and test your solution properly. \$\endgroup\$ Commented Dec 1, 2017 at 16:39
  • \$\begingroup\$ I have to print numbers in a line is the function of say itr, that is, if n is (say 5) so the first number in a row will be n*itr+1, which is 1*2*3*4*5, that means itr is 0, so the starting is from 0 and changing as (if n is 5) 0, 2, 4, 3, 1. So the maximum limit is that even number which is less than n (4 in this case) so we can't say it is n/2 as if we take n = 4 so it will change as 0,2,3,1. So here it is n/1, so it depends on whether the number is even or odd. \$\endgroup\$ Commented Dec 1, 2017 at 17:29

1 Answer 1

4
\$\begingroup\$

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.

answered Dec 1, 2017 at 14:36
\$\endgroup\$
2
  • \$\begingroup\$ Is there any better logic for this? I just made it as it comes into my mind but don't know if there's a better logic for this. \$\endgroup\$ Commented Dec 1, 2017 at 15:06
  • \$\begingroup\$ @nwp gave you a suggestion on that already. You might want to reply to thier comment. \$\endgroup\$ Commented Dec 1, 2017 at 15:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.