CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    May 2015
    Posts
    500

    Reversing an array

    Hi,

    I have an input array which is containing info from a rectangle, each point is started from left to right and top to bottom,

    I want to put this into an array, where the info is read from left to right, but from bottom to top .

    Lets say the width is fixed. My pseudocode. I;ll try compiling and [put actual code soon

    int inputArray[1000];
    int outputArray[1000]

    int width = 5;

    int in_index = max_size-width;

    do{
    for(j=0; j<width; j++)
    {
    outputArray[outindex] = inputArray[inindex];
    }

    in_index = in_index-width;

    }
    while(in_index > = 0)

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

    Re: Reversing an array

    And what is your question?
    Victor Nijegorodov

  3. #3
    Join Date
    May 2015
    Posts
    500

    Re: Reversing an array

    Quote Originally Posted by VictorN View Post
    And what is your question?
    Looking for better implementation and comments.

  4. #4
    Join Date
    May 2015
    Posts
    500

    Re: Reversing an array

    @kaud: please help me with your inputs
    Last edited by pdk5; October 10th, 2019 at 03:40 PM.

  5. #5
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Reversing an array

    input array which is containing info from a rectangle, each point is started from left to right and top to bottom,

    I want to put this into an array, where the info is read from left to right, but from bottom to top .
    I'm not sure I understand. Please give an example of the contents of the input array and the corresponding contents of the required output array.
    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++23 Compiler: Microsoft VS2022 (17.6.5)

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

    Re: Reversing an array

    Quote Originally Posted by pdk5 View Post
    Looking for better implementation and comments.
    Did you consider using the array of CRect or RECT objects to do reverse directly with their top/bottom?
    Victor Nijegorodov

  7. #7
    Join Date
    May 2015
    Posts
    500

    Re: Reversing an array

    I have now written a simple example.

    Code:
        int inputArray[8] = {0,1,2,3,4,5, 6, 7};
        int outputArray[8];
    
        int max = 8;
        int width = 4;
    
        int outindex = 0;
        int inindex = max-width;
    
        do {
            for (int j = 0; j < width; j++) {
                outputArray[outindex] = inputArray[inindex + j];
                outindex++;
                int q = inindex + j;
            }
    
            inindex= inindex - width;
        } while (inindex >= 0);
    
        for (int x = 0; x < max; x++) {
               cout <<  outputArray[x] << "\t";
        }
    Last edited by 2kaud; October 12th, 2019 at 06:26 AM. Reason: Added code tags

  8. #8
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Reversing an array

    ...and what happened to code tags?

    Try this:

    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main()
    {
    	const size_t width = 4;
    
    	const vector<int> inputArray {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
    	vector<int> outputArray; outputArray.reserve(inputArray.size());
    
    	for (int inindex = inputArray.size() - width; inindex >= 0; inindex -= width)
    		for (int j = 0; j < width; ++j)
    			outputArray.push_back(inputArray[inindex + j]);
    
    	for (const auto& x : outputArray)
    		cout << x << "\t";
    }
    which produces the output:

    Code:
    8       9       10      11      4       5       6       7       0       1       2       3
    Last edited by 2kaud; October 12th, 2019 at 07:34 AM.
    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++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    May 2015
    Posts
    500

    Re: Reversing an array

    Thanks a lot kaud for the code.

    Somehow it was easy in eclipse, we select the blcok and ctrl+i, indents. But visual studio i couldnot find such command.

  10. #10
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Reversing an array

    However, if you organise the input data in blocks of 4, rather than straight linear, then it is even simpler as std::reverse_copy() can be used. Consider:

    Code:
    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    int main()
    {
    	const vector<vector<int>> inputArray {{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}};
    	vector<vector<int>> outputArray(inputArray.size());
    
    	reverse_copy(inputArray.begin(), inputArray.end(), outputArray.begin());
    
    	for (const auto& x : outputArray)
    		for (const auto& y : x)
    			cout << y << "\t";
    }
    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++23 Compiler: Microsoft VS2022 (17.6.5)

  11. #11
    Join Date
    May 2015
    Posts
    500

    Re: Reversing an array

    Thanks a lot kaud for the options. I actually was storing the input in an array. Because each value is generated one at a time. I guess it is better to store in the vector and do pushback on each of them.
    Will it be optimal, if is large array to store in vector because of the stl overhead ?

  12. #12
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Reversing an array

    If you know the number of elements that's going to be entered into the input vector (and if you're currently using an array then you must at least know the upper bound), then the storage can be pre-allocated via reserve() so that pushback doesn't incur the reallocation overhead. The same with the output vector (see the code in post #8).
    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++23 Compiler: Microsoft VS2022 (17.6.5)

  13. #13
    Join Date
    May 2015
    Posts
    500

    Re: Reversing an array

    I retained the following code, as its using the pure malloc. And also i needed to deliver that week itself. So didnot have time to do the changes.

    But now coming back while testing with the client,
    Code:
    	    vector<float> lossvalues;
    		int widthOfRectangle = 0;
    		pInfo.PerformLossCalc(nResolution_cm, bLimitIteratorRegion, lossvalues, widthOfRectangle);
    		
    		ULONG numberOflossvalues = lossvalues.size();
    		float *pDesignlossValues = (float *)malloc(sizeof(float)*numberOflossvalues);
    		ULONG outindex = 0;
    		ULONG inindex = numberOflossvalues - widthOfRectangle;
    		do {
    			for (int j = 0; j<widthOfRectangle; j++)
    			{
    				pDesignlossValues[outindex] = lossvalues[inindex + j];
    				outindex++;
    			}
    			inindex = inindex - widthOfRectangle;
    		} while (inindex >= 0);
    
    		// copy the  results in the correct format to the pdesign output Rectangle
    		memcpy(out.propagation->getBuffer(), pDesignlossValues, sizeof(float) * numberOflossvalues);
    But now this code is asserting when the numberOflossvalues =40402 and width =201.

  14. #14
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Reversing an array

    and debugging shows the issue is where because...? What assert?
    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++23 Compiler: Microsoft VS2022 (17.6.5)

  15. #15
    Join Date
    May 2015
    Posts
    500

    Re: Reversing an array

    Ah I remember, it was because inindex is ULONG. I changed it in my local setup at that time. (it needs to be signed)
    Btw, what is the signed long int equivalent of ULONG ?

Page 1 of 2 12 LastLast

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