dmilicev

c_arrays_with_pointers_with_dynamic_memory_allocation.c

May 3rd, 2021 (edited)
600
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     c_arrays_with_pointers_with_dynamic_memory_allocation.c
  3.  
  4.     Updated: 2021.05.04  ,  by Dragan Milicev, https://www.facebook.com/dmilicev
  5.  
  6.     In C language string is actually a one-dimensional array of characters
  7.     which is terminated by a null character '\0'.
  8.  
  9. // Function to print array, n is number of elements in array arr[]
  10. // text[] describes the shown array arr[]
  11. void printArray( char *text, int *arr, int n )
  12.  
  13. // Fills array arr with n integers starting from first
  14. // and update pointer to number of elements pn with n.
  15. void fill_array_with_integers( int *arr, int *pn, int n, int first )
  16.  
  17. // Refills (reset) two arrays.
  18. // Fills array arr1 with n1 integers starting from first1 and update pn1,
  19. // Fills array arr2 with n2 integers starting from first2 and update pn2.
  20. void fill_two_arrays_with_integers( int *arr1, int *pn1, int n1, int first1,
  21.                                     int *arr2, int *pn2, int n2, int first2 )
  22.  
  23. // Refills (reset) array and print it.
  24. // Fills array arr1 with n1 integers starting from first1 and update pn1.
  25. void fill_and_print_array_with_integers( int *arr1, int *pn1, int n1, int first1 )
  26.  
  27. // Refills (reset) two arrays and print them.
  28. // Fills array arr1 with n1 integers starting from first1 and update pn1,
  29. // Fills array arr2 with n2 integers starting from first2 and update pn2.
  30. void fill_and_print_two_arrays_with_integers( int *arr1, int *pn1, int n1, int first1,
  31.                                               int *arr2, int *pn2, int n2, int first2 )
  32.  
  33. // Copies the array arr2 to the array arr1 and updates pointer n1.
  34. // Function itself extends arr1 array to have enough space for arr2 array.
  35. // Returns the copied array arr1.
  36. int *arrcpy( int *arr1, int *n1, int *arr2, int n2 )
  37.  
  38. // Concatenates array arr2 to array arr1 and updates pointer n1.
  39. // Function itself extends array arr1 to have enough space for array arr2.
  40. // Returns the concatenated array arr1.
  41. int *arrcat( int *arr1, int *n1, int *arr2, int n2 )
  42.  
  43. // Inserts a new element num into the array of n elements,
  44. // in place of the index pos_index. Returns array arr.
  45. int *insert_num_into_array(int *arr, int *n, int num, int pos_index)
  46.  
  47. // Inserts array arr2 with n2 elements into array arr1 with n1 elements,
  48. // in place of the index pos_index. Returns array arr.
  49. int *insert_arr2_into_arr1(int *arr1, int *n1, int *arr2, int n2, int pos_index)
  50.  
  51. // If num exists in array arr of n integers,
  52. // returns index of num in array arr,
  53. // otherwise returns -1.
  54. int arr_find_num(int *arr, int n, int num)
  55.  
  56. // If subarray arr2 with n2 integers exists in array array arr1 with n1 integers,
  57. // returns the index of the beginning of the subarray arr2 in array arr1,
  58. // otherwise returns -1.
  59. int arr_find_subarr(int *arr1, int n1, int *arr2, int n2)
  60.  
  61. // In array arr with n elements deletes one element at position pos_index
  62. int *del_array_element_at_pos_index(int *arr, int *n, int pos_index)
  63.  
  64. // In array arr1 with n1 elements deletes a subarray arr2 that has n2 elements
  65. int *del_subarray_into_array(int *arr1, int *n1, int *arr2, int n2)
  66.  
  67. // Deletes odd elements in array arr with n elements
  68. int *del_odd_array_elements(int *arr, int *n)
  69.  
  70. // Deletes even elements in array arr with n elements
  71. int *del_even_array_elements(int *arr, int *n)
  72.  
  73. // Deletes all elements in array arr with n elements
  74. int *del_all_array_elements(int *arr, int *n)
  75.  
  76. // The auxiliary sort function replaces xp and yp with each other
  77. void swap(int *xp, int *yp)
  78.  
  79. // Bubble sort sorts array arr of n elements in ascending order
  80. void sort_array_ascending(int *arr, int n)
  81.  
  82. // Bubble sort sorts array arr of n elements in descending order
  83. void sort_array_descending(int *arr, int n)
  84.  
  85. // Sums the n elements of the array and returns that sum
  86. float get_array_sum(int *arr, int n)
  87.  
  88. // Returns mean value of array arr with n elements.
  89. // Mean and average are the same.
  90. // Mean of an array = (sum of all elements) / (number of elements)
  91. float get_array_mean(int *arr, int n)
  92.  
  93. // Returns median of n array arr elements.
  94. // Median of a sorted array of size n is defined as
  95. // the middle element when n is odd and
  96. // average of middle two elements when n is even.
  97. // Since the array is not sorted here, we sort the array first, then apply above formula.
  98. float get_array_median(int *arr, int n)
  99.  
  100.  
  101.     You can find all my C programs at Dragan Milicev's pastebin:
  102.  
  103.     https://pastebin.com/u/dmilicev
  104.  
  105. */
  106.  
  107. #include <stdio.h>
  108. #include <stdlib.h>     // for malloc() and realloc()
  109. #include <string.h>
  110.  
  111. // Function to print array, n is number of elements in array arr[]
  112. // text[] describes the shown array arr[]
  113. void printArray( char *text, int *arr, int n ){
  114.     int i;
  115.     printf("%s", text );
  116.     for(i=0; i<n; i++)
  117.         printf("%3d", *(arr+i) );
  118.     printf("\n");
  119. }
  120.  
  121. // Fills array arr with n integers starting from first
  122. // and update pointer to number of elements pn with n.
  123. void fill_array_with_integers( int *arr, int *pn, int n, int first ){
  124.     int i;
  125.     // reallocate sufficient space
  126.     arr = (int *)realloc( arr, (size_t)n * sizeof(int) );
  127.     if( arr == NULL ) {
  128.         fprintf(stderr, "\n\n arr realloc() error! \n\n");
  129.         exit(EXIT_FAILURE);
  130.     }
  131.     *pn = n;
  132.     for(i=0; i<*pn; i++) // fill array arr with n integers from first
  133.         *(arr+i) = first+i;
  134. } // fill_array_with_integers()
  135.  
  136. // Refills (reset) two arrays.
  137. // Fills array arr1 with n1 integers starting from first1 and update pn1,
  138. // Fills array arr2 with n2 integers starting from first2 and update pn2.
  139. void fill_two_arrays_with_integers( int *arr1, int *pn1, int n1, int first1,
  140.                                     int *arr2, int *pn2, int n2, int first2 ) {
  141.     fill_array_with_integers(arr1,pn1,n1,first1);
  142.     fill_array_with_integers(arr2,pn2,n2,first2);
  143. } // fill_array_with_integers()
  144.  
  145. // Refills (reset) array and print it.
  146. // Fills array arr1 with n1 integers starting from first1 and update pn1.
  147. void fill_and_print_array_with_integers( int *arr1, int *pn1, int n1, int first1 ) {
  148.     fill_array_with_integers(arr1,pn1,n1,first1);
  149.     printArray("\n Before, arr1 = ", arr1, *pn1);
  150. } // fill_array_with_integers()
  151.  
  152. // Refills (reset) two arrays and print them.
  153. // Fills array arr1 with n1 integers starting from first1 and update pn1,
  154. // Fills array arr2 with n2 integers starting from first2 and update pn2.
  155. void fill_and_print_two_arrays_with_integers( int *arr1, int *pn1, int n1, int first1,
  156.                                               int *arr2, int *pn2, int n2, int first2 ) {
  157.     fill_array_with_integers(arr1,pn1,n1,first1);
  158.     fill_array_with_integers(arr2,pn2,n2,first2);
  159.     printArray("\n Before, arr1 = ", arr1, *pn1);
  160.     printArray("\n Before, arr2 = ", arr2, *pn2);
  161. } // fill_array_with_integers()
  162.  
  163. // Copies the array arr2 to the array arr1 and updates pointer n1.
  164. // Function itself extends arr1 array to have enough space for arr2 array.
  165. // Returns the copied array arr1.
  166. int *arrcpy( int *arr1, int *n1, int *arr2, int n2 ){
  167.     int i;
  168.     // allocate sufficient space, reallocating memory size of arr1 for arr2
  169.     arr1 = (int *)realloc( arr1, (size_t)n2*sizeof(int) );
  170.     if( arr1 == NULL ) {
  171.         fprintf(stderr, "\n\n arr1 realloc() error! \n\n");
  172.         exit(EXIT_FAILURE);
  173.     }
  174.     for(i=0; i<n2; i++)                 // copying...
  175.         *(arr1+i) = *(arr2+i);
  176.     *n1 = n2;                           // update number of elements
  177.     return arr1;
  178. } // arrcpy()
  179.  
  180. // Concatenates array arr2 to array arr1 and updates pointer n1.
  181. // Function itself extends array arr1 to have enough space for array arr2.
  182. // Returns the concatenated array arr1.
  183. int *arrcat( int *arr1, int *n1, int *arr2, int n2 ){
  184.     int i,j;
  185.     // allocate sufficient space, reallocating memory size of arr1 for arr2
  186.     arr1 = (int *)realloc( arr1, (size_t)(*n1+n2)*sizeof(int) );
  187.     if( arr1 == NULL ) {
  188.         fprintf(stderr, "\n\n arr1 realloc() error! \n\n");
  189.         exit(EXIT_FAILURE);
  190.     }
  191.     for(i=(*n1),j=0; i<(*n1+n2); i++,j++)   // adding arr2 to the end of arr1
  192.         *(arr1+i) = *(arr2+j);
  193.     *n1 += n2;                              // update number of elements
  194.     return arr1;
  195. } // arrcat()
  196.  
  197. // Inserts a new element num into the array of n elements,
  198. // in place of the index pos_index. Returns array arr.
  199. int *insert_num_into_array(int *arr, int *n, int num, int pos_index){
  200.     int i;
  201.     // check that pos_index is valid
  202.     if( pos_index < 0  ||  pos_index > ( (*n)+1 ) ) {
  203.         fprintf(stderr, "\n\n Function insert_num_into_array(), pos_index = %d not valid ! \n\n", pos_index);
  204.         return arr;
  205.     }
  206.     // allocate sufficient space, reallocating memory size of array arr for one new element
  207.     arr = (int *)realloc( arr, (size_t)( (*n)+1) * sizeof(int) );
  208.     if( arr == NULL ) {
  209.         fprintf(stderr, "\n\n arr realloc() error! \n\n");
  210.         exit(EXIT_FAILURE);
  211.     }
  212.     // we increase the number of array elements by one because we insert one new element
  213.     (*n)++;
  214.     i = (*n) - 1;                   // i is the index at the end of the extended array
  215.     while( i > pos_index ) {        // from the end of the arr to the pos_index
  216.         *(arr+i) = *(arr+i-1);      // we move the elements of the array one place to the right
  217.         i--;                        // the next left element
  218.     }
  219.     *(arr+i) = num;                 // now i = pos_index and here we insert a new element num
  220.     return arr;
  221. } // insert_num_into_array()
  222.  
  223. // Inserts array arr2 with n2 elements into array arr1 with n1 elements,
  224. // in place of the index pos_index. Returns array arr.
  225. int *insert_arr2_into_arr1(int *arr1, int *n1, int *arr2, int n2, int pos_index) {
  226.     int i, j;
  227.     // check that pos_index is valid
  228.     if( pos_index < 0  ||  pos_index > ( (*n1)+1 ) ) {
  229.         fprintf(stderr, "\n\n Function insert_arr2_into_arr1(), pos_index = %d not valid ! \n\n", pos_index);
  230.         return arr1;
  231.     }
  232.     // allocate sufficient space, reallocating memory size of array arr1 for new array arr2
  233.     arr1 = (int *)realloc( arr1, (size_t)( (*n1) + n2 ) * sizeof(int) );
  234.     if( arr1 == NULL ) {
  235.         fprintf(stderr, "\n\n arr1 realloc() error! \n\n");
  236.         exit(EXIT_FAILURE);
  237.     }
  238.     // we increase the number of arr1 elements by n2 because we insert new array arr2
  239.     (*n1) += n2;
  240.     i = (*n1) - 1;                  // i is the index at the end of the extended array arr1
  241.     while( i > pos_index+n2-1 ) {   // from the end of the arr1 to the pos_index+n2
  242.         *(arr1+i) = *(arr1+i-n2);   // we move the elements of the array n2 places to the right
  243.         i--;                        // the next left element
  244.     }
  245.     j = n2 - 1;                     // j is index at the end of the arr2 with n2 elements
  246.     while( j >= 0 ) {               // from the end of the arr2 to the left
  247.         *(arr1+i) = *(arr2+j);      // we copy the elements of the arr2 to arr1
  248.         i--;                        // the next left element
  249.         j--;                        // the next left element
  250.     }
  251.     return arr1;
  252. } // insert_arr2_into_arr1()
  253.  
  254. // If num exists in array arr of n integers,
  255. // returns index of num in array arr,
  256. // otherwise returns -1.
  257. int arr_find_num(int *arr, int n, int num){
  258.     int i;
  259.     for(i=0; i<n; i++)          // for all n elements in array arr
  260.         if( *(arr+i) == num )
  261.             return i;           // returns index of num in array arr
  262.     return (-1);                // num not found in array arr
  263. } // arr_find_num()
  264.  
  265.  
  266. // If subarray arr2 with n2 integers exists in array array arr1 with n1 integers,
  267. // returns the index of the beginning of the subarray arr2 in array arr1,
  268. // otherwise returns -1.
  269. int arr_find_subarr(int *arr1, int n1, int *arr2, int n2){
  270.     int i=0, j=0;
  271.     // for all elements of arr1
  272.     while( i < n1 ){            // if first element of subarray arr2 match, check the whole arr1
  273.         while( i<n1 && j<n2 && *(arr1+i) == *(arr2+j) ){
  274.             i++;                // next element of array arr1
  275.             j++;                // next element of subarray arr2
  276.         }
  277.         if( j == n2 )           // if complete subarray arr2 match, return starting index
  278.             return ( i - j );   // returns the index of the beginning of the subarray arr2 in array arr1
  279.         i++;                    // next element of array arr1
  280.     }
  281.     return (-1);                // subarray arr2 not found in array arr1
  282. } // arr_find_subarr()
  283.  
  284. // In array arr with n elements deletes one element at position pos_index
  285. int *del_array_element_at_pos_index(int *arr, int *n, int pos_index) {
  286.     int i=0;                        // index of the array arr
  287.     // check that pos_index is valid
  288.     if( pos_index < 0  ||  pos_index > ( (*n)-1 ) ) {
  289.         fprintf(stderr, "\n\n Function del_array_element_at_pos_index(), pos_index = %d not valid ! \n\n", pos_index);
  290.         return arr;
  291.     }
  292.     while( i < pos_index )          // from the begin of the arr to the pos_index
  293.         i++;                        // the next element
  294.  
  295.     while( i < *n ) {               // from the pos_index to the end of the arr
  296.         *(arr+i) = *(arr+i+1);      // we move the elements of the array one place to the left
  297.         i++;                        // the next element
  298.     }
  299.     // we decrease the number of array elements by one because we deleted one element on pos_index
  300.     (*n)--;
  301.     // reallocate sufficient space, reallocating memory size of array arr for one element less
  302.     arr = (int *)realloc( arr, (size_t)( *n ) * sizeof(int) );
  303.     if( arr == NULL ) {
  304.         fprintf(stderr, "\n\n arr realloc() error! \n\n");
  305.         exit(EXIT_FAILURE);
  306.     }
  307.     return arr;
  308. } // del_array_element_at_pos_index
  309.  
  310. // In array arr1 with n1 elements deletes a subarray arr2 that has n2 elements
  311. int *del_subarray_into_array(int *arr1, int *n1, int *arr2, int n2){
  312.     int i=0, pos_index;
  313.     // check if subarray arr2 exists in array arr1
  314.     if( pos_index = arr_find_subarr(arr1, *n1, arr2, n2) == -1 )
  315.         return arr1;                // in array arr1 there is no subarray arr2
  316.     while( i <= pos_index )         // from the begin of the arr to the pos_index
  317.         i++;                        // the next element
  318.     i += n2;                        // we skip the n2 elements of the array arr1
  319.     while( i < *n1 ) {              // from the pos_index to the end of the arr1
  320.         *(arr1+i) = *(arr1+n2+i);   // we move the elements of the array arr1 n2 places to the left
  321.         i++;                        // the next element
  322.     }
  323.     // we decrease the number of array elements by n2 because we deleted n2 elements on pos_index
  324.     (*n1) -= n2;
  325.     // reallocate sufficient space, reallocating memory size of array arr1 for n2 elements less
  326.     arr1 = (int *)realloc( arr1, (size_t)( *n1 ) * sizeof(int) );
  327.     if( arr1 == NULL ) {
  328.         fprintf(stderr, "\n\n arr1 realloc() error! \n\n");
  329.         exit(EXIT_FAILURE);
  330.     }
  331.     return arr1;
  332. } // del_subarray_into_array()
  333.  
  334. // Deletes odd elements in array arr with n elements
  335. int *del_odd_array_elements(int *arr, int *n){
  336.     int i=0, j=0, n_odd=0;
  337.     while( i < (*n) ) {             // for all n elements of array arr
  338.         if( *(arr+i)%2 !=  0 )      // if that element is odd,
  339.             n_odd++;                // count odd elements
  340.         else{
  341.             *(arr+j) = *(arr+i);    // put it in the array
  342.             j++;                    // prepare index for the next array element
  343.         }
  344.         i++;                        // the next element of array arr
  345.     }
  346.     (*n) -= n_odd;                  // we decrease the number of array elements by n_odd odd elements
  347.     // reallocate sufficient space, reallocating memory size of array arr for n_odd elements less
  348.     arr = (int *)realloc( arr, (size_t)( *n ) * sizeof(int) );
  349.     if( arr == NULL ) {
  350.         fprintf(stderr, "\n\n arr realloc() error! \n\n");
  351.         exit(EXIT_FAILURE);
  352.     }
  353.     return arr;
  354. } // del_odd_array_elements()
  355.  
  356. // Deletes even elements in array arr with n elements
  357. int *del_even_array_elements(int *arr, int *n){
  358.     int i=0, j=0, n_even=0;
  359.     while( i < (*n) ) {             // for all n elements of array arr
  360.         if( *(arr+i)%2 ==  0 )      // if that element is even,
  361.             n_even++;                // count even elements
  362.         else{
  363.             *(arr+j) = *(arr+i);    // put it in the array
  364.             j++;                    // prepare index for the next array element
  365.         }
  366.         i++;                        // the next element of array arr
  367.     }
  368.     (*n) -= n_even;                 // we decrease the number of array elements by n_even even elements
  369.     // reallocate sufficient space, reallocating memory size of array arr for n_even elements less
  370.     arr = (int *)realloc( arr, (size_t)( *n ) * sizeof(int) );
  371.     if( arr == NULL ) {
  372.         fprintf(stderr, "\n\n arr realloc() error! \n\n");
  373.         exit(EXIT_FAILURE);
  374.     }
  375.     return arr;
  376. } // del_even_array_elements()
  377.  
  378. // Deletes all elements in array arr with n elements
  379. int *del_all_array_elements(int *arr, int *n){
  380.     (*n) = 0;                       // there i no elements in array arr
  381.     // reallocate sufficient space, reallocating memory size of array arr for 1 element (can't be 0 elements)
  382.     arr = (int *)realloc( arr, (size_t)( 1 ) * sizeof(int) );
  383.     if( arr == NULL ) {
  384.         fprintf(stderr, "\n\n arr realloc() error! \n\n");
  385.         exit(EXIT_FAILURE);
  386.     }
  387.     return arr;
  388. } // del_even_array_elements()
  389.  
  390. // The auxiliary sort function replaces xp and yp with each other
  391. void swap(int *xp, int *yp){
  392.     int temp = *xp;
  393.     *xp = *yp;
  394.     *yp = temp;
  395. } // swap()
  396.  
  397. // Bubble sort sorts array arr of n elements in ascending order
  398. void sort_array_ascending(int *arr, int n){
  399.     int i, j;
  400.     for(i=0; i<n-1; i++)
  401.        for(j=0; j<n-1-i; j++)       // Last i elements are already in place
  402.            if( arr[j] > arr[j+1] )  // the only difference is in the sign '<' or '>'
  403.               swap( &arr[j], &arr[j+1] );
  404. } // sort_array_ascending()
  405.  
  406. // Bubble sort sorts array arr of n elements in descending order
  407. void sort_array_descending(int *arr, int n){
  408.     int i, j;
  409.     for(i=0; i<n-1; i++)
  410.        for(j=0; j<n-1-i; j++)       // Last i elements are already in place
  411.            if( arr[j] < arr[j+1] )  // the only difference is in the sign '<' or '>'
  412.               swap( &arr[j], &arr[j+1] );
  413. } // sort_array_descending()
  414.  
  415. // Sums the n elements of the array and returns that sum
  416. float get_array_sum(int *arr, int n){
  417.     int i;
  418.     float sum=0;
  419.     for(i=0; i<n; i++)
  420.         sum += (*arr+i);
  421.     return sum;
  422. } // get_array_sum()
  423.  
  424. // Returns mean value of array arr with n elements.
  425. // Mean and average are the same.
  426. // Mean of an array = (sum of all elements) / (number of elements)
  427. float get_array_mean(int *arr, int n){
  428.     int i;
  429.     float sum=0;
  430.     for(i=0; i<n; i++)
  431.         sum += (*arr+i);
  432.     return (sum/n);
  433. } // get_array_mean()
  434.  
  435. // Returns median of n array arr elements.
  436. // Median of a sorted array of size n is defined as
  437. // the middle element when n is odd and
  438. // average of middle two elements when n is even.
  439. // Since the array is not sorted here, we sort the array first, then apply above formula.
  440. float get_array_median(int *arr, int n){
  441.     // First we sort the array
  442.     sort_array_ascending(arr, n);
  443.     // check for even case
  444.     if ( n%2 != 0 )
  445.         return (double)arr[ n/2 ];
  446.      return (double)( arr[ (n-1)/2 ] + arr[ n/2] ) / 2.0;
  447. } // get_array_median()
  448.  
  449.  
  450. int main(void){
  451.     int *arr1, *arr2;               // pointers to two arrays of integers arr1 and arr2
  452.     int n1, n2;                     // and their numbers of elements
  453.  
  454.     arr1 = (int*)malloc( n1*sizeof(int) );      // allocate sufficient space
  455.     if( arr1 == NULL ) {
  456.         fprintf(stderr, "\n\n arr1 malloc() error! \n\n");
  457.         exit(EXIT_FAILURE);
  458.     }
  459.  
  460.     arr2 = (int*)malloc( n2*sizeof(int) );      // allocate sufficient space
  461.     if( arr2 == NULL ) {
  462.         fprintf(stderr, "\n\n arr2 malloc() error! \n\n");
  463.         exit(EXIT_FAILURE);
  464.     }
  465.  
  466. printf("\n\n -------  arrcpy(arr1, &n1, arr2, n2) -------------------- \n\n");
  467.  
  468.     fill_and_print_two_arrays_with_integers( arr1, &n1, 5, 1,
  469.                                              arr2, &n2, 4, 6 );
  470.     arrcpy( arr1, &n1, arr2, n2 );
  471.  
  472.     printf("\n        arrcpy(arr1, &n1, arr2, n2) \n");
  473.     printArray("\n After, arr1 = ", arr1, n1);
  474.     printArray("\n After, arr2 = ", arr2, n2);
  475.  
  476. printf("\n\n -------  arrcat(arr1, &n1, arr2, n2) -------------------- \n\n");
  477.  
  478.     fill_and_print_two_arrays_with_integers( arr1, &n1, 5, 1,
  479.                                              arr2, &n2, 4, 6 );
  480.     arrcat( arr1, &n1, arr2, n2 );
  481.  
  482.     printf("\n        arrcat(arr1, &n1, arr2, n2) \n");
  483.     printArray("\n After, arr1 = ", arr1, n1);
  484.     printArray("\n After, arr2 = ", arr2, n2);
  485.  
  486. printf("\n\n -------  arr_find_num(arr1, n1, 3) ---------------------- \n\n");
  487.  
  488.     fill_two_arrays_with_integers( arr1, &n1, 5, 1,
  489.                                    arr2, &n2, 4, 6 );
  490.  
  491.     printArray("\n        arr1 = ", arr1, n1);
  492.     printf("\n Number %d has index %d \n", 3, arr_find_num(arr1, n1, 3) );
  493.  
  494. printf("\n\n -------  arr_find_subarr(arr1, n1, arr2, n2) ------------ \n\n");
  495.  
  496.     fill_and_print_two_arrays_with_integers( arr1, &n1, 9, 1,
  497.                                              arr2, &n2, 3, 5 );
  498.  
  499.     printf("\n In arr1 subarray arr2 begin at index %d \n", arr_find_subarr(arr1, 9, arr2, 3) );
  500.  
  501. printf("\n\n ---  insert_num_into_array(arr1, &n1, num, pos_index) --- \n\n");
  502.  
  503.     fill_and_print_array_with_integers( arr1, &n1, 5, 1 );
  504.  
  505.     insert_num_into_array(arr1, &n1, 9, 2);
  506.  
  507.     printArray("\n After insert_num_into_array(arr1, &n1, 9, 2) \n\n", arr1, n1);
  508.  
  509. printf("\n\n --insert_arr2_into_arr1(arr1, &n1, arr2, n2, pos_index)-- \n\n");
  510.  
  511.     fill_and_print_two_arrays_with_integers( arr1, &n1, 5, 1,
  512.                                              arr2, &n2, 3, 6 );
  513.     insert_arr2_into_arr1(arr1, &n1, arr2, n2, 2);
  514.  
  515.     printArray("\n After insert_arr2_into_arr1(arr1, &n1, arr2, n2, 2) \n\n", arr1, n1);
  516.  
  517. printf("\n\n -- del_array_element_at_pos_index(arr1, &n1, pos_index)-- \n\n");
  518.  
  519.     fill_and_print_array_with_integers( arr1, &n1, 9, 1 );
  520.  
  521.     del_array_element_at_pos_index(arr1, &n1, 3);
  522.  
  523.     printArray("\n After del_array_element_at_pos_index(arr1, &n1, 3) \n\n", arr1, n1);
  524.  
  525. printf("\n\n ----- del_subarray_into_array(arr1, &n1, arr2, n2 ) ----- \n\n");
  526.  
  527.     fill_and_print_array_with_integers( arr1, &n1, 9, 1 );
  528.  
  529.     fill_and_print_array_with_integers( arr2, &n2, 3, 5 );
  530.  
  531.     del_subarray_into_array(arr1, &n1, arr2, n2);
  532.  
  533.     printArray("\n After del_subarray_into_array(arr1, &n1, arr2, n2) \n\n", arr1, n1);
  534.  
  535. printf("\n\n ----------- del_odd_array_elements(arr1, &n1) ----------- \n\n");
  536.  
  537.     fill_and_print_array_with_integers( arr1, &n1, 9, 1 );
  538.  
  539.     del_odd_array_elements(arr1, &n1);
  540.  
  541.     printArray("\n After del_odd_array_elements(arr1, &n1) \n\n", arr1, n1);
  542.  
  543. printf("\n\n ----------- del_even_array_elements(arr1, &n1) ---------- \n\n");
  544.  
  545.     fill_and_print_array_with_integers( arr1, &n1, 9, 1 );
  546.  
  547.     del_even_array_elements(arr1, &n1);
  548.  
  549.     printArray("\n After del_even_array_elements(arr1, &n1) \n\n", arr1, n1);
  550.  
  551. printf("\n\n ----------- del_all_array_elements(arr1, &n1) ----------- \n\n");
  552.  
  553.     fill_and_print_array_with_integers( arr1, &n1, 9, 1 );
  554.  
  555.     del_all_array_elements(arr1, &n1);
  556.  
  557.     printArray("\n After del_all_array_elements(arr1, &n1) \n\n", arr1, n1);
  558.  
  559. printf("\n\n ----------- sort_array_descending(arr1, &n1) ------------ \n\n");
  560.  
  561.     fill_and_print_array_with_integers( arr1, &n1, 9, 1 );
  562.  
  563.     sort_array_descending(arr1, n1);
  564.  
  565.     printArray("\n After sort_array_descending(arr1, n1) \n\n", arr1, n1);
  566.  
  567.     sort_array_ascending(arr1, n1);
  568.  
  569.     printArray("\n After sort_array_ascending(arr1, n1) \n\n", arr1, n1);
  570.  
  571. printf("\n\n ----------- get_array_sum(arr1, n1) --------------------- \n\n");
  572.  
  573.     fill_and_print_array_with_integers( arr1, &n1, 9, 1 );
  574.  
  575.     printf("\n get_array_sum(arr1, n1) = %f \n\n", get_array_sum(arr1, n1) );
  576.  
  577. printf("\n\n ----------- get_array_mean(arr1, n1) -------------------- \n\n");
  578.  
  579.     fill_and_print_array_with_integers( arr1, &n1, 9, 1 );
  580.  
  581.     printf("\n get_array_mean(arr1, n1) = %f \n\n", get_array_mean(arr1, n1) );
  582.  
  583. printf("\n\n ----------- get_array_median(arr1, n1) ------------------ \n\n");
  584.  
  585.     fill_and_print_array_with_integers( arr1, &n1, 9, 1 );
  586.  
  587.     printf("\n get_array_median(arr1, n1) = %f \n\n", get_array_median(arr1, n1) );
  588.  
  589.     fill_and_print_array_with_integers( arr1, &n1, 8, 1 );
  590.  
  591.     printf("\n get_array_median(arr1, n1) = %f \n\n", get_array_median(arr1, n1) );
  592.  
  593. printf("\n\n --------------------------------------------------------- \n\n");
  594.  
  595.     free(arr1);     // freeing up memory because we used malloc()
  596.     free(arr2);
  597.     return 0;
  598. } // main()
  599.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×