Answer to Question #249124 in C++ for Talha

Question #249124
Write a C++ program to update every array element by multiplication of next and previous values of a given array of integers.
1
Expert's answer
2021-10-09T12:04:24-0400

Source: https://www.w3resource.com/cpp-exercises/array/cpp-array-exercise-11.php

#include<iostream>
using namespace std;
 
void replace_elements(int nums[], int n) {
    if (n <= 1) { return; }
    int prev_element = nums[0];
    nums[0] = nums[0] * nums[1];
    for (int i=1; i<n-1; i++) {
        int curr_element = nums[i];
        nums[i] = prev_element * nums[i+1];
        prev_element = curr_element;
    }
    nums[n-1] = prev_element * nums[n-1];
}
 
int main() {
    int nums[] = {0, 1, 3, 4, 5, 6, 7, 8, 10};
    int n = sizeof(nums)/sizeof(nums[0]);
   	cout << "Original array: ";
    for (int i=0; i < n; i++) 
    cout << nums[i] <<" ";
    replace_elements(nums,n);
    cout << "\nNew array elements: ";
    for (int i=0; i < n; i++) 
      cout << nums[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