Answer to Question #281051 in C for Syed

Question #281051

Given an array of integers,update every element with largest of previous and next elements with the following exception.



a)First element - check largest of first and second.



b)Last element - check largest of last and second.

1
Expert's answer
2021-12-20T09:57:41-0500
#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