What am I missing in my code?

I take an integer array, it's length and the number of elements to right shift.

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
#include <iostream>
#include <string>
#include <vector>
using namespace std;

void shiftright (int myarray[], int size);

int main (void)
{
int myarray []= {1, 2, 3, 4, 5};

shiftright( myarray, 5);

for ( int i=0; i<5; i++) 
{
	cout << myarray[i] << ' ';
}

return(0);

}

void shiftright (int myarray[], int size, int M)
{
for (int m = (size-1); m>=1; m--){  
int temp = myarray[size-1];

  for (int i=(size- 1); i>=1; i--)
  {
    myarray[i] = myarray[i-1] ;
  }
myarray[0] = temp;
}

}
Line 6: void shiftright (int myarray[], int size);

This tells the compiler that shiftright() has 2 parameters, however in line 23

void shiftright (int myarray[], int size, int M) // incorrect

you are telling the compiler that shiftright() now has 3 parameters. Drop the int M because you are declaring it inside the for loop (line 25).

void shiftright (int myarray[], int size) // fixed
But that doesn't solve the problem of shifting the elements to the right.
You shifted them to the right 4 times.

1
2
3
4
5
6
7
8
9
10
11
12
void shiftright (int myarray[], int size)
{
    for (int m = (size-1); m>=1; m--){  // shifts 4 times
        int temp = myarray[size-1];

        for (int i=(size- 1); i>=1; i--)
        {
            myarray[i] = myarray[i-1] ;
        }
        myarray[0] = temp;
    }
}


Just set it to m>=4 if you want it to only shift once (or trash the m loop all together)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <string>
#include <vector>
using namespace std;

/**
 * PURPOSE: Takes in an integer array (arr[]), its length (N), and the number of elements to right-shift (M):
 * PARAMETERS:
 *     arr[] , integer array
 *     N , its length
 *     M , number of elements to right shift
 * RETURN VALUES:
 *     the new array after right shifting the elements.
*/
 

void rightShiftElementsbyOne(int arr[], int N);
 
/*Function to right rotate arr[] of size N by M elements*/
void rightShiftElements(int arr[], int N, int M)
{
  int i;
  for (i = 0; i < N - M; i++)
    rightShiftElementsbyOne(arr, N);
   
}
 
void rightShiftElementsbyOne(int arr[], int N)
{
  int i, temp;
  temp = arr[0];
  for (i = 0; i < N-1; i++)
     arr[i] = arr[i+1];
  arr[i] = temp;
}
 
/* utility function to print an array */
void printArray(int arr[], int size)
{
  int i;
  for(i = 0; i < size; i++)
    printf("%d ", arr[i]);
}
 
/* Driver program to test above functions */
int main()
{
    
    // it shifts to the right if (N - 2)+ M
   int arr[] = {1, 2, 3, 4, 5, 6, 7};
   rightShiftElements(arr, 7, 6);
   printArray(arr, 7);
   getchar();
   return 0;
}


This doesn't work 100% of the time, only when the number of shifts is less than or equal to the number of elements.
Does someone give you the elements in the array or just the number of elements?
If you are given the number of elements but not what each element is, do you assume each element is from 1 to the given length in ascending order?
Are you given the number of ELEMENTS to shift or the number of TIMES to shift?
If you are given the number of elements to shift, how do you know which ones to shift?
What happens to the gap between elements that sit still and elements that are shifted?
What happens if elements are shifted off the edge of the array?
Are they discarded or stuck on the beginning of the array?
What happens to elements that are overwritten because they stay still but the others move to their position?
If you are given the number of times to shift, do elements at the end get discarded or are they looped back to the beginning?
If they are not looped back to the beginning, what happens to the gaps in the array where elements no longer exist?
Do those elements become 0 or are they unchanged?
I'm given a positive integer array in order for example: {1,2,3,4,5} but never {2,1,3,5,4}. Always in ascending order and only integers.
Topic archived. No new replies allowed.