Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- number_triangle_pattern_alternate.c
- https://www.facebook.com/groups/3092811364081112/permalink/7605244639504406/
- Solution from Dan Mathew Gamale :
- https://www.facebook.com/groups/3092811364081112/user/100005184546962/
- numbers indexes matrix
- 1 00 00 01 02 03 04
- 2 9 10 11 10 11 12 13 14
- 3 8 10 20 21 22 20 21 22 23 24
- 4 7 11 14 30 31 32 33 30 31 32 33 34
- 5 6 12 13 15 40 41 42 43 44 40 41 42 43 44
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include<stdio.h>
- #include <string.h> // for memset()
- // print triangle of numbers
- void number_triangle_pattern_from_top(int n){
- int r,c, num=1; // r - row, c - column, num - number
- int arr[n][n]; // n - number of rows and columns
- // memset() is used to fill a block of memory with a particular value.
- memset(arr,0,sizeof(arr)); // set all elements to 0
- /*
- for(r=0; r<n; r++) // set all elements to 0, another way
- for(c=0; c<n; c++)
- arr[r][c]=0;
- */
- // set all elements to num
- for(c=0; c<n; c++) // column
- for (r=0; r<n-c; r++) // row
- if (c%2) // odd column
- arr[n-r-1][c] = num++;
- else
- arr[r+c][c] = num++;
- // print triangle
- for(r=0; r<n; r++) { // row
- for(c=0; c<n; c++) // column
- if (arr[r][c] != 0)
- printf("%3d",arr[r][c]);
- printf("\n");
- }
- printf("\n");
- }
- int main(void){
- int n; // n - number of rows and columns
- for(n=3; n<=6; n++){ // a few examples
- printf("\n n = %d \n\n",n);
- number_triangle_pattern_from_top(n);
- }
- return 0;
- } // main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement