Moving values of an array to the right
Nov 21, 2016 at 11:21pm UTC
I'm having some issues with making a function that moves every value in the array to the right.
the order is coming out messed up, and the last value is coming out as a very strange value.
What's supposed to happen is every value is moved to the right and the first value is replaced by the former last value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
void shift(int values[], int size) {
for (int i = 0; i < size; i++) {
int temp;
temp = values[size - 1];
values[i] = values[i + 1];
values[0] = temp;
cout << values[i] << setw(4);
}
cout << endl;
}
int main()
{
cout << "Random 10 index array" << endl;
const int CAP = 10;
int numbers[CAP];
srand(time(0));
int i;
for (i = 0; i < CAP; i++) {
int rng = rand() % 100 + 1;
numbers[i] = rng;
cout << numbers[i] << setw(4);
}
cout << "shifting all elements to the right: " << endl;
shift(numbers, CAP);
system("pause" );
return 0;
}
Last edited on Nov 21, 2016 at 11:29pm UTC
Nov 21, 2016 at 11:24pm UTC
values[i] = values[i + 1];
What happens when i == size - 1?
Also, I'd recommend that you move
1 2 3
int temp;
temp = values[size - 1];
values[0] = temp;
outside the for-loop so you don't unnecessarily perform operations on it.
Where is your main()?
Line 18: infinite recursion?
Nov 21, 2016 at 11:27pm UTC
when I try i < size - 1
, the array prints 9 values instead of 10.
and I edited the code because I messed up the copy paste.
Nov 21, 2016 at 11:35pm UTC
when I try i < size - 1
, the array prints 9 values instead of 10.
and I edited the code because I messed up the copy paste.
Print the array in another function. Don't try to do two things in a single function.
Nov 21, 2016 at 11:40pm UTC
Print the array in another function. Don't try to do two things in a single function.
I don't think it really matters where I print it.
I chose to print it there because I'm printing multiple functions using the array.
In any case I'm asking about fixing the shift function.
Last edited on Nov 21, 2016 at 11:40pm UTC
Nov 22, 2016 at 11:47am UTC
Hello Chamat,
Untested, but the more I look at the shift function the more I come to the conclusion that it actually shift to the left not the right. This is what I did to fix the problem:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
void printArray(int values[], const int size)
{
for (size_t i = 0; i < size; i++)
{
std::cout << std::setw(4) << values[i];
}
std::cout << std::endl;
}
void shift(int values[], const int size)
{
int tempArray[10]{ values[size - 1] }; // Sets tempArray[0] to the last eleent of values. As a bonus everything else is set to zero.
for (int i = 1; i < size; i++) // Starts at 1 because 0 is taken care of.
{
tempArray[i] = values[i - 1];
}
values = tempArray;
printArray(tempArray, size); // Printed here because numbers in main did not change.
}
It works except "numbers" in main does not get changed. Have not figured out that part yet because I could not pass the array by reference. That is why I printed the "tempArray" from within the "shift" function. Maybe someone else will have an answer for passing the array.
Hope that helps,
Andy
Nov 22, 2016 at 3:05pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
template <class T> void shift( T values[], int size)
{
T temp;
temp = values[size - 1];
for ( int i = size - 1; i > 0; i-- ) values[i] = values[i-1];
values[0] = temp;
}
template <class T> void print( T values[], int size, int width )
{
for ( int i = 0; i < size; i++ ) cout << setw( width ) << values[i];
cout << endl;
}
int main()
{
const int CAP = 10;
cout << "Random " << CAP << " index array" << endl;
int numbers[CAP];
srand(time(0));
for ( int i = 0; i < CAP; i++ ) numbers[i] = rand() % 100 + 1;
cout << "Initial numbers: \n" ; print<int >( numbers, CAP, 4 );
shift<int >( numbers, CAP );
cout << "Shifted numbers: \n" ; print<int >( numbers, CAP, 4 );
system("pause" );
return 0;
}
Last edited on Nov 22, 2016 at 3:08pm UTC
Topic archived. No new replies allowed.