Write a function named zeropadshift() that takes its arguments the following: 1-
ID: 3548306 • Letter: W
Question
Write a function named zeropadshift() that takes its arguments the following:
1-an array of integer values.
2- an integer o the array size.
3- 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:
1 2 3 4 5 6 7
If distance = 2, the array will be
0 0 1 2 3 4 5
Write a main() function to call the zeropadshift() function and display the array before and after shift in main()
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: 1 2 3 4 5 6 7 if distance = 2, the array will be 0 0 1 2 3 4 5 Write a main() function to call the zeropadshift() function and display the array before and after shift in main()Explanation / Answer
#include <iostream>
using namespace std;
void zeropadshift(int[], int, int);
void display(int[], int); //display array
int main()
{
int a[7] = {1,2,3,4,5,6,7}; //initialize array
display (a,7); //display before shifting
zeropadshift(a, 7, 2 ); //shifting
display (a,7); //display after shifting
return 0;
}
void zeropadshift(int array[], int size, int distance)
{
int temp=0;
if (distance > 0 && distance <= size)
{
for (int i = size - 1; i > distance - 1; i--)
{
array[i] = array[size - distance - temp - 1];
temp++;
}
for (int i = 0; i < distance; i++)
array[i] = 0;
}
}
void display(int array[], int size)
{
cout<<"The array is: ";
for (int i = 0; i < size; i++)
{
cout<<array[i]<<" ";
}
cout<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.