This is for a class project. I am having trouble "pulling out" the last element in my array, shifting all the elements back then inserting that last value in the front. Then also displaying the array after the shift. here's my code.
int values[5]; //integer array of 5, the values that are to be shifted
...
//display un-shifted array
for(int i = 0; i < 5; i++)
std::cout << values[i] << std::endl;
std::cout << std::endl << "Shifting array..." << std::endl << std::endl;
//begin shift***
int lastElement = values[5]; //save the last value of the array
for(int i = 0; i < 4; i++) //shift all values from 0 to 4 over
values[i + 1] = values[i];
values[0] = lastElement; //replace the first element with the saved last element
//end shift***
//display shifted array
for(int i = 0; i < 5; i++)
std::cout << values[i] << std::endl;
Wrote this code in a hurry, sorry if I failed at something.
It would be good if you define some constant in your class that will denote the number of elements of the array. Also I would make a separate method that displays the array.
Function movevalues is invalid. Apart from accessing non-existent elements of the array it tries to shift the array left instead of right.