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;
}
}
1 Answer 1
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.
-
\$\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\$sirajalam049– sirajalam0492017年12月01日 15:06:54 +00:00Commented Dec 1, 2017 at 15:06
-
\$\begingroup\$ @nwp gave you a suggestion on that already. You might want to reply to thier comment. \$\endgroup\$2017年12月01日 15:13:28 +00:00Commented Dec 1, 2017 at 15:13
n
. And then decreasing from 1 (if n is even) or 2 (if n is odd) less thann
. So firstly, do I need to find out whether the number is even or odd? \$\endgroup\$n
s. You just print the odd lines starting at1
until you hit some limit (n/2
?) with a simplefor
loop and then the even values fromn/2
starting atn/2
(rounded down?) until you reach 0 with a secondfor
loop. You just need to try to not make off-by-1 errors and test your solution properly. \$\endgroup\$itr
, that is, ifn
is (say 5) so the first number in a row will ben*itr+1
, which is 1*2*3*4*5, that meansitr
is 0, so the starting is from 0 and changing as (ifn
is 5) 0, 2, 4, 3, 1. So the maximum limit is that even number which is less thann
(4 in this case) so we can't say it isn/2
as if we taken = 4
so it will change as 0,2,3,1. So here it isn/1
, so it depends on whether the number is even or odd. \$\endgroup\$