Question

Write a function replaceNums that replaces each element except the first and last by the larger of its two neighbors. Your function should take two parameters, an integer array and the array length (in this order). Your function should not return or print anything. Examples int myArray[2] = {1, 5); → {1.9 int myArray 1 3(1); → {1} int myArray[3] = {1, 3, 5); → {1, 5.9 int myArray[3] = {5, 3, 1); → {5, 5, 1} int myArray[5] = {1, 2, 3, 4, 5); → {1, 3, 4, 5, 5} int myArray[5] = {5, 4, 3, 2, 1); → {5, 5, 4, 3, 1} Note: as you attempt to replace elements of the array, keep in mind that once an elements value is overwritten, you wont be able to have access to the old value. Think of a way to remember the original values in the array. Good luck!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>

using namespace std;

int main()

{

int n;

cout<<"Enter size of Array : ";

cin>>n;

int arr[n];

int res[n];

cout<<"Enter Array Elements : ";

for(int i=0;i<n;i++)

cin>>arr[i];

res[0]=arr[0];

res[n-1]=arr[n-1];

for(int i=1;i<n-1;i++)

{

if(arr[i-1]>arr[i+1])

res[i]=arr[i-1];

else

res[i]=arr[i+1];

}

cout<<"Output Array"<<endl;

for(int i=0;i<n;i++)

cout<<res[i]<<" ";

}

Add a comment
Know the answer?
Add Answer to:
Write a function replaceNums that replaces each element except the first and last by the larger...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Homework Question Write a void function called transformArray that takes two parameters - a reference to...

    Homework Question Write a void function called transformArray that takes two parameters - a reference to a pointer to a dynamically allocated array of ints, and the size of that array.  The pointer is passed by referencebecause you want to change the value of the pointer. The function should dynamically allocate an array that is twice as long, filled with the values from the original array followed by each of those values times 2. For example, if the array that was...

  • Question 14; Write a C function named isSymmetric, the prototype of which is given below, that...

    Question 14; Write a C function named isSymmetric, the prototype of which is given below, that returns 1 if the elements of an array of integers named myArray of size n are symmetric around the middle. If the array elements are not symmetric, the function should return 0. Both the array and its size are specified as parameters. (10 marks) Clue: Array myArray of size n is symmetric if myArray[0] is equal to myArray[n-1], myArray[1] is equal to myArray[n-2], and...

  • (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element...

    (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to the element 1 of the new array. Element 1 of the argument array should be copied to element 2 of the new array, and so forth....

  • 1) Write a public static method named printArray, that takes two arguments. The first argument is...

    1) Write a public static method named printArray, that takes two arguments. The first argument is an Array of int and the second argument is a String. The method should print out a list of the values in the array, each separated by the value of the second argument. For example, given the following Array declaration and instantiation: int[] myArray = {1, 22, 333, 400, 5005, 9}; printArray(myArray, ", ") will print out 1, 22, 333, 400, 5005, 9 printArray(myArray,...

  • C Programming 2) Write a function that removes the redundant values from the array and replaces...

    C Programming 2) Write a function that removes the redundant values from the array and replaces them with -1 Example: A = {4, 1, 2, 3, 4, 4, 5, 6, 2, 7} The array becomes: A = {4, 1, 2, 3, -1, -1, 6, -1, 7}

  • 1. (40 pts) Consider the following function. Complete the function that does the following: (a) Take...

    1. (40 pts) Consider the following function. Complete the function that does the following: (a) Take three arguments, int array, int as the length of the array, and int *max. (b) Find the maximum value and the minimum value in the array. (c) The pointer max points to the maximum value. (d) Return the pointer that points to the minimum value max)( findMinAndMax(int al l, int length, int int maxValue a[0], minValue al0 int int maxldx 0, minldx 0; for...

  • Must be C++ 11 Write a function named insertinArray that takes the following parameters: list: an...

    Must be C++ 11 Write a function named insertinArray that takes the following parameters: list: an integer array index: index at which to insert the new item numitems: number of items currently in the array arrayCapacity: capacity of the array newVal: value to insert into the array If the array is at capacity then the function should return-1. Otherwise, insert newVal at index and return O for success int insertInArray(int listl 1, int index, int numItems, int arrayCapacity, int newVal)...

  • Write a function that gets an array as a parameter, sorts it in a descending fashion...

    Write a function that gets an array as a parameter, sorts it in a descending fashion and returns the number of elements that did not change position in the sort (they were originally in the right position). int sortDESC(int* myArray, int size) if array = { 2, 3, 1, 0, -1} after calling the function the array should become {-1, 0, 1, 2 , 3 } and the function should return 1 (only one element has not changed place in...

  • Write a function named stats, that takes an array and the number of elements in the...

    Write a function named stats, that takes an array and the number of elements in the array as arguments. It must compute and print the minimum value in the array, maximum value in the array and the average value of all the values inside the array. Your function MUST be named stats Your function has two parameters in the following order: an array of type double The number of elements in the array, type int Your function should NOT return...

  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT