 # v_pattern_with_explanation_v1.c

May 13th, 2020
382
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. /*
2.
3.     v_pattern_with_explanation_v1.c
4.
8.
9. -*-------*-
10. --*-----*--
11. ---*---*---
12. ----*-*----
13. -----*-----
14.
15. I will try to explain one way of solving this task.
16.
17. Observing the V pattern, we notice the following:
18. The pattern consists of n rows.
19. In each line, before the first * there are n1b space characters.
20. In each line, there are n2b space characters between the first * and the second *.
21. Each line ends with *, except last one.
22. After the second * there are n3b space characters (this is not necessary).
23.
24. We will write a function that has the following arguments:
25. n        - number of rows in the pattern,
26. ch       - the character by which the pattern is drawn,
27. space_ch - a character that draws a space.
28.
29. For all n pattern rows,
30. first we draw n1b space_ch characters (-) before first ch (*),
31. then we draw the first ch (*),
32. then we draw n2b space_ch characters (-),
33. then we draw second ch (*) (except in the last row),
34. and finally we draw n2b blank characters (-) (this is not necessary).
35.
36. For that we need three for loops.
37.
38. The first for loop is for rows and that is why we call its iterator r.
39. From the picture pattern we see that there are as many rows r as the argument n.
40. So in the first for loop r goes from 0 to n for all n rows.
41.
42. How to calculate n1b ?
43. n1b must depend on r.
44. From the picture of pattern it is obvious that there are r space_ch characters
45. in every row r.
46. n1b = r
47.
48. Next is the most tricky part of the code !!!
49. How to calculate n2b ?
50. n2b must depend on n and r.
51. From the picture of pattern we see that:
52.
53. n   r   n2b
54.
55. 5   0   7
56. 5   1   5
57. 5   2   3
58. 5   3   1
59. 5   4   0
60.
61. So from n and r we have to come up with a formula to get n2b.
62. This is where your creativity comes to the fore.
63. By observing, thinking and using the method of trial and error,
64. we finally come to a formula:
65. n2b = 2*(n-r)-3
66.
67. With this formula, in last row, case n=5 and r=4, result is -1 (not 0)
68. but even that satisfies the condition in for loop
69. for(c=0; c<2*(n-r)-3; c++) .
70.
71.
72.     You can find all my C programs at Dragan Milicev's pastebin:
73.
74.     https://pastebin.com/u/dmilicev
75.
76. */
77.
78. #include <stdio.h>
79.
80. // Draw V pattern, version 1 with n1b and n2b which are not necessary
81. void draw_v_pattern_v1( int n, char ch, char space_ch )
82. {
83.     int r,c;                        // represent row and column for loops iterators
84.     int n1b;                        // n1b is number of space_ch characters (-) before first ch (*)
85.     int n2b;                        // n2b is number of space_ch characters (-) between two (*) in row
86.
87.     for(r=0; r<n; r++)              // for all n pattern rows
88.     {
89.         n1b=r;
90.         for(c=0; c<n1b; c++)        // first we draw n1b space characters (-) before first ch (*)
91.             putchar(space_ch);      // character for space
92.
93.         putchar(ch);                // then we draw the first ch (*)
94.
95.         n2b = 2*(n-r)-3;            // this is the most tricky part of the code !!!
96.         for(c=0; c<n2b; c++)        // then we draw n2b space characters (-) between two (*) in row
97.             putchar(space_ch);      // character for space
98.
99.         if( r < n-1 )               // then in all rows except the last row
100.             putchar(ch);            // we draw the second ch (*)
101.
102.         putchar('\n');              // then we go to new pattern row
103.     }
104. }
105.
106. // Draw V pattern, version 2, without n1b and n2b
107. void draw_v_pattern_v2( int n, char ch, char space_ch )
108. {
109.     int r,c;                        // for loops iterators
110.
111.     for(r=0; r<n; r++)              // for all n pattern rows
112.     {
113.         for(c=0; c<r; c++)          // first we draw n1b space characters (-) before first ch (*)
114.             putchar(space_ch);      // character for space
115.
116.         putchar(ch);                // then we draw the first ch (*)
117.                                     // next line is the most tricky part of the code !!!
118.         for(c=0; c<2*(n-r)-3; c++)  // then we draw n2b space characters (-) between two (*) in row
119.             putchar(space_ch);      // character for space
120.
121.         if( r < n-1 )               // then in all rows except the last row
122.             putchar(ch);            // we draw the second ch (*)
123.
124.         putchar('\n');              // then we go to new pattern row
125.     }
126. }
127.
128. // Draw V pattern, version 3, code from Patrick Benedict Lirio
131. void draw_v_pattern_v3( int n, char ch, char space_ch )
132. {
133.     int r,c;                        // for loops iterators
134.     int x=1;                        // x coordinate for print
135.     int y=2*n-1;                    // y coordinate for print
136.
137.     for(r=0; r<n; r++)              // for n rows of pattern
138.     {
139.         for(c=0; c<2*n+1; c++)      // for all characters in one row r
140.             if( c==x || c==y )      // decide witch character to put
141.                 putchar(ch);        // character for drawing
142.             else
143.                 putchar(space_ch);  // character for space
144.         x++;                        // increase x coordinate
145.         y--;                        // decrease y coordinate
146.         putchar('\n');              // new pattern row
147.     }
148. }
149.
150. // Draw V pattern, version 4, code from Keshav Sahu
152. // https://pastebin.com/f5r47eQ6
153. void draw_v_pattern_v4( int n, char ch, char space_ch )
154. {
155.     int r,c;                        // for loops iterators
156.
157.     for(r=0; r<n; r++){             // for n rows of pattern
158.         for(c=0; c<2*n-1; c++){     // for all characters in one row r
159.             if(c == r || c == 2*n-r-2)  // decide witch character to put
160.                 putchar(ch);        // character for drawing
161.             else
162.                 putchar(space_ch);  // character for space
163.         }
164.         putchar('\n');              // new pattern row
165.     }
166. }
167.
168. // Draw V pattern, version 5, code from Keshav Sahu
170. // https://pastebin.com/f5r47eQ6
171. void draw_v_pattern_v5( int n, char ch, char space_ch )
172. {
173.     int r,c;                        // for loops iterators
174.
175.     for(r=0; r<n; r++){             // for n rows of pattern
176.         for(c=0; c<2*n; c++){       // for all characters in one row r
177.             if(c == r || c == 2*n-r-1)  // decide witch character to put
178.                 putchar(ch);        // character for drawing
179.             else
180.                 putchar(space_ch);  // character for space
181.         }
182.         putchar('\n');              // new pattern row
183.     }
184. }
185.
186.
187. int main(void)
188. {
189.     int n=5;                        // the number of rows that make up the pattern
190.     char ch='*';                    // the character with which we draw the pattern
191.     char space_ch='-';              // the character with which we draw a space
192.
193.     printf("\n draw_v_pattern_v1() \n\n");
194.     draw_v_pattern_v1(n, ch, space_ch);
195.     printf("\n");
196.
197.     printf("\n draw_v_pattern_v2() \n\n");
198.     draw_v_pattern_v2(n, ch, space_ch);
199.     printf("\n");
200.
201.     printf("\n draw_v_pattern_v3() \n\n");
202.     draw_v_pattern_v3(n, ch, space_ch);
203.     printf("\n");
204.
205.     printf("\n draw_v_pattern_v4() \n\n");
206.     draw_v_pattern_v4(n, ch, space_ch);
207.     printf("\n");
208.
209.     printf("\n draw_v_pattern_v5() \n\n");
210.     draw_v_pattern_v5(n, ch, space_ch);
211.     printf("\n");
212.
213.     return 0;
214.
215. } // main()
RAW Paste Data Copied