Answer to Question #229669 in C for swastica k g

Question #229669

Given an array of integers, update every element with the product of previous and next elements with following exceptions. i.       First element is replaced by multiplication of first and second.ii.      Last element is replaced by multiplication of last and second last.

1
Expert's answer
2021-08-26T02:26:12-0400
#include <stdio.h>
void modify(int arr[], int n)
{
    if (n <= 1)
      return;
    int prev = arr[0];
    arr[0] = arr[0] * arr[1];
    for (int i=1; i<n-1; i++)
    {
        int curr = arr[i];
        arr[i] = prev * arr[i+1];
        prev = curr;
    }
    arr[n-1] = prev * arr[n-1];
}
int main() {
    int arr[] = {2, 3, 4, 5, 6};
    int n = sizeof(arr)/sizeof(arr[0]);
    modify(arr, n);
    for (int i=0; i<n; i++)
      printf("%d ",arr[i]);
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS