-
October 10th, 2019, 01:05 PM
#1
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)
-
October 10th, 2019, 01:43 PM
#2
Re: Reversing an array
And what is your question?
Victor Nijegorodov
-
October 10th, 2019, 02:11 PM
#3
Re: Reversing an array
Originally Posted by VictorN
And what is your question?
Looking for better implementation and comments.
-
October 10th, 2019, 03:20 PM
#4
Re: Reversing an array
@kaud: please help me with your inputs
Last edited by pdk5; October 10th, 2019 at 03:40 PM.
-
October 11th, 2019, 03:26 AM
#5
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)
-
October 11th, 2019, 03:34 AM
#6
Re: Reversing an array
Originally Posted by pdk5
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
-
October 12th, 2019, 02:40 AM
#7
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
-
October 12th, 2019, 06:23 AM
#8
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)
-
October 12th, 2019, 07:39 AM
#9
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.
-
October 12th, 2019, 07:46 AM
#10
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)
-
October 12th, 2019, 08:06 AM
#11
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 ?
-
October 12th, 2019, 08:14 AM
#12
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)
-
October 24th, 2019, 09:58 AM
#13
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.
-
October 24th, 2019, 10:02 AM
#14
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)
-
October 24th, 2019, 10:17 AM
#15
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 ?
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|