Pattern mountain prob

https://ide.codingblocks.com/s/44870 code work for input 4 but for input 5 or any other no it does not work

Hey Mayur, you are supposed to write a generalised code which works for every case
for(int k=1;k<=7-2*i;k++) => this for loop’s terminating condition is not generalised, so it’s not working for all other cases. So, a think a lil bit more how you can make a relation for the number of spaces in every line based on row number.

Now i understand the logic

Okay, Here i am sharing my code for your reference.

1 Like

Sir how do you think that spaced should be 2n-2i-1 as no pattern is shown

Hey Mayur, we can observe this from the pattern as given below
let N=3
for 1st row i.e. i=1 , there are 3 spaces between left 1 and right 1
and 2n-2i-1 => 23 - 21 -1 => 3
or 2st row i.e. i=2 , there is 1 spaces between left 2 and right 2
and 2n-2i-1 => 23 - 22 -1 => 1
and at last for i=n=3 there is an if check for printing 3 only once.

So, in this way you can his is the relation between the current row and total no.of rows for printing the spaces.

Sir in hollow diamond pattern

what should be logic for spaces as my only work for n=5

Hey Mayur, there can be many approaches for this pattern, one of them can be
let a variable count = (N/2)+1; i -> denotes the current row, spaces -> denotes the spaces in between
i=1, spaces=1
iterate in a loop i=1 to count
every time increase space by 2 i.e. spaces = spaces+2;

then iterate in a loop i = 1 to (N-count)
every time decrease space by 2 i.e. spaces = spaces-2;

IMG_20181229_211017
Why the answer is auto

Hey Mayur, the auto keyword can be used in place of a variable’s type when doing an initialization in order to perform type inference. You can refer the given links to explore about auto in c++

https://www.learncpp.com/cpp-tutorial/4-8-the-auto-keyword/

Why the answer in auto unable to understand


Why here run time error please explain

Hey Mayur, it will give run error because TAB() return pointer to variable temp and temp is local variable in TAB() so it can’t be accessed outside TAB(). Therefore *ptr will give the run time error because you are trying to access the memory which doesn’t exist as temp is local variable in TAB() and temp variable get destroyed after TAB().

But ptr get the address of temp so why run time error as we pass the address int value so why it shows

because that address doesn’t exist now and if you try to access that address later it will give you a run error.

Is it not return the address of temp

it returns the address of variable temp but that temp variable is destroyed after function call TAB().

Sir if we let address of temp is 2 in tab function and in victim function cout << ptr ; is output will be 2

Yes, but here you are trying to store 42 at that address which don’t exist now in function victiim.

Is the run time error because ptr store the address of the variable which is not exist