Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- floyds_triangle_bottom_right_corner_v1.c
- Task:
- https://web.facebook.com/photo.php?fbid=1802749856529188&set=pcb.1570362636455914&type=3&theater&ifg=1
- https://www.tutorialspoint.com/c_standard_library/c_function_atoi.htm
- https://www.tutorialspoint.com/c_standard_library/c_function_isdigit.htm
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- #include <stdlib.h> // for atoi()
- #include <ctype.h> // for isdigit()
- void make_triangle_flipped( int arr[], int n )
- {
- int i=0; // index of array elements
- int r=0, c=0; // row and column of matrix
- int number=n*(n+1)/2, num_of_zeros=-1;
- int j; // iterator for loop
- printf("\n\t n = %d \n\n", n);
- for(r=0; r<n; r++) // for all rows of matrix
- {
- num_of_zeros++;
- for(j=0; j<num_of_zeros; j++) // put zeros
- arr[i++]=0;
- for(j=num_of_zeros; j<n; j++) // put numbers
- arr[i++]=number--;
- }
- }
- void print_array_as_square_matrix( int arr[], int n )
- {
- int i=0,r,c; // r is row, c is column
- printf("\n");
- for(r=0; r<n; r++)
- {
- for(c=0; c<n; c++)
- {
- printf("%4d", arr[i++]);
- }
- printf("\n\n"); // new line for new row
- }
- }
- int main(int argc, char *argv[])
- {
- int n;
- int arr[1000];
- if( argc != 2 ){ // if there is no only one command line argument
- printf( "\n Do not forget first to compile this program \n"
- "\n and then run exe file from command line \n"
- "\n exactly with quotes, like this: \n\n"
- "\n \"floyds_triangle_top_right_corner_v1.exe\" 6 \n\n" );
- printf("\n\n Press any key to exit. \n");
- _getch(); // to pause the screen
- return 1; // exit because there is no command line arguments
- }
- n = atoi( argv[1] );
- if( isdigit(n) || n<2 || n>20 )
- {
- printf("\n Argument of program must be an integer greater than 1 and less than 21 ! \n");
- printf("\n\n Press any key to exit. \n");
- _getch(); // to pause the screen
- return 1; // exit because argument is not valid integer
- }
- make_triangle_flipped( arr, n );
- print_array_as_square_matrix( arr, n );
- _getch(); // to pause the screen
- return 0;
- } // main()
RAW Paste Data
Copied