Answer to Question #249125 in C++ for Talha

Question #249125
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-10T01:49:29-0400
#include<iostream>
using namespace std;
 
void subtitutesElements(int arr[], int size)
{
    if (size <= 1)
      return;
 
    int previous = arr[0];
    arr[0] = arr[0] * arr[1];
 
    for (int i=1; i<size-1; i++)
    {
        int current = arr[i];
 
        arr[i] = previous * arr[i+1];
 
        previous = current;
    }
 
    arr[size-1] = previous * arr[size-1];
    }
 
int main()
{
    int arr[] = {0, 1, 3, 4, 5, 6, 7, 8, 10};
    int size = sizeof(arr)/sizeof(arr[0]);
   	cout << "Initial Array array: ";
    for (int i=0; i < size; i++) 
    cout << arr[i] <<" ";
    subtitutesElements(arr,size);
    cout << "\nNew Array: ";
    for (int i=0; i < size; i++) 
      cout << 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