C++ Write a function named zeropadshift () that takes its arguments the followin
ID: 3695348 • Letter: C
Question
C++
Write a function named zeropadshift () that takes its arguments the following: an array of integer values. an integer o the array size. an integer distance that tells how many cells to shift by. Distance > 0, shift left, pad zeros in the end of the array. Distance = 0, no shift. For example: if the array passes to the function looks like the following: If distance = 2, the array will be Write a main () function to call the zeropadshift () function and display the array before and after shift in main ()Explanation / Answer
Answer
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void zeropadshift(int arr[],int size,int dis)
{
cout<<" array before shift is : ";
for(int i=0;i<size;i++) //shows array
{
cout<<arr[i]<<" ";
}
for(int i=size-1;i>=0;i--) //pads zeros to array
{
if(i>=dis)
{
arr[i]=arr[i-dis];
}
else
{
arr[i]=0;
}
}
cout<<" array After shift is : ";
for(int i=0;i<size;i++) //shows array
{
cout<<arr[i]<<" ";
}
}
int main()
{
int siz;
cout<<" Enter array size : ";
cin>>siz;
int ran_arr[siz],distance;
srand(time(0));
for(int i=0;i<siz;i++)
{
int ran=rand()%100; //genrates random number
ran_arr[i]=ran; //assigning random value to array
}
cout<<" Enter distance : ";
cin>>distance;
zeropadshift(ran_arr,siz,distance); //calling function
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.