Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- contiguous_subarrays_v2.c
- Find all subarrays of array of integers.
- Array:
- 1 2 3 4
- number of elements n=4
- Contiguous subarrays:
- 1 first=0 lines_in_goup=4 how_much=1
- 12 first=0 lines_in_goup=4 how_much=2
- 123 first=0 lines_in_goup=4 how_much=3
- 1234 first=0 lines_in_goup=4 how_much=4
- 2 first=1 lines_in_goup=3 how_much=1
- 23 first=1 lines_in_goup=3 how_much=2
- 234 first=1 lines_in_goup=3 how_much=3
- 3 first=2 lines_in_goup=2 how_much=1
- 34 first=2 lines_in_goup=2 how_much=2
- 4 first=3 lines_in_goup=1 how_much=1
- first goes from 1 to n
- lines_in_goup in one group goes from 1 to n-first
- how_much goes from 1 to lines_in_goup
- 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 first, lines_in_group, how_much, count=0;
- printf("\n Contiguous subarrays are : \n\n");
- for(first=0;first<n;first++)// for all elements
- { // for (n-first) lines in group
- for(lines_in_group=0;lines_in_group<n-first;lines_in_group++)
- { // print from first element, how_much elements in one line
- for(how_much=0;how_much<=lines_in_group;how_much++)
- {
- printf("%2d", array[first+how_much] );
- }
- count++;
- printf("\n"); // new line
- }
- printf("\n"); // to separate groups
- }
- printf("\n There are %d contiguous subarrays. \n", count);
- }
- int main(void)
- {
- int array[]={1,2,3,4};
- 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