We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Printing Pattern Using Loops
Printing Pattern Using Loops
Sort by
recency
|
1079 Discussions
|
Please Login in order to post a comment
I decided that i am not going to use ai so i thinked how to solve and tried and run each time upadating code in onlinegdp compiler then i got answer after three and half hour(3:30) so it take so much time but i learn very much
include
include
include
include
int numberOfLine(int a){ if(a==1) return 1; else if(a==2) return 3; else return (a+a)-1;
}
int main() {
}
My approach is that, since the figure is symmetrical, I solve the upper-left part and then mirror it to the right, downward, and to the lower-right quadrant. Although it’s not particularly elegant.
int main() {
}
int main() {
}
I tried it for more than half an hour could not solve it so turned to ChatGPT. Then i understood it. 1. First make variables to store the distance of a particular position from all the borders(top, left, right, bottom). 2. then make a new variable(I named it min_dist) to store the minimum distance from any border. 3. Make any distance from any border as min_dist. then check if the distances are lower than the min_dist. If lower then set that distance as new min_dist. 4. Lastly, print the value of that position which is (n-min_dist) HOW? See this structure as a square , the minimum distance from the outermost square to the outermost square is o ,SO,the outermost square is made of the integer n itself. and then when we go to the lower square the minimum distance from the outermost square becomes 1 ,SO,this square is made of (n-1) i.e (n-min_dist) and the same goes on for all the remaining inner squares.
I HOPE I EXPLAINED IT WELL
include
int main() {
}
I modified the way to represent absolute value f(x) = |x| for easier implementation.