CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2016
    Posts
    1

    Question How do I complete my code to shift array elements to the right?

    I've asked on other sites but have not gotten an answer. So I started an account to ask this question here. I need to write a function that takes an integer array (arr[]), its length (N) and the number of elements to right shift (M). My current code does not work, but I think I might be close to solving this problem.

    Code:
    #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 rightShiftElements (int arr[], int N);
    
    int main (void)
    {
    int arr []= {1, 2, 3, 4, 5};
    
    shiftright( arr, 5);
    
    for ( int i=0; i<5; i++) 
    {
    	cout << arr[i] << ' ';
    }
    
    return(0);
    
    }
    
    void rightShiftElements (int arr[], int N, int M)
    {
    for (int m = (N-1); m>=1; m--){  
    int temp = arr[N-1];
    
      for (int i=(N- 1); i>=1; i--)
      {
        arr[i] = arr[i-1] ;
      }
    arr[0] = temp;
    }
    
    }

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: How do I complete my code to shift array elements to the right?

    you have non-matching signatures:
    void rightShiftElements (int arr[], int N);
    vs
    void rightShiftElements (int arr[], int N, int M)

    Also, you are calling it with the wrong name:
    shiftright( arr, 5);

    Fixing these issues means your code works and is correct in the case where you want to shift for exactly 1 element, which is good.

    Start by fixing that, and then take it to the next step, shifting by M elements at once.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,095

    Re: How do I complete my code to shift array elements to the right?

    1. What are these "functions" you refer to in your code snippet:
    • void rightShiftElements (int arr[], int N);
    • void rightShiftElements (int arr[], int N, int M)
    • shiftright( arr, 5);


    2. Your original array is
    Code:
    int arr []= {1, 2, 3, 4, 5};
    How would it look like after the shifting?
    Victor Nijegorodov

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,551

    Re: How do I complete my code to shift array elements to the right?

    My current code does not work,
    Have you used the debugger to debug the code and see where the execution deviates from that expected? Have you tried doing this with a pen and paper to develop the algorithm required? Trace through the code and then you'll see where is the issue.

    Why are you including <vector> and <string> when a vector/string is not used in the code?

    You don't need to pass the number of elements to rightShiftElements. From your existing non-fixed code
    Code:
    #include <iostream>
    using namespace std;
    
    template<size_t N>
    void rightShiftElements(int (&arr)[N], int M)
    {
    	for (int m = (N - 1); m >= 1; m--) {
    		int temp = arr[N - 1];
    
    		for (int i = (N - 1); i >= 1; i--)
    			arr[i] = arr[i - 1];
    
    		arr[0] = temp;
    	}
    }
    
    int main(void)
    {
    	int arr[] = { 1, 2, 3, 4, 5 };
    
    	rightShiftElements(arr, 3);
    
    	for (const auto& a : arr)
    		cout << a << ' ';
    
    	return 0;
    }
    Also note that I'm changed your loop to display the contents of arr so that it will work with however many elements there are in arr.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2019 (16.11.5)

  5. #5
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: How do I complete my code to shift array elements to the right?

    This approach creates binary bloat, and increases compile times. What you want to do is merely use the template interface to forward to a non-template interface.

    But even then, things like c++17's std::size kind of make this obsolete, and arguably, you shouldn't be dealing with arrays to begin with. Worst case scenario, you also have std::array. IMO, it's not something you want to promote with easy to use interfaces.

    The *only* place I'd arguably see this approach is being worth using, is for functions that deal with manifest constants (think raw strings). Typically, microsoft's "_s" variants make good use of this: strncpy_s has overloads that infers the target buffer size if it can.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,551

    Re: How do I complete my code to shift array elements to the right?

    and arguably, you shouldn't be dealing with arrays to begin with
    yep!

    c++17's std::size kind of make this obsolete
    Can't wait!

    but in the meantime if a c-style array is used as a function parameter I really, really, really don't like having to pass the number of elements as function parameter(s) IMO having some binary bloat and an increased compile time is worth it for not having to do this.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2019 (16.11.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured