|
| 1 | + |
| 2 | +/******************************************************************************************************* |
| 3 | +/* ___ _ _ _ _ |
| 4 | +/* / _ \ | | (_)| | | | |
| 5 | +/* / /_\ \__ __ ___ | | __ _ ___ _ __ _ | |_ | |__ _ __ ___ ___ |
| 6 | +/* | _ |\ \ /\ / / / _ \| | / _` | / _ \ | '__|| || __|| '_ \ | '_ ` _ \ / __| |
| 7 | +/* | | | | \ V V / | __/| || (_| || (_) || | | || |_ | | | || | | | | |\__ \ |
| 8 | +/* \_| |_/ \_/\_/ \___||_| \__, | \___/ |_| |_| \__||_| |_||_| |_| |_||___/ |
| 9 | +/* __/ | |
| 10 | +/* |___/ |
| 11 | +/******************************************************************************************************/ |
| 12 | + |
| 13 | +/*********************************** |
| 14 | +/* 12_PatternChallenge |
| 15 | +/***********************************/ |
| 16 | + |
| 17 | +// Library imports |
| 18 | +#include <iostream> |
| 19 | +using namespace std; |
| 20 | + |
| 21 | +//Prototypes for the algorithms |
| 22 | +void printPattern_12(int); |
| 23 | + |
| 24 | +int main(){ |
| 25 | +// user input |
| 26 | +int userInput; |
| 27 | + |
| 28 | +cout<<"Enter number of lines."<<endl; |
| 29 | +cin>>userInput; |
| 30 | + |
| 31 | +printPattern_12(userInput); |
| 32 | +return 0; |
| 33 | +} |
| 34 | + |
| 35 | +/********************************************************** |
| 36 | +/* <printPattern_12> |
| 37 | +/* |
| 38 | +/* @description : Prints a pyramid pattern in increasing and then in a decreasing way after reaching the mid point. |
| 39 | +/* @param : Integer<input> |
| 40 | +/* @return : None, void |
| 41 | +/* @complexity : O(n) |
| 42 | +/* @explanation : Algorithm that iterates the first half of the pyramid with whitespaces upto 'mid' times, and the |
| 43 | +/* lower half of the pyramid is decremented again after middle(mid). First, we keep a track of the |
| 44 | +/* middle of the pyramid. we increment the numbers with every iteration and decrease the whitespaces. |
| 45 | +/* similar to '11_PatternChallenge', we add another loop to print a pyramid to the right to the existing. |
| 46 | +/* once the outer loop reaches mid, we use an if statement to check if 'i' is greater than mid. if yes, |
| 47 | +/* we increment the whitespaces and decrement the digits with every iteration, same as above but opposite. |
| 48 | +/* We also add another loop for the right bottom pyramid that iterates based on the 'input-i', giving us |
| 49 | +/* a rhombus like figure in the end. |
| 50 | +/* @author : Pranay Kothapalli @kotAPI |
| 51 | +/**********************************************************/ |
| 52 | +void printPattern_12(int input){ |
| 53 | +// Outer loop loops input number of times |
| 54 | +int mid; |
| 55 | +// Check if the user input is even or odd, choose a valid mid point depending on the input |
| 56 | +if(input%2==0){ |
| 57 | +// Mid/2 if the input is even. (if input is 8, mid is 4, 8/2 = 4). |
| 58 | +mid = input/2; |
| 59 | +} |
| 60 | +else{ |
| 61 | +// Mid/2+1 if the input is odd. (if input is 9, mid is 5, 9/2+1 = 5). |
| 62 | +mid = input/2 +1; |
| 63 | +} |
| 64 | + |
| 65 | +for(int i=1;i<input;i++){ |
| 66 | +// if i hasn't reached the mid point of the pyramid |
| 67 | +// execute this block |
| 68 | +if(i<=mid){ |
| 69 | +// decrementing white spaces on iteration depending on mid-i |
| 70 | +for(int j=mid-i;j>0;j--){ |
| 71 | +cout<<" "; |
| 72 | +} |
| 73 | +// incrementing digits with every iteration depending on i |
| 74 | +for(int k=1;k<=i;k++){ |
| 75 | +cout<<k; |
| 76 | +} |
| 77 | +// loop to print the upper right pyramid based on i; |
| 78 | +for(int l=i-1;l>0;l--){ |
| 79 | +cout<<l; |
| 80 | +} |
| 81 | +} |
| 82 | +// if i is greater than mid. |
| 83 | +// execute this code block |
| 84 | +else{ |
| 85 | +// Incrementing white spaces with every iteration depending on i-mid |
| 86 | +for(int j=1;j<=i-mid;j++){ |
| 87 | +cout<<" "; |
| 88 | +} |
| 89 | +// decrementing the digits with every iteration depending on input-i. forming a reverse pyramid. |
| 90 | +for(int k=1;k<=input-i;k++){ |
| 91 | +cout<<k; |
| 92 | +} |
| 93 | +// Decrementing the digits in the lower half right of the pyramid, forming an inverse right pyramid. |
| 94 | +for(int l=input-i-1;l>0;l--){ |
| 95 | +cout<<l; |
| 96 | +} |
| 97 | +} |
| 98 | +// Add a newline once the inner loops have finished executing. |
| 99 | +cout<<endl; |
| 100 | +} |
| 101 | +} |
0 commit comments