Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- every_array_element_is_multiplication_of_previous_and_next_103.c
- https://www.w3resource.com/c-programming-exercises/array/c-array-exercise-103.php
- 103. Write a program in C to update every array element
- with multiplication of previous and next numbers in array.
- Expected Output:
- The given array is:
- 1 2 3 4 5 6
- The new array is:
- 2 3 8 15 24 30
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include<stdio.h>
- #include <stdlib.h>
- // Displays the array arr[n] of n integers with text before and after the array
- void print_array(char *text_before, int arr[], int n, char *text_after){
- printf("%s", text_before);
- for(int i=0;i<n;i++)
- printf("%3d",arr[i]);
- printf("%s", text_after);
- }
- // Fills array arr[n] of n integers with ordinal numbers
- void fill_array(int arr[], int n){
- for(int i=0;i<n;i++)
- arr[i] = i+1;
- }
- void newArrPrevNext_v1(int arr[], int n){
- if (n <= 1)
- return;
- int pre_elem = arr[0];
- int cur_elem;
- // new first element is multiplication of first and second element.
- arr[0] = pre_elem * arr[1];
- // every new inner element is multiplication of previous and next element.
- for (int i=1; i<n-1; i++){
- cur_elem = arr[i];
- arr[i] = pre_elem * arr[i+1];
- pre_elem = cur_elem;
- }
- // new last element is multiplication of previous and last element.
- arr[n-1] = pre_elem * arr[n-1];
- }
- void newArrPrevNext_v2(int *arr, int n){
- if (n <= 1)
- return;
- int *tmp = (int *)malloc(n*sizeof(int)); // temporary array of n integers
- if (tmp == NULL) { // Check if the memory has been successfully allocated by malloc
- printf("\n Memory not allocated.\n");
- return;
- }
- for (int i=0; i<n; i++) // copy elements from arr in tmp
- *(tmp+i) = *(arr+i);
- // new first element is multiplication of first and second element.
- tmp[0] = *(arr+0) * *(arr+1);
- // every new inner element is multiplication of previous and next element.
- for (int i=1; i<n-1; i++)
- tmp[i] = *(arr+i-1) * *(arr+i+1);
- // new last element is multiplication of previous and last element.
- tmp[n-1] = *(arr+n-2) * *(arr+n-1);
- for (int i=0; i<n; i++) // copy elements from arr in tmp
- *(arr+i) = *(tmp+i);
- free(tmp); // keep it clean
- }
- void newArrPrevNext_v3(int arr[], int n){
- if (n <= 1)
- return;
- int *tmp = (int *)malloc(n*sizeof(int)); // temporary array of n integers
- if (tmp == NULL) { // Check if the memory has been successfully allocated by malloc
- printf("\n Memory not allocated.\n");
- return;
- }
- for (int i=0; i<n; i++) // copy elements from arr in tmp
- *(tmp+i) = arr[i];
- // new first element is multiplication of first and second element.
- tmp[0] = arr[0] * arr[1];
- // every new inner element is multiplication of previous and next element.
- for (int i=1; i<n-1; i++)
- tmp[i] = arr[i-1] * arr[i+1];
- // new last element is multiplication of previous and last element.
- tmp[n-1] = arr[n-2] * arr[n-1];
- for (int i=0; i<n; i++) // copy elements from arr in tmp
- *(arr+i) = *(tmp+i);
- free(tmp); // keep it clean
- }
- void newArrPrevNext_v4(int arr[], int n){
- if (n <= 1)
- return;
- int tmp[n]; // temporary array of n integers
- for (int i=0; i<n; i++) // copy elements from arr in tmp
- tmp[i] = arr[i];
- // new first element is multiplication of first and second element.
- tmp[0] = arr[0] * arr[1];
- // every new inner element is multiplication of previous and next element.
- for (int i=1; i<n-1; i++)
- tmp[i] = arr[i-1] * arr[i+1];
- // new last element is multiplication of previous and last element.
- tmp[n-1] = arr[n-2] * arr[n-1];
- for (int i=0; i<n; i++) // copy elements from arr in tmp
- arr[i] = tmp[i];
- }
- int main(){
- int arr[] = {1, 2, 3, 4, 5, 6};
- int n = sizeof(arr)/sizeof(arr[0]);
- print_array("\n The given array is: \n\n",arr,n,"\n");
- newArrPrevNext_v1(arr, n);
- print_array("\n After function newArrPrevNext_v1(), new array is: \n\n",arr,n,"\n");
- fill_array(arr,n); // Let's fill the array elements again with ordinal numbers
- newArrPrevNext_v2(arr, n);
- print_array("\n After function newArrPrevNext_v2(), new array is: \n\n",arr,n,"\n");
- fill_array(arr,n); // Let's fill the array elements again with ordinal numbers
- newArrPrevNext_v3(arr, n);
- print_array("\n After function newArrPrevNext_v3(), new array is: \n\n",arr,n,"\n");
- fill_array(arr,n); // Let's fill the array elements again with ordinal numbers
- newArrPrevNext_v4(arr, n);
- print_array("\n After function newArrPrevNext_v4(), new array is: \n\n",arr,n,"\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement