Write a function named \"shiftRight\" that takes as its arguments the following:
ID: 3568605 • Letter: W
Question
Write a function named "shiftRight" that takes as its arguments the following:
(1) an array of floating point values (double type);
(2) an integer that tells the number of cells in the array (its size/capacity).
The function should shift the contents of each cell one place to the right, except for the contents
of the last cell, which should be moved into the cell with index 0.
Thus, for example, if the array passed to the function looks like this:
0 1 2 3 4
5.8 | 2.6 | 9.1 | 3.4 | 7.0
then after the call to the function is made from main(), the array will have been changed so that it
looks like this:
0 1 2 3 4
7.0 | 5.8 | 2.6 | 9.1 | 3.4
IMPORTANT! The function should NOT return a value.
Explanation / Answer
#include<iostream>
using namespace std;
void printArr(double arr[], int size){
for(int i=0; i<size;i++){
cout << arr[i] << " ";
}
cout << " ";
}
void shiftRight(double arr[], int size){
double temp[size];
for(int i=0; i<size; i++){
if(i == size-1){
temp[0] = arr[i];
}else{
temp[i+1] = arr[i];
}
}
for(int i=0; i<size; i++){
arr[i] = temp[i];
}
}
int main(){
double arr[] = {5.8, 2.6, 9.1, 3.4, 7.0};
cout << "The initial array : ";
printArr(arr, 5);
cout << "Array after right shift: ";
shiftRight(arr, 5);
printArr(arr, 5);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.