Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- v_pattern_with_explanation_v1.c
- Task:
- https://web.facebook.com/photo.php?fbid=2607667832670872&set=p.2607667832670872&type=3&theater
- https://web.facebook.com/groups/c.programing/permalink/1577186559106855/?comment_id=1577227462436098
- -*-------*-
- --*-----*--
- ---*---*---
- ----*-*----
- -----*-----
- I will try to explain one way of solving this task.
- Observing the V pattern, we notice the following:
- The pattern consists of n rows.
- In each line, before the first * there are n1b space characters.
- In each line, there are n2b space characters between the first * and the second *.
- Each line ends with *, except last one.
- After the second * there are n3b space characters (this is not necessary).
- We will write a function that has the following arguments:
- n - number of rows in the pattern,
- ch - the character by which the pattern is drawn,
- space_ch - a character that draws a space.
- For all n pattern rows,
- first we draw n1b space_ch characters (-) before first ch (*),
- then we draw the first ch (*),
- then we draw n2b space_ch characters (-),
- then we draw second ch (*) (except in the last row),
- and finally we draw n2b blank characters (-) (this is not necessary).
- For that we need three for loops.
- The first for loop is for rows and that is why we call its iterator r.
- From the picture pattern we see that there are as many rows r as the argument n.
- So in the first for loop r goes from 0 to n for all n rows.
- How to calculate n1b ?
- n1b must depend on r.
- From the picture of pattern it is obvious that there are r space_ch characters
- in every row r.
- n1b = r
- Next is the most tricky part of the code !!!
- How to calculate n2b ?
- n2b must depend on n and r.
- From the picture of pattern we see that:
- n r n2b
- 5 0 7
- 5 1 5
- 5 2 3
- 5 3 1
- 5 4 0
- So from n and r we have to come up with a formula to get n2b.
- This is where your creativity comes to the fore.
- By observing, thinking and using the method of trial and error,
- we finally come to a formula:
- n2b = 2*(n-r)-3
- With this formula, in last row, case n=5 and r=4, result is -1 (not 0)
- but even that satisfies the condition in for loop
- for(c=0; c<2*(n-r)-3; c++) .
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- // Draw V pattern, version 1 with n1b and n2b which are not necessary
- void draw_v_pattern_v1( int n, char ch, char space_ch )
- {
- int r,c; // represent row and column for loops iterators
- int n1b; // n1b is number of space_ch characters (-) before first ch (*)
- int n2b; // n2b is number of space_ch characters (-) between two (*) in row
- for(r=0; r<n; r++) // for all n pattern rows
- {
- n1b=r;
- for(c=0; c<n1b; c++) // first we draw n1b space characters (-) before first ch (*)
- putchar(space_ch); // character for space
- putchar(ch); // then we draw the first ch (*)
- n2b = 2*(n-r)-3; // this is the most tricky part of the code !!!
- for(c=0; c<n2b; c++) // then we draw n2b space characters (-) between two (*) in row
- putchar(space_ch); // character for space
- if( r < n-1 ) // then in all rows except the last row
- putchar(ch); // we draw the second ch (*)
- putchar('\n'); // then we go to new pattern row
- }
- }
- // Draw V pattern, version 2, without n1b and n2b
- void draw_v_pattern_v2( int n, char ch, char space_ch )
- {
- int r,c; // for loops iterators
- for(r=0; r<n; r++) // for all n pattern rows
- {
- for(c=0; c<r; c++) // first we draw n1b space characters (-) before first ch (*)
- putchar(space_ch); // character for space
- putchar(ch); // then we draw the first ch (*)
- // next line is the most tricky part of the code !!!
- for(c=0; c<2*(n-r)-3; c++) // then we draw n2b space characters (-) between two (*) in row
- putchar(space_ch); // character for space
- if( r < n-1 ) // then in all rows except the last row
- putchar(ch); // we draw the second ch (*)
- putchar('\n'); // then we go to new pattern row
- }
- }
- // Draw V pattern, version 3, code from Patrick Benedict Lirio
- // https://web.facebook.com/pbenedict.lirio
- // https://web.facebook.com/photo.php?fbid=1602215383269843&set=p.1602215383269843&type=3&theater
- void draw_v_pattern_v3( int n, char ch, char space_ch )
- {
- int r,c; // for loops iterators
- int x=1; // x coordinate for print
- int y=2*n-1; // y coordinate for print
- for(r=0; r<n; r++) // for n rows of pattern
- {
- for(c=0; c<2*n+1; c++) // for all characters in one row r
- if( c==x || c==y ) // decide witch character to put
- putchar(ch); // character for drawing
- else
- putchar(space_ch); // character for space
- x++; // increase x coordinate
- y--; // decrease y coordinate
- putchar('\n'); // new pattern row
- }
- }
- // Draw V pattern, version 4, code from Keshav Sahu
- // https://web.facebook.com/profile.php?id=100042709719960
- // https://pastebin.com/f5r47eQ6
- void draw_v_pattern_v4( int n, char ch, char space_ch )
- {
- int r,c; // for loops iterators
- for(r=0; r<n; r++){ // for n rows of pattern
- for(c=0; c<2*n-1; c++){ // for all characters in one row r
- if(c == r || c == 2*n-r-2) // decide witch character to put
- putchar(ch); // character for drawing
- else
- putchar(space_ch); // character for space
- }
- putchar('\n'); // new pattern row
- }
- }
- // Draw V pattern, version 5, code from Keshav Sahu
- // https://web.facebook.com/profile.php?id=100042709719960
- // https://pastebin.com/f5r47eQ6
- void draw_v_pattern_v5( int n, char ch, char space_ch )
- {
- int r,c; // for loops iterators
- for(r=0; r<n; r++){ // for n rows of pattern
- for(c=0; c<2*n; c++){ // for all characters in one row r
- if(c == r || c == 2*n-r-1) // decide witch character to put
- putchar(ch); // character for drawing
- else
- putchar(space_ch); // character for space
- }
- putchar('\n'); // new pattern row
- }
- }
- int main(void)
- {
- int n=5; // the number of rows that make up the pattern
- char ch='*'; // the character with which we draw the pattern
- char space_ch='-'; // the character with which we draw a space
- printf("\n draw_v_pattern_v1() \n\n");
- draw_v_pattern_v1(n, ch, space_ch);
- printf("\n");
- printf("\n draw_v_pattern_v2() \n\n");
- draw_v_pattern_v2(n, ch, space_ch);
- printf("\n");
- printf("\n draw_v_pattern_v3() \n\n");
- draw_v_pattern_v3(n, ch, space_ch);
- printf("\n");
- printf("\n draw_v_pattern_v4() \n\n");
- draw_v_pattern_v4(n, ch, space_ch);
- printf("\n");
- printf("\n draw_v_pattern_v5() \n\n");
- draw_v_pattern_v5(n, ch, space_ch);
- printf("\n");
- return 0;
- } // main()
Add Comment
Please, Sign In to add comment