Respuesta :
The function in cpp language is given.
void shift(int n[], int num)
{
// integer variable to keep count of remaining elements
int count=10;
for(int j=0; j<10; j++)
{
// key element is compared to each element in the array
if(n[j] == num)
{
n[j] = n[j+1];
// decremented after each occurrence of key element in the array
count--;
}
}
std::cout<<count<<" elements are remaining out of 10 elements "<<std::endl;
}
1. The method accepts two parameters, one array and one element.
2. The integer array, arr, and integer element, key, passed to this function are declared outside all the methods.
3. The integer variable, count, is initialized to the size of the integer array, i.e., count contains the number of elements present in the array, arr.
4. Inside the for loop, the element is compared with each element in the array. If they match, the integer variable, count, is decremented each time the element occurs in the array.
5. After the loop completes, the number of remaining elements are displayed.
This function is implemented inside a cpp program as shown below.
#include <stdio.h>
#include <iostream>
// integer array declared and initialized
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 2, 8, 2};
// element to be removed from the array
int key = 2;
void shift(int n[], int num)
{
// integer variable to keep count of remaining elements
int count=10;
for(int j=0; j<10; j++)
{
if(n[j] == num)
{
n[j] = n[j+1];
count--;
}
}
std::cout<<count<<" elements are remaining out of 10 elements "<<std::endl;
}
int main()
{
// function called having array and key as parameters
shift(arr, key);
return 0;
}
The program begins when the array and the element are declared and initialized at the beginning of the program.
Inside the main() method, the method shift() is called by passing two parameters to this method.
The main() method has return type of integer. Hence, this program ends with a return statement.