dmilicev

floyds_triangle_bottom_right_corner_v1.c

May 4th, 2020
178
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.20 KB | None
  1. /*
  2.  
  3.     floyds_triangle_bottom_right_corner_v1.c
  4.  
  5.     Task:
  6.     https://web.facebook.com/photo.php?fbid=1802749856529188&set=pcb.1570362636455914&type=3&theater&ifg=1
  7.  
  8.     https://www.tutorialspoint.com/c_standard_library/c_function_atoi.htm
  9.  
  10.     https://www.tutorialspoint.com/c_standard_library/c_function_isdigit.htm
  11.  
  12.  
  13.     You can find all my C programs at Dragan Milicev's pastebin:
  14.  
  15.     https://pastebin.com/u/dmilicev
  16.  
  17. */
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>     // for atoi()
  21. #include <ctype.h>      // for isdigit()
  22.  
  23.  
  24. void make_triangle_flipped( int arr[], int n )
  25. {
  26.     int i=0;                            // index of array elements
  27.     int r=0, c=0;                       // row and column of matrix
  28.     int number=n*(n+1)/2, num_of_zeros=-1;
  29.     int j;                              // iterator for loop
  30.  
  31.     printf("\n\t n = %d \n\n", n);
  32.  
  33.     for(r=0; r<n; r++)                  // for all rows of matrix
  34.     {
  35.         num_of_zeros++;
  36.         for(j=0; j<num_of_zeros; j++)   // put zeros
  37.             arr[i++]=0;
  38.  
  39.         for(j=num_of_zeros; j<n; j++)   // put numbers
  40.             arr[i++]=number--;
  41.     }
  42. }
  43.  
  44. void print_array_as_square_matrix( int arr[], int n )
  45. {
  46.     int i=0,r,c;                        // r is row, c is column
  47.  
  48.     printf("\n");
  49.  
  50.     for(r=0; r<n; r++)
  51.     {
  52.         for(c=0; c<n; c++)
  53.         {
  54.             printf("%4d", arr[i++]);
  55.         }
  56.         printf("\n\n");                 // new line for new row
  57.     }
  58. }
  59.  
  60.  
  61. int main(int argc, char *argv[])
  62. {
  63.     int n;
  64.     int arr[1000];
  65.  
  66.     if( argc != 2 ){    // if there is no only one command line argument
  67.  
  68.         printf( "\n Do not forget first to compile this program \n"
  69.                 "\n and then run exe file from command line \n"
  70.                 "\n exactly with quotes, like this: \n\n"
  71.                 "\n \"floyds_triangle_top_right_corner_v1.exe\" 6 \n\n" );
  72.  
  73.         printf("\n\n Press any key to exit. \n");
  74.         _getch();       // to pause the screen
  75.         return 1;       // exit because there is no command line arguments
  76.     }
  77.  
  78.     n = atoi( argv[1] );
  79.  
  80.     if( isdigit(n) || n<2 || n>20 )
  81.     {
  82.         printf("\n Argument of program must be an integer greater than 1 and less than 21 ! \n");
  83.         printf("\n\n Press any key to exit. \n");
  84.         _getch();       // to pause the screen
  85.         return 1;       // exit because argument is not valid integer
  86.     }
  87.  
  88.     make_triangle_flipped( arr, n );
  89.     print_array_as_square_matrix( arr, n );
  90.  
  91.     _getch();           // to pause the screen
  92.     return 0;
  93.  
  94. } // main()
RAW Paste Data Copied