Dereferencing member pointer - c++

I have a c++ struct which has a dynamically allocated array as a pointer. I have a function to reverse the array, but it doesn't seem to work (I think its because the temporary variable points to the original value).
struct s {
int *array;
int length;
...
s(int n) {
this->array = new int[n];
this->length = n;
}
...
void reverse() {
for (int i = 0; i < this->length; i++) {
int n = this->array[i];
this->array[i] = this->array[this->length - i - 1];
this->array[this->length - i - 1] = n;
}
}
...
}
I think what this is doing is this->array[this->length - i - 1] = this->array[i]
Hence the array remains the same and doesn't get reversed. I don't know how to deference the array pointer or how to just take the value of this->array[i] in n.

The reason your reverse doesn't work is that you're going through the length of the whole array. You need to only go through half of it. If you go through the second half, then you're un-reversing it.
As an example, if you try to reverse [1, 2, 3, 4] you get
after i = 0: [4, 2, 3, 1]
after i = 1: [4, 3, 2, 1]
--- reversed ---
after i = 2: [4, 2, 3, 1]
after i = 3: [1, 2, 3, 4]
--- back to original ---
Instead, just make your loop
for (int i = 0; i < this->length / 2; i++) {
...
}

On a side note, using 2 indexers will simplify your code considerably:
void reverse()
{
int limit = length / 2;
for ( int front = 0 , back = length - 1; front < limit; front++ , back-- )
{
int n = array[front];
array[front] = array[back];
array[back] = n;
}
}

Related

I need to implement a simple shuffle and sort alogrithm as a class

I have created the class
void NmbSetA::sort(){
bool swaped = false;
int minPos, tmp;
for(int i = 0; i < (size_ - 1); i++){
minPos = i;
for(int j = (i + 1); j < size_; j++){
if(dataField_[j] < dataField_[minPos]){
minPos = j;
swaped = true;
}
}
if(swaped){
tmp = dataField_[i];
dataField_[i] = dataField_[minPos];
dataField_[minPos] = tmp;
}
}
}
and the class
void NmbSetA::shuffle(){
for(int i=size_-1;i>=1;i--){
int j = rand()%size_;
int tmp;
tmp=dataField_[i];
dataField_[i]=j;
dataField_[j]=tmp;
}
My output looks like this, what can I improve?
turn 1:
shuffled: []
sorted : []
turn 2:
shuffled: [9180128]
sorted : [0]
turn 3:
shuffled: [0, 10]
sorted : [0, 2]
turn 4:
shuffled: [11, 6, 7]
sorted : [0, 1, 2]
turn 5:
shuffled: [0, 2, 1, 9]
sorted : [0, 1, 2, 2]
turn 6:
shuffled: [5, 8, 7, 0, 7]
sorted : [0, 1, 2, 3, 5]
It should put out a shuffled array from 1 to turn n-1 and sort it. The Way the array is created is given and cannot be changed. I only should add the method to sort and shuffle the created array. Sorting it from the lowest to the highest number.
You could use the STL to solve your problem. The STL has a sort and a shuffle algorithm:
constexpr std::size_t size;
class NmbSetA {
public:
NmbSetA();
void sort();
void shuffle() {
std::shuffle(dataField_, dataField_ + size_, g);
}
private:
std::random_device rd;
std::mt19937 g;
int dataField_[size];
std::size_t size_ = size;
};
NmbSetA::NmbSetA() : g(rd()) {}
void NmbSetA::sort() {
std::sort(dataField_, dataField_ + size_);
}
void NmbSetA::shuffle() {
std::shuffle(dataField_, dataField_ + size_, g);
}

Dividing a data set almost equally among worker threads

I have n number of elements and p number of threads. I am trying to divide the elements as equally as possible among the threads.
For example:
If n = 8 and p = 1, then [8]
If n = 8 and p = 2, then [4, 4]
If n = 8 and p = 3, then [2, 3, 3]
If n = 8 and p = 4, then [2, 2, 2, 2]
If n = 8 and p = 5, then [1, 1, 2, 2, 2]
If n = 8 and p = 6, then [1, 1, 1, 1, 2, 2]
If n = 8 and p = 7, then [1, 1, 1, 1, 1, 1, 2]
If n = 8 and p = 8, then [1, 1, 1, 1, 1, 1, 1, 1]
I cooked up a solution that almost works but not quite.
#include <vector>
#include <stdio.h>
#include <cmath>
int main(int argc, char **argv)
{
int p = 5;
const int SIZE = 8;
int i = 0;
int num = 0;
std::vector<int> iter;
if (p == 1)
iter.push_back(SIZE);
else
{
if (SIZE % p == 0)
{
num = SIZE / p;
for (i = 0; i < p; ++i)
iter.push_back(num);
}
else
{
num = (int)floor((float)SIZE / (float)p);
for (i = 0; i < p - 1; ++i)
iter.push_back(num);
iter.push_back((SIZE - (num * (p - 1))));
}
}
for (unsigned int j = 0; j < iter.size(); ++j)
printf("[%d] = %d\n", j, (int)iter[j]);
return 0;
}
Results produced with my solution:
If n = 8 and p = 1, then [8]
If n = 8 and p = 2, then [4, 4]
If n = 8 and p = 3, then [2, 2, 4]
If n = 8 and p = 4, then [2, 2, 2, 2]
If n = 8 and p = 5, then [1, 1, 1, 1, 4]
If n = 8 and p = 6, then [1, 1, 1, 1, 1, 3]
If n = 8 and p = 7, then [1, 1, 1, 1, 1, 1, 2]
If n = 8 and p = 8, then [1, 1, 1, 1, 1, 1, 1, 1]
try to think about this. if you have less objects then threads then each thread will get one object. if you have more objects then threads (buckets) then think about how would you divide 100 tennis balls to 8 buckets.
you could take 1 ball at a time and put that in the next bucket, once you passed all buckets you start from first bucket, this will make sure that at most the difference between each bucket size will be 1.
#include <vector>
#include <stdio.h>
int main(int argc, char **argv)
{
int p = 5;
const int SIZE = 8;
int p_size = SIZE > p ? p : SIZE;
std::vector<int> iter(p_size);
for (int i = 0; i < SIZE; i++)
{
iter[i%p_size] += 1;
}
for (unsigned int j = 0; j < iter.size(); ++j)
printf("[%d] = %d\n", j, (int)iter[j]);
return 0;
}
You can try this:
std::vector<int> iter(p);
std::generate(iter.begin(), iter.end(), [&]()
{
num += 1;
return SIZE / p + (num <= SIZE % p ? 1 : 0);
});
The first line creates the required number of elements, and the second operator fills this vector with actual data. It is written without explicit loops to make code more expressive.
This is less an answer to your specific question/problem, but more an alternative approach to your intended problem.
your solution is way to complicate... this code does the same, except the additional tasks are put into the front...
#include <iostream>
#include <vector>
int main(int argc, char **argv)
{
int p = 5;
const int n = 8;
// calculate number of tasks every thread have to do...
int every = n / p;
// calculate rest
int rest = n % p;
// initialize the vector with the number of tasks every thread have to do
std::vector<int> lst(p, every);
// split rest on the threads
for(int i=0; i<rest; ++i)
lst[i]++;
// print out
for(auto it : lst)
std::cout << it << ",";
return 0;
}
The trick is integer truncation, there is no need for floating point arithmetics, as you probably see, the other answers also do...

Mixing two arrays by alternating elements two by two

What is an elegant algorithm to mix the elements two by two in two arrays (of potentially differing sizes) so that the items are drawn in an alternating fashion from each array, with the leftovers added to the end?
E.g.
Array 1: 0, 2, 4, 6
Array 2: 1, 3, 5, 7
Mixed array: 0, 2, 1, 3, 4, 6, 5, 7
Don't worry about null checking or any other edge cases, I'll handle those.
Here is my solution but it does not work properly:
for (i = 0; i < N; i++) {
arr[2 * i + 0] = A[i];
arr[2 * i + 1] = A[i+1];
arr[2 * i + 0] = B[i];
arr[2 * i + 1] = B[i+1];
}
It is very fiddly to calculate the array indices explicitly, especially if your arrays can be of different and possibly odd lengths. It is easier if you keep three separate indices, one for each array:
int pairwise(int c[], const int a[], size_t alen, const int b[], size_t blen)
{
size_t i = 0; // index into a
size_t j = 0; // index into b
size_t k = 0; // index into c
while (i < alen || j < blen) {
if (i < alen) c[k++] = a[i++];
if (i < alen) c[k++] = a[i++];
if (j < blen) c[k++] = b[j++];
if (j < blen) c[k++] = b[j++];
}
return k;
}
The returned value k will be equal to alen + blen, which is the implicit dimension of the result array c. Because the availability of a next item is checked for each array operation, this code works for arrays of different lengths and when the arrays have an odd number of elements.
You can use the code like this:
#define countof(x) (sizeof(x) / sizeof(*x))
int main()
{
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int b[] = {-1, -2, -3, -4, -5, -6};
int c[countof(a) + countof(b)];
int i, n;
n = pairwise(c, a, countof(a), b, countof(b));
for (i = 0; i < n; i++) {
if (i) printf(", ");
printf("%d", c[i]);
}
puts("");
return 0;
}
(The example is in C, not C++, but your code doesn't use any of C++'s containers such as vector, so I've uses plain old ´int` arrays with explicit dimensions, which are the same in C and C++.)
Some notes on the loop you have;
You use the same position in the result array arr to assign two values to it (one from A and one from B).
The calculation for the index is possibly more complex than it needs to be, consider using two indexers given the two ways you are indexing over the arrays.
I would propose you use a loop that has two indexers (i and j) and explicitly loop over the four elements of the result (i.e. two position for each input array). In each loop you increment the indexers appropriately (by 4 for the output array and by 2 for the input arrays).
#include <iostream>
int main()
{
using namespace std;
constexpr int N = 4;
int A[N] = {2, 4, 6, 8};
int B[N] = {1, 3, 5, 7};
int arr[N*2];
for (auto i = 0, j=0; i < N*2; i+=4, j+=2) {
arr[i + 0] = A[j];
arr[i + 1] = A[j+1];
arr[i + 2] = B[j];
arr[i + 3] = B[j+1];
}
for (auto i =0; i < N*2; ++i) {
cout << arr[i] << ",";
}
cout << endl;
}
Note: you mention you take care of corner cases, so the code here requires the input arrays to be of the same length and that the length is even.
Try this:
for (i = 0; i < N; i += 2) {
arr[2 * i + 0] = A[i];
arr[2 * i + 1] = A[i+1];
arr[2 * i + 2] = B[i];
arr[2 * i + 3] = B[i+1];
}
Didn't consider any corner case, just fixing your concept. For example, check whether any array index out of bound occurs or not. You can run live here.
it should like this.
for (i = 0; i < N; i+=2) {
arr[2 * i + 0] = A[i];
arr[2 * i + 1] = A[i+1];
arr[2 * i + 2] = B[i];
arr[2 * i + 3] = B[i+1];
}

Find the minimum sum of 'P' elements in an array of N elements such that no more than 'k' consecutive elements are selected together

Suppose the array is 1 2 3 4 5
Here N = 5 and we have to select 3 elements and we cannot select more than 2 consecutive elements, so P = 3 and k = 2. So the output here will be 1 + 2 + 4 = 7.
I came up with a recursive solution, but it has an exponential time complexity. Here is the code.
#include<iostream>
using namespace std;
void mincost_hoarding (int *arr, int max_size, int P, int k, int iter, int& min_val, int sum_sofar, int orig_k)
{
if (P == 0)
{
if (sum_sofar < min_val)
min_val = sum_sofar;
return;
}
if (iter == max_size)
return;
if (k!=0)
{
mincost_hoarding (arr, max_size, P - 1, k - 1, iter + 1, min_val, sum_sofar + arr[iter], orig_k);
mincost_hoarding (arr, max_size, P, orig_k, iter + 1, min_val, sum_sofar, orig_k);
}
else
{
mincost_hoarding (arr, max_size, P, orig_k, iter + 1, min_val, sum_sofar, orig_k);
}
}
int main()
{
int a[] = {10, 5, 13, 8, 2, 11, 6, 4};
int N = sizeof(a)/sizeof(a[0]);
int P = 2;
int k = 1;
int min_val = INT_MAX;
mincost_hoarding (a, N, P, k, 0, min_val, 0, k);
cout<<min_val;
}
Also, if supposedly P elements cannot be selected following the constraint, then we return INT_MAX.
I was asked this question in an interview. After proposing this solution, the interviewer was expecting something faster. Maybe, a DP approach towards the problem. Can someone propose a DP algorithm if there exists one, or a faster algorithm.
I have tried various tests cases and got correct answers. If you find some test cases that are giving incorrect response, please point that out too.
Below is a Java Dynamic Programming algorithm.
(the C++ version should look very similar)
It basically works as follows:
Have a 3D array of [pos][consecutive length][length]
Here length index = actual length - 1), so [0] would be length 1, similarly for consecutive length. This was done since there's no point to having length 0 anywhere.
At every position:
If at length 0 and consecutive length 0, just use the value at pos.
Otherwise, if consecutive length 0, look around for the minimum in all the previous positions (except pos - 1) with length - 1 and use that plus the value at pos.
For everything else, if pos > 0 && consecutive length > 0 && length > 0,
use [pos-1][consecutive length-1][length-1] plus the value at pos.
If one of those are 0, initialize it to an invalid value.
Initially it felt like one only needs 2 dimensions for this problem, however, as soon as I tried to figure it out, I realized I needed a 3rd.
Code:
int[] arr = {1, 2, 3, 4, 5};
int k = 2, P = 3;
int[][][] A = new int[arr.length][P][k];
for (int pos = 0; pos < arr.length; pos++)
for (int len = 0; len < P; len++)
{
int min = 1000000;
if (len > 0)
{
for (int pos2 = 0; pos2 < pos-1; pos2++)
for (int con = 0; con < k; con++)
min = Math.min(min, A[pos2][len-1][con]);
A[pos][len][0] = min + arr[pos];
}
else
A[pos][0][0] = arr[pos];
for (int con = 1; con < k; con++)
if (pos > 0 && len > 0)
A[pos][len][con] = A[pos-1][len-1][con-1] + arr[pos];
else
A[pos][len][con] = 1000000;
}
// Determine the minimum sum
int min = 100000;
for (int pos = 0; pos < arr.length; pos++)
for (int con = 0; con < k; con++)
min = Math.min(A[pos][P-1][con], min);
System.out.println(min);
Here we get 7 as output, as expected.
Running time: O(N2k + NPk)

fastest algorithm count number of 3 length AP in array

I want to solve this CodeChef challenge:
Suppose We are given an array A of N(of range 100,000) elements. We are to find the count of all pairs of 3 such elements 1<=Ai,Aj,Ak<=30,000 such that
Aj-Ai = Ak- Aj and i < j < k
In other words Ai,Aj,Ak are in Arithmetic Progression. For instance for Array :
9 4 2 3 6 10 3 3 10
so The AP are:
{2,6,10},{9,6,3},{9,6,3},{3,3,3},{2,6,10}
So the required answer is 5.
My Approach
What I tried is take 30,000 long arrays named past and right. Initially right contains the count of each 1-30,000 element.
If we are at ith position past stores the count of array value before i and right stores the count of array after i. I simply loop for all possible common difference in the array. Here is the code :
right[arr[1]]--;
for(i=2;i<=n-1;i++)
{
past[arr[i-1]]++;
right[arr[i]]--;
k=30000 - arr[i];
if(arr[i] <= 15000)
k=arr[i];
for(d=1;d<=k;d++)
{
ans+= right[arr[i] + d]*past[arr[i]-d] + past[arr[i] + d]*right[arr[i]-d];
}
ans+=past[arr[i]]*right[arr[i]];
}
But this gets me Time Limit Exceeded. Please help with a better algorithm.
You can greatly cut execution time if you make a first pass over the list and only extract number pairs that it is possible to have an 3 term AP between (difference is 0 mod 2). And then iterating between such pairs.
Pseudo C++-y code:
// Contains information about each beginning point
struct BeginNode {
int value;
size_t offset;
SortedList<EndNode> ends; //sorted by EndNode.value
};
// Contains information about each class of end point
struct EndNode {
int value;
List<size_t> offsets; // will be sorted without effort due to how we collect offsets
};
struct Result {
size_t begin;
size_t middle;
size_t end;
};
SortedList<BeginNode> nodeList;
foreach (auto i : baseList) {
BeginNode begin;
node.value = i;
node.offset = i's offset; //you'll need to use old school for (i=0;etc;i++) with this
// baseList is the list between begin and end-2 (inclusive)
foreach (auto j : restList) {
// restList is the list between iterator i+2 and end (inclusive)
// we do not need to consider i+1, because not enough space for AP
if ((i-j)%2 == 0) { //if it's possible to have a 3 term AP between these two nodes
size_t listOffset = binarySearch(begin.ends);
if (listOffset is valid) {
begin.ends[listOffset].offsets.push_back(offsets);
} else {
EndNode end;
end.value = j;
end.offsets.push_back(j's offset);
begin.ends.sorted_insert(end);
}
}
}
if (begin has shit in it) {
nodeList.sorted_insert(begin);
}
}
// Collection done, now iterate over collection
List<Result> res;
foreach (auto node : nodeList) {
foreach (auto endNode : node.ends) {
foreach (value : sublist from node.offset until endNode.offsets.last()) {
if (value == average(node.value, endNode.value)) {
// binary_search here to determine how many offsets in "endNode.offsets" "value's offset" is less than.
do this that many times:
res.push_back({node.value, value, endNode.value});
}
}
}
}
return res;
Here's a simple C version of the solution that takes advantage of the Ai + Ak must be even test:
#include <stdio.h>
static int arr[] = {9, 4, 2, 3, 6, 10, 3, 3, 10};
int main ()
{
int i, j, k;
int sz = sizeof(arr)/sizeof(arr[0]);
int count = 0;
for (i = 0; i < sz - 2; i++)
{
for (k = i + 2; k < sz; k++)
{
int ik = arr[i] + arr[k];
int ikdb2 = ik / 2;
if ((ikdb2 * 2) == ik) // if ik is even
{
for (j = i + 1; j < k; j++)
{
if (arr[j] == ikdb2)
{
count += 1;
printf("{%d, %d, %d}\n", arr[i], arr[j], arr[k]);
}
}
}
}
}
printf("Count is: %d\n", count);
}
and the console dribble:
tmp e$ cc -o triples triples.c
tmp e$ ./triples
{9, 6, 3}
{9, 6, 3}
{2, 6, 10}
{2, 6, 10}
{3, 3, 3}
Count is: 5
tmp e$
This more complicated version keeps a list of Aj indexed by value to go from n-cubed to n-squared (kinda).
#include <stdio.h>
#include <stdint.h>
static uint32_t arr[] = {9, 4, 2, 3, 6, 10, 3, 3, 10};
#define MAX_VALUE 100000u
#define MAX_ASIZE 30000u
static uint16_t index[MAX_VALUE+1];
static uint16_t list[MAX_ASIZE+1];
static inline void remove_from_index (int subscript)
{
list[subscript] = 0u; // it is guaranteed to be the last element
uint32_t value = arr[subscript];
if (value <= MAX_VALUE && subscript == index[value])
{
index[value] = 0u; // list now empty
}
}
static inline void add_to_index (int subscript)
{
uint32_t value = arr[subscript];
if (value <= MAX_VALUE)
{
list[subscript] = index[value]; // cons
index[value] = subscript;
}
}
int main ()
{
int i, k;
int sz = sizeof(arr)/sizeof(arr[0]);
int count = 0;
for (i = 0; i < sz - 2; i++)
{
for (k = i; k < sz; k++) remove_from_index(k);
for (k = i + 2; k < sz; k++)
{
uint32_t ik = arr[i] + arr[k];
uint32_t ikdb2 = ik / 2;
add_to_index(k-1); // A(k-1) is now a legal middle value
if ((ikdb2 * 2) == ik) // if ik is even
{
uint16_t rover = index[ikdb2];
while (rover != 0u)
{
count += 1;
printf("{%d, %d, %d}\n", arr[i], arr[rover], arr[k]);
rover = list[rover];
}
}
}
}
printf("Count is: %d\n", count);
}
and the dribble:
tmp e$ cc -o triples triples.c
tmp e$ ./triples
{9, 6, 3}
{9, 6, 3}
{2, 6, 10}
{2, 6, 10}
{3, 3, 3}
Count is: 5
tmp e$