Advertisement
dmilicev

pattern_challenge_v1.c

May 23rd, 2020
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.71 KB | None | 0 0
  1. /*
  2.  
  3.     pattern_challenge_v1.c
  4.  
  5.     Task
  6.     https://web.facebook.com/photo.php?fbid=2194948743984354&set=gm.1587094954782682&type=3&theater
  7.  
  8.     For n=7
  9.  
  10.     1 * A 2 * B 3
  11.     * A 2 * B 3 *
  12.     A 2 * B 3 * C
  13.     2 * B 3 * C 4
  14.     * B 3 * C 4 *
  15.     B 3 * C 4 * D
  16.     3 * C 4 * D 5
  17.  
  18.     pattern string is
  19.     1 * A  2 * B  3 * C  4 * D  5 * E  6 * F  7 * G  8 * H  9 * I 1 * A  2 * B  3 * C ...
  20.  
  21.  
  22.     You can find all my C programs at Dragan Milicev's pastebin:
  23.  
  24.     https://pastebin.com/u/dmilicev
  25.  
  26. */
  27.  
  28. #include <stdio.h>
  29. #include <stdlib.h>     // for exit()
  30.  
  31. // pattern string is
  32. // 1 * A  2 * B  3 * C  4 * D  5 * E  6 * F  7 * G  8 * H  9 * I 1 * A  2 * B  3 * C ...
  33. void make_pattern_string( char pattern_string[], int n )
  34. {
  35.     int i, j=0, k=0;
  36.  
  37.     for(i=0; i<3*n; i++)
  38.     {
  39.         pattern_string[k++] = '1' + j;
  40.         pattern_string[k++] = '*';
  41.         pattern_string[k++] = 'A' + j;
  42.  
  43.         if( j < 8 )
  44.             j++;
  45.         else
  46.             j = 0;
  47.     }
  48. /*
  49.     printf("\n Pattern string is: \n\n");
  50.  
  51.     for(i=0; i<k; i++)
  52.         printf("%c ", pattern_string[i] );
  53.  
  54.     printf("\n\n");
  55. */
  56. }
  57.  
  58.  
  59. void print_pattern( char pattern_string[], int n )
  60. {
  61.     int i, j, k, first_character = 0;
  62.  
  63.     printf("\n Pattern is: \n\n");
  64.  
  65.     for(i=0; i<n; i++)
  66.     {
  67.         k = 0;
  68.  
  69.         for(j=0; j<n; j++)
  70.             printf("%c ", pattern_string[first_character + k++] );
  71.  
  72.         printf("\n");
  73.  
  74.         if( first_character < 26 )
  75.             first_character++;
  76.         else
  77.             first_character = 0;
  78.     }
  79. }
  80.  
  81.  
  82. int main(void)
  83. {
  84.     char pattern_string[100];
  85.     int n;
  86.  
  87.     printf("\n Enter number between 1 and 40 , n = ");
  88.     scanf("%d",&n);
  89.  
  90.     if( n<1 || n>40 )
  91.     {
  92.         printf("\n Number n must be between 1 and 40 ! \n");
  93.         exit(0);
  94.     }
  95.  
  96.     make_pattern_string(pattern_string, 9);
  97.  
  98.     print_pattern(pattern_string, n);
  99.  
  100.  
  101.     return 0;
  102.  
  103. } // main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement