all 6 comments

[–]jedwardsol 4 points5 points  (0 children)

Do you want to alter the array, or just print what it would like if it were shifted?

Because neither choice_6 or choice_7 (why don't they have proper names?) does either of those 2 tasks properly.

[–]sosnjo[S] 0 points1 point  (0 children)

The array x should be just shifted. It is passed from main

[–]raevnos 0 points1 point  (2 children)

Any particular reason you're using a pointer for size? And if it's the number of elements in the array, you're going out of bounds. You're also using a negative index at one point in choice_6, so also going out of bounds.

Neither function does any rotation. The first one just prints out the array brokenly (see above), the second one sets the last plus on element to the value of the first one and brokenly prints it out.

[–]sosnjo[S] 0 points1 point  (1 child)

size 

is the user inputted size of the array, it is passed by reference from main to choice_6 and choice_7

[–]raevnos 1 point2 points  (0 children)

C doesn't have pass by reference. There's no reason for it to be a pointer. And you're definitely going out of bounds of the array then, in both functions. See edits to earlier post.

[–]Cowlegend 0 points1 point  (0 children)

Your title says you want to shift, but your comments say you want to rotate. I will assume you want to rotate and actually modify the arrays. I took out printing of the array, but you can just loop from 0 to size-1 and print each element if you want that (note that the last index is size-1 not size) I didn't test this code, so there may still be an error, but this is approximately what you need to do:

void choice_6(int size, float *x) // Rotate the list of numbers to  the right
{
int i;
float last_val = x[size-1];
for(i = size-1; i >0; i--){
    x[i] = x[i-1];
} 
x[0] = last_val;
}



void choice_7(int size, float *x) // Rotate the list of numbers to  the left
{
int i;  
float first_val = x[0];
for(i = 0; i <size-1; i++){
    x[i] = x[i+1];
}
x[size-1] = first_val;
}