Advertisement
dmilicev

every_array_element_is_multiplication_of_previous_and_next_103.c

Nov 9th, 2023
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.91 KB | None | 0 0
  1. /*
  2.  
  3.     every_array_element_is_multiplication_of_previous_and_next_103.c
  4.  
  5. https://www.w3resource.com/c-programming-exercises/array/c-array-exercise-103.php
  6.  
  7. 103. Write a program in C to update every array element
  8. with multiplication of previous and next numbers in array.
  9. Expected Output:
  10. The given array is:
  11. 1 2 3  4  5  6
  12. The new array is:
  13. 2 3 8 15 24 30
  14.  
  15.  
  16.     You can find all my C programs at Dragan Milicev's pastebin:
  17.  
  18.     https://pastebin.com/u/dmilicev
  19.  
  20. */
  21.  
  22. #include<stdio.h>
  23. #include <stdlib.h>
  24.  
  25. // Displays the array arr[n] of n integers with text before and after the array
  26. void print_array(char *text_before, int arr[], int n, char *text_after){
  27.     printf("%s", text_before);
  28.     for(int i=0;i<n;i++)
  29.         printf("%3d",arr[i]);
  30.     printf("%s", text_after);
  31. }
  32.  
  33. // Fills array arr[n] of n integers with ordinal numbers
  34. void fill_array(int arr[], int n){
  35.     for(int i=0;i<n;i++)
  36.         arr[i] = i+1;
  37. }
  38.  
  39. void newArrPrevNext_v1(int arr[], int n){
  40.     if (n <= 1)
  41.       return;
  42.  
  43.     int pre_elem = arr[0];
  44.     int cur_elem;
  45.  
  46. // new first element is multiplication of first and second element.
  47.     arr[0] = pre_elem * arr[1];
  48.  
  49. // every new inner element is multiplication of previous and next element.
  50.     for (int i=1; i<n-1; i++){
  51.         cur_elem = arr[i];
  52.         arr[i] = pre_elem * arr[i+1];
  53.         pre_elem = cur_elem;
  54.     }
  55.  
  56. // new last element is multiplication of previous and last element.
  57.     arr[n-1] = pre_elem * arr[n-1];
  58. }
  59.  
  60. void newArrPrevNext_v2(int *arr, int n){
  61.     if (n <= 1)
  62.       return;
  63.  
  64.     int *tmp = (int *)malloc(n*sizeof(int));    // temporary array of n integers
  65.  
  66.     if (tmp == NULL) {  // Check if the memory has been successfully allocated by malloc
  67.         printf("\n Memory not allocated.\n");
  68.         return;
  69.     }
  70.  
  71.     for (int i=0; i<n; i++)     // copy elements from arr in tmp
  72.         *(tmp+i) = *(arr+i);
  73.  
  74. // new first element is multiplication of first and second element.
  75.     tmp[0] = *(arr+0) * *(arr+1);
  76.  
  77. // every new inner element is multiplication of previous and next element.
  78.     for (int i=1; i<n-1; i++)
  79.         tmp[i] = *(arr+i-1) * *(arr+i+1);
  80.  
  81. // new last element is multiplication of previous and last element.
  82.     tmp[n-1] = *(arr+n-2) * *(arr+n-1);
  83.  
  84.     for (int i=0; i<n; i++)     // copy elements from arr in tmp
  85.         *(arr+i) = *(tmp+i);
  86.  
  87.     free(tmp);                  // keep it clean
  88. }
  89.  
  90. void newArrPrevNext_v3(int arr[], int n){
  91.     if (n <= 1)
  92.       return;
  93.  
  94.     int *tmp = (int *)malloc(n*sizeof(int));    // temporary array of n integers
  95.  
  96.     if (tmp == NULL) {  // Check if the memory has been successfully allocated by malloc
  97.         printf("\n Memory not allocated.\n");
  98.         return;
  99.     }
  100.  
  101.     for (int i=0; i<n; i++)     // copy elements from arr in tmp
  102.         *(tmp+i) = arr[i];
  103.  
  104. // new first element is multiplication of first and second element.
  105.     tmp[0] = arr[0] * arr[1];
  106.  
  107. // every new inner element is multiplication of previous and next element.
  108.     for (int i=1; i<n-1; i++)
  109.         tmp[i] = arr[i-1] * arr[i+1];
  110.  
  111. // new last element is multiplication of previous and last element.
  112.     tmp[n-1] = arr[n-2] * arr[n-1];
  113.  
  114.     for (int i=0; i<n; i++)     // copy elements from arr in tmp
  115.         *(arr+i) = *(tmp+i);
  116.  
  117.     free(tmp);                  // keep it clean
  118. }
  119.  
  120. void newArrPrevNext_v4(int arr[], int n){
  121.     if (n <= 1)
  122.       return;
  123.  
  124.     int tmp[n];                 // temporary array of n integers
  125.  
  126.     for (int i=0; i<n; i++)     // copy elements from arr in tmp
  127.         tmp[i] = arr[i];
  128.  
  129. // new first element is multiplication of first and second element.
  130.     tmp[0] = arr[0] * arr[1];
  131.  
  132. // every new inner element is multiplication of previous and next element.
  133.     for (int i=1; i<n-1; i++)
  134.         tmp[i] = arr[i-1] * arr[i+1];
  135.  
  136. // new last element is multiplication of previous and last element.
  137.     tmp[n-1] = arr[n-2] * arr[n-1];
  138.  
  139.     for (int i=0; i<n; i++)     // copy elements from arr in tmp
  140.         arr[i] = tmp[i];
  141. }
  142.  
  143.  
  144. int main(){
  145.     int arr[] = {1, 2, 3, 4, 5, 6};
  146.     int n = sizeof(arr)/sizeof(arr[0]);
  147.  
  148.     print_array("\n The given array is:  \n\n",arr,n,"\n");
  149.  
  150.     newArrPrevNext_v1(arr, n);
  151.     print_array("\n After function newArrPrevNext_v1(), new array is:  \n\n",arr,n,"\n");
  152.  
  153.     fill_array(arr,n);      // Let's fill the array elements again with ordinal numbers
  154.     newArrPrevNext_v2(arr, n);
  155.     print_array("\n After function newArrPrevNext_v2(), new array is:  \n\n",arr,n,"\n");
  156.  
  157.     fill_array(arr,n);      // Let's fill the array elements again with ordinal numbers
  158.     newArrPrevNext_v3(arr, n);
  159.     print_array("\n After function newArrPrevNext_v3(), new array is:  \n\n",arr,n,"\n");
  160.  
  161.     fill_array(arr,n);      // Let's fill the array elements again with ordinal numbers
  162.     newArrPrevNext_v4(arr, n);
  163.     print_array("\n After function newArrPrevNext_v4(), new array is:  \n\n",arr,n,"\n");
  164.  
  165.     return 0;
  166. }
  167.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement