# contiguous_subarrays_v2.c

Sep 1st, 2020 (edited)
154
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. /*
2.     contiguous_subarrays_v2.c
3.
4.     Find all subarrays of array of integers.
5.
6.     Array:
7.     1 2 3 4
8.
9.     number of elements n=4
10.
11.     Contiguous subarrays:
12.
13.     1       first=0     lines_in_goup=4     how_much=1
14.     12      first=0     lines_in_goup=4     how_much=2
15.     123     first=0     lines_in_goup=4     how_much=3
16.     1234    first=0     lines_in_goup=4     how_much=4
17.
18.     2       first=1     lines_in_goup=3     how_much=1
19.     23      first=1     lines_in_goup=3     how_much=2
20.     234     first=1     lines_in_goup=3     how_much=3
21.
22.     3       first=2     lines_in_goup=2     how_much=1
23.     34      first=2     lines_in_goup=2     how_much=2
24.
25.     4       first=3     lines_in_goup=1     how_much=1
26.
27.     first goes from 1 to n
28.     lines_in_goup in one group goes from 1 to n-first
29.     how_much goes from 1 to lines_in_goup
30.
31.     There are 10 contiguous subarrays.
32.
33.
34.     You can find all my C programs at Dragan Milicev's pastebin:
35.
36.     https://pastebin.com/u/dmilicev
37.
38. */
39.
40. #include <stdio.h>
41.
42. void ShowArray( char text[], int array[],int n )
43. {
44.     int i;
45.
46.     printf("%s", text);
47.
48.     for(i=0;i<n;i++)
49.         printf("%2d", array[i]);
50.
51.     printf("\n");
52. }
53.
54. void print_contiguous_subarrays(int array[],int n)
55. {
56.     int first, lines_in_group, how_much, count=0;
57.
58.     printf("\n Contiguous subarrays are : \n\n");
59.
60.     for(first=0;first<n;first++)// for all elements
61.     {                           // for (n-first) lines in group
62.         for(lines_in_group=0;lines_in_group<n-first;lines_in_group++)
63.         {                       // print from first element, how_much elements in one line
64.             for(how_much=0;how_much<=lines_in_group;how_much++)
65.             {
66.                 printf("%2d", array[first+how_much] );
67.             }
68.             count++;
69.             printf("\n");       // new line
70.         }
71.         printf("\n");           // to separate groups
72.     }
73.     printf("\n There are %d contiguous subarrays. \n", count);
74. }
75.
76.
77. int main(void)
78. {
79.     int array[]={1,2,3,4};
80.     int NumberOfArrayElements;
81.
82.     NumberOfArrayElements = sizeof(array)/sizeof(int);
83.
84.     ShowArray("\n Array is : \n\n", array, NumberOfArrayElements);
85.
86.     print_contiguous_subarrays(array, NumberOfArrayElements);
87.
88.
89.     return 0;
90. }
91.
RAW Paste Data Copied