Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- contiguous_subarrays_v1.c
- Find all subarrays of array of integers.
- Array:
- 1 2 3 4
- Contiguous subarrays:
- 1
- 12
- 123
- 1234
- 2
- 23
- 234
- 3
- 34
- 4
- There are 10 contiguous subarrays.
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- void ShowArray( char text[], int array[],int n )
- {
- int i;
- printf("%s", text);
- for(i=0;i<n;i++)
- printf("%2d", array[i]);
- printf("\n");
- }
- void print_contiguous_subarrays(int array[],int n)
- {
- int i, j, k, count=1;
- printf("\n Contiguous subarrays are : \n");
- for(i=0;i<n;i++)
- {
- count--;
- for(j=i;j<=n;j++)
- {
- for(k=i;k<j;k++)
- {
- printf("%2d", array[k]);
- }
- printf("\n");
- count++;
- }
- }
- printf("\n There are %d contiguous subarrays. \n", count-1);
- }
- int main(void)
- {
- int array[]={1,2,3,4,5};
- int NumberOfArrayElements;
- NumberOfArrayElements = sizeof(array)/sizeof(int);
- ShowArray("\n Array is : \n\n", array, NumberOfArrayElements);
- print_contiguous_subarrays(array, NumberOfArrayElements);
- return 0;
- }
Add Comment
Please, Sign In to add comment