Summing the elements of two int vectors parallel to each other - c++

I'm trying to get the sum of each element of two vectors and push the total to a new vector.
ie.
a = {1, 2 ,3} & b = {1, 2 ,3}
c = a + b
c = {2, 4, 6}
I've got it working for vectors of the same size, but whenever one of the vectors is larger than the other I get a vector subscript out of range error.
a = {1, 2 ,3} & b = {1, 2 ,3, 4}
c = a + b
I'm assuming this is happening during b[3] + ?? , how would I go about adding it so the output is:
c = {2, 4, 6, 4}
What i've got so far is:
vector<int> a = { 1,2,3,4 };
vector<int> b = { 5,4,3,2,1 };
vector<int> *vPtr;
vPtr = new vector<int>;
int sum;
int size = a.size();
if (size < b.size())
size = b.size();
for (unsigned i = 0; i < size; i++) {
sum = a[i] + b[i];
(*vPtr).push_back(sum);
}
for (vector<int>::const_iterator it = (*vPtr).begin(); it != (*vPtr).end(); it++)
cout << *it << " ";
cout << endl;
return 0;

Pad the smaller vector with extra zeros at the end. Here's an example:
int sizeDifference = abs(a.size() - b.size());
if(sizeDifference != 0){
if(a.size() > b.size())
for(int i = 0; i<sizeDifference; ++i)
b.push_back(0);
else
for(int i = 0; i<sizeDifference; ++i)
a.push_back(0);
}

Typically I do this with iterators in three parts. The part where both vectors have common elements. The part if/where a is longer than b and the part if/where b is longer than a.
std::vector<int> a = {1, 2, 3};
std::vector<int> b = {4, 5, 6, 7};
std::vector<int> c;
// reserve enough space for the longest vector
c.reserve(std::max(a.size(), b.size()));
auto a_itr = std::begin(a);
auto b_itr = std::begin(b);
// for the part where the positions coincide
for(; a_itr != std::end(a) && b_itr != std::end(b); ++a_itr, ++b_itr)
c.push_back(*a_itr + *b_itr);
// for the part where a is longer than b (if any)
c.insert(std::end(c), a_itr, std::end(a));
// for the part where b is longer than a (if any)
c.insert(std::end(c), b_itr, std::end(b));
for(auto i: c)
std::cout << i << '\n';

size_type sm_size, lg_size;
vector<int> *lg_vec_ptr;
if (a.size < b.size()) {
sm_size = a.size();
lg_size = b.size();
lg_vector_ptr = &b;
} else {
sm_size = b.size();
lg_size = a.size();
lg_vector_ptr = &a;
}
vector<int> *sum_vec_ptr = new vector<int>;
size_type i;
for (i=0; i<sm_size; ++i)
sum_vec_ptr->push_back( a[i] + b[i] );
for ( ; i<lg_size; ++i)
sum_vec_ptr->push_back( lg_vector_ptr->[i] );
This method doesn't modify the original vectors.

Related

Question about comparing two unordered arrays and removing elements in a[] that can be found in b[]

There is a problem that I can't solve it. Here are two unordered arrays
int a1[] = { 5, 7, 14, 0, 6, 2, 9, 11, 3 }; int n = 9;
int b[] = { 6, 4, 3, 10, 9, 15, 7 }; int m = 7;
I want to compare them and remove elements in a[] that can be found in b[]. The following code return a correct value of n to me. The correct value should be 4 but it give me 5 even if I successfully sorted array a1[]. It gave me a result like this:
a1[] = { 5, 2, 14, 0 ,11 }
There is a slightly difference between my result and the model answer. I mean the order of the elements in a1[]. The model answer is
a1[] = {5, 11, 14, 0, 2}
Can you guys help me to figure out the problem?
int removeAll_unordered(int *a, int& n, const int *b, int m)
{
for (int i = 0; i < m; i++) {
int j = 0;
for (j = 0; j < n; j++)
{
if (b[i] == a[j])
{
a[j] = a[n - 1];
n -= 1;
}
}
}
return n;
}
If you write code in C++ you should use what standard library provides for you - in your case std::vector and std::remove_if algo:
void removeAll_unordered( std::vector<int> &a, const std::vector<int> &b )
{
auto end = std::remove_if( a.begin(), a.end(), [b]( int i ) {
return std::find( b.begin(), b.end(), i ) != b.end();
} );
a.erase( end, a.end() );
}
Live code 1
But this usage is very inefficient, so using standard library as well which provides std::unordered_set aka hash set we can easily make it optimized:
void removeAll_unordered( std::vector<int> &a, const std::vector<int> &b )
{
auto end = std::remove_if( a.begin(), a.end(),
[set = std::unordered_set<int>( b.begin(), b.end() )]( int i ) {
return set.count( i );
} );
a.erase( end, a.end() );
}
Live code 2
I found one problem in you code, I couldn't compile though, but it should work.
In your code,
if (b[i] == a[j])
{
a[j] = a[n - 1];
n -= 1;
}
When an element in b is found in a, you replace that value with a[n-1], this is okay, but that value was not compared with b[i] as j got incremented, So I correct this part. If you run with different inputs you will able to catch this problem.
int removeAll_unordered(int *a, int& n, const int *b, int m)
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n;)
{
if (a[j] == b[i]) // replace a[j] with a[n-1] and decrease n
{
a[j] = a[n - 1];
n--;
}
else
j++; // otherwise increase j
}
}
return n;
}
To get the exact answer (order of the elements in a after the removal)
here is the modified code:
int duplicates = 0; // counts total numbers that removed from a[]
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m;)
{
if (a[i] == b[j]) // replace a[j] with a[n-1] and decrease n
{
if (i == n - 1) // when we reach the last element of a that matches in b
{
n--; // updating length of a[]
duplicates++; // one more removed
break;
}
a[i] = a[n - 1];
n--; // updating length of a[]
duplicates++; // one more removed
j = 0;
}
else
j++; // otherwise increase j
}
}
return duplicates; // returned total removed numbers
I found the problem of your code. You change the variables in the for loops. First for loop use the 'n' variable for their maximum value and second for loop use 'm' value.
Then you only decreasing n value but you didn't check the new i th value. Because now i th value changed. So, you want check again that value also. For that you can decrease 'i' value also.
And also you mentioned above your answer is 5 not 4. Then it correct answer. Because you write this code thinking about 9 elements of the array. Not the 0 to 8. So, if you write this code thinking about 0 to 8 elements you can get whatever you want. If you want 4 you can decrease final value by one. Then you can get your value.
Modified cord given below.
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (b[j] == a[i])
{
a[i] = a[n - 1];
n -= 1;
i = i - 1;
}
}
}
return n;

Deleting an even number in an array and shift the elements

I'm trying to write a code where there is a research of even numbers and then it deletes the even numbers and then shifts all the other elements.
i is for offset and are the actual position of the elements in the array.
k is the position of the even number in the array.
int k;
for(i=0; i < N; i++)
{
if(Array[i] % 2 == 0)
{
for(k=i+1; k < N; k++)
{
Array[k-1] = Array[k];
}
N--;
}
}
Array=[2,10,3,5,8,7,3,3,7,10] the even numbers should be removed, but a 10
stays in the Array=[10,3,5,7,3,3,7].
Now is more than 3 hours that I'm trying to figure out what's wrong in my code.
This appears to be some sort of homework or school assignment. So what's the actual problem with the posted code?
It is that when you remove an even number at index i, you put the number that used to be at index i + 1 down into index i. Then you continue the outer loop iteration, which will check index i + 1, which is the number that was at the original i + 2 position in the array. So the number that started out at Array[i + 1], and is now in Array[i], is never checked.
A simple way to fix this is to decrement i when you decrement N.
Though already answered, I fail to see the reason people are driving this through a double for-loop, repetitively moving data over and over, with each reduction.
I completely concur with all the advice about using containers. Further, the algorithms solution doesn't require a container (you can use it on a native array), but containers still make it easier and cleaner. That said...
I described this algorithm in general-comment above. you don't need nested loops fr this. You need a read pointer and a write pointer. that's it.
#include <iostream>
size_t remove_even(int *arr, size_t n)
{
int *rptr = arr, *wptr = arr;
while (n-- > 0)
{
if (*rptr % 2 != 0)
*wptr++ = *rptr;
++rptr;
}
return (wptr - arr);
}
int main()
{
int arr[] = { 2,10,3,5,8,7,3,3,7,10 };
size_t n = remove_even(arr, sizeof arr / sizeof *arr);
for (size_t i=0; i<n; ++i)
std::cout << arr[i] << ' ';
std::cout << '\n';
}
Output
3 5 7 3 3 7
If you think it doesn't make a difference, I invite you to fill an array with a million random integers, then try both solutions (the nested-for-loop approach vs. what you see above).
Using std::remove_if on a native array.
Provided only for clarity, the code above basically does what the standard algorithm std::remove_if does. All we need do is provide iterators (the array offsets and size will work nicely), and know how to interpret the results.
#include <iostream>
#include <algorithm>
int main()
{
int arr[] = { 2,10,3,5,8,7,3,3,7,10 };
auto it = std::remove_if(std::begin(arr), std::end(arr),
[](int x){ return x%2 == 0; });
for (size_t i=0; i<(it - arr); ++i)
std::cout << arr[i] << ' ';
std::cout << '\n';
}
Same results.
The idiomatic solution in C++ would be to use a STL algorithm.
This example use a C-style array.
int Array[100] = {2,10,3,5,8,7,3,3,7,10};
int N = 10;
// our remove_if predicate
auto removeEvenExceptFirst10 = [first10 = true](int const& num) mutable {
if (num == 10 && first10) {
first10 = false;
return false;
}
return num % 2 == 0;
};
auto newN = std::remove_if(
std::begin(Array), std::begin(Array) + N,
removeEvenExceptFirst10
);
N = std::distance(std::begin(Array), newN);
Live demo
You could use a std::vector and the standard function std::erase_if + the vectors erase function to do this:
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> Array = {2, 10, 3, 5, 8, 7, 3, 3, 7, 10};
auto it = std::remove_if(
Array.begin(),
Array.end(),
[](int x) { return (x & 1) == 0 && x != 10; }
);
Array.erase(it, Array.end());
for(int x : Array) {
std::cout << x << "\n";
}
}
Output:
10
3
5
7
3
3
7
10
Edit: Doing it the hard way:
#include <iostream>
int main() {
int Array[] = {2, 10, 3, 5, 8, 7, 3, 3, 7, 10};
size_t N = sizeof(Array) / sizeof(int);
for(size_t i = 0; i < N;) {
if((Array[i] & 1) == 0 && Array[i] != 10) {
for(size_t k = i + 1; k < N; ++k) {
Array[k - 1] = Array[k];
}
--N;
} else
++i; // only step i if you didn't shift the other values down
}
for(size_t i = 0; i < N; ++i) {
std::cout << Array[i] << "\n";
}
}
Or simpler:
#include <iostream>
int main() {
int Array[] = {2, 10, 3, 5, 8, 7, 3, 3, 7, 10};
size_t N = sizeof(Array) / sizeof(int);
size_t k = 0;
for(size_t i = 0; i < N; ++i) {
if((Array[i] & 1) || Array[i] == 10) {
// step k after having saved this value
Array[k++] = Array[i];
}
}
N = k;
for(size_t i = 0; i < N; ++i) {
std::cout << Array[i] << "\n";
}
}

Algorithm for deleting multiple array elements and shifting array

I have an array let's say with 5 items, if element[i] is less than 3 need to move element[i+1] in place of element[i].
int array[5] = {4, 2, 3, 5, 1};
int number = 3;
for (int i = 0; i < number; i++)
{
if (array[i] > number)
{
for (int j = 0; j < i - 1; j++)
{
array[j] = array[j + 1];
}
number = number - 1;
}
}
expected result is array = {2, 3, 1, anyNumber, anyNumber};
A O(n) working code for the above problem.. But as others pointed out in the comments.. You end up with an array that is using less space then allocated to it..
#include<stdio.h>
int main()
{
int arr[] = {4, 2, 3, 5, 1};
int* temp1 = arr;
int* temp2 = arr;
int i, n1 = 5, n2 = 5;
for(i = 0; i < n1; i++)
{
if(*temp2 >= 3)
{
*temp1 = *temp2;
temp1++;
temp2++;
}
else
{
n2--; //the number of elements left in the array is denoted by n2
temp2++;
}
}
}
Nested loops give you O(n2) complexity, and non-obvious code.
Better use std::remove_if:
int array[5] = {4, 2, 3, 5, 1};
int number = 3;
remove_if( begin( array ), end( array ), [=]( int x ) { return x>number; } );
Disclaimer: code untouched by compiler's hands.
Try this code. You should not decrease number at each step. Also, the second loop should start at i and stop at the end of array:
int array[5] = {4, 2, 3, 5, 1};
int number = 3;
for (int i = 0; i < number; i++)
{
if (array[i] > number)
{
for (int j = i; j < 5; j++)
{
array[j] = array[j + 1];
}
}
}
Here's a more compact and idiomatic (that's how I view it anyway) way to remove items from an array:
#include <iostream>
#include <algorithm>
#include <iterator>
int main()
{
int array[] = {4, 2, 3, 5, 1};
int* begin = array;
int* end = begin + sizeof(array)/sizeof(array[0]);
int number = 3;
end = std::remove_if(begin, end, [&number](int v) {return v > number;});
std::copy(begin, end, std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
return 0;
}
Just for comparison, here's a version using std::vector:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
int main()
{
std::vector<int> array = {4, 2, 3, 5, 1};
int number = 3;
auto end = std::remove_if(array.begin(), array.end(), [&number](int v) {return v > number;});
std::copy(array.begin(), end, std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
return 0;
}
As an alternative, if you want to keep your items, but denote what will be at some later time, "removed", the algorithm that can be used is stable_partition:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <functional>
int main()
{
int vValues[] = {4,2,3,5,1};
// partition the values on left and right. The left side will have values
// <= 3, and on right >3. The return value is the partition point.
int *p = std::stable_partition(vValues, vValues + 5,
std::bind2nd(std::less_equal<int>(), 3));
// display information
std::cout << "Partition is located at vValues[" << std::distance(vValues, p) << "]\n";
std::copy(vValues, vValues + 5, std::ostream_iterator<int>(std::cout, " "));
}
Output:
Partition is located at vValues[3]
2 3 1 4 5
You will see that 2,3,1 are on the left of partition p, and 4,5 are on the right of the partition p. So the "removed" items start at where p points to. The std::partition ensures the elements are still in their relative order when done.
I created my own example, hope this helps people as a reference:
// Removing an element from the array. Thus, shifting to the left.
void remove(){
int a[] = {1, 2, 3, 4, 5, 6};
int size = sizeof(a)/sizeof(int); // gives the size
for(int i = 0; i < size; i++){
cout << "Value: " << a[i] << endl;
}
int index = 2; // desired index to be removed
for(int i = 0; i < size; i++){
if(i == index){
for(int j = i; j < size; j++){
a[j] = a[j+1];
}
}
}
size--; // decrease the size of the array
cout << "\nTesting output: " << endl;
for(int i = 0; i < size; i++){
cout << "Value: " << a[i] << endl;
}
}
int main(){
remove();
return 0;
}

Using pointers to iterate through multidimensional array

If you can use pointers to iterate through an array like this:
for (int *iter = arr; iter != std::end(arr); ++iter) {
// code
}
How do you iterate through a multidimensional array with pointers (without using auto)?
EDIT: I am assuming this is an int[][] such as {{3, 6, 8}, {2, 9, 3}, {4, 8, 2}}
If you declared array as arr[][], and yes you can because they are stored sequentially in memory. You can do:
for(int * iter = &arr[0][0]; iter != &arr[0][0] + col * row; iter++)
//...
How about trying like this:-
const int* d = data;
for ( int i = 0; i < width; ++i )
for ( int j = 0; j < height; ++j )
for ( int k = 0; k < depth; ++k )
sum += *d++;
Check this tutorial
For example:
constexpr size_t rowCnt = 3, colCnt = 4;
int ia[rowCnt][colCnt] = { // three elements; each element is an array of size 4
{0, 1, 2, 3}, // initializers for the row indexed by 0
{4, 5, 6, 7}, // initializers for the row indexed by 1
{8, 9, 10, 11} // initializers for the row indexed by 2
};
Not using a type alias for the type of the loop control variables:
// p points to the first array in ia
for (int (*p)[colCnt] = ia; p != ia + rowCnt; ++p) {
// q points to the first element of an array of four ints; that is, q points to an int
for (int *q = *p; q != *p + colCnt; ++q)
cout << *q << " ";
cout << endl;
}
or easier using auto:
for (auto p = ia; p != ia + rowCnt; ++p) {
// q points to the first element of an array of four ints; that is, q points to an int
for (auto q = *p; q != *p + colCnt; ++q)
cout << *q << " ";
cout << endl;
}
Improving texasbruce's answer(couldn't comment sorry) with sizeof trick, assuming array is statically allocated(dimensions are known at compile-time)
for(int * iter = &arr[0][0]; iter != &arr[0][0] + sizeof(arr)/sizeof(int); iter++)
or iter != (int*)((void*)&arr[0][0] + sizeof(arr)) if you're (void*) fan and hate any compile-time divisions
So you don't have to bother with array dimensions :)
Probably something like this
for (int **iter = arr; iter != std::end(arr); ++iter) {
for (int *iter2 = *iter; iter2 != std::end(*arr); ++iter2) {
// code
}
}
Assuming you are simply talking about statically declared multi-dimensional arrays:
const int ROWS = 10;
const int COLS = 20;
const int DEPTH = 30;
int array[ROWS][COLS][DEPTH]; // statically allocated array
int i = 0;
for (int row = 0; row < ROWS; ++row)
{
for (int col = 0; col < COLS; ++col)
{
for (int depth = 0; depth < DEPTH; ++depth)
{
*(*(*(array + row) + col) + depth) = i++; // do whatever with it
// is equivalent to array[row][col][depth] = i++;
}
}
}
If you need more levels, you just keep adding levels of pointer indirection.
Alternatively:
const int ROWS = 5;
const int COLS = 6;
const int DEPTH = 3;
int array[ROWS][COLS][DEPTH]; // statically allocated array
int* p = &array[0][0][0];
int c = 0;
for (int i = 0; i < ROWS * COLS * DEPTH; ++i, p++)
{
*p = c++;
}
Since a statically declared array is going to be contiguous in memory, with the first element (array[0][0][0]) starting at the base address (&array), this works.
Declaring a multi-dimensional array dynamically is going to be a pointer to an array of pointers to (etc) to pointers to an array of object_type. You can simplify that by using std::vector or std::array (if you know the size at compile time).
Not that these use pointers to iterate (at least not directly), but
Vector/Array
std::vector<std::vector<std::vector<int> > > array;
// or
//std::array<std::array<std::array<int, DEPTH>, COLS>, ROWS> array; // with some slight modifications
// fill in the vectors/arrays however you want
std::for_each(array.begin(), array.end(), [](const std::vector<std::vector<int> >& v)
{
std::for_each(v.begin(), v.end(), [](const std::vector<int>& a)
{
std::for_each(a.begin(), a.end(), [](int i)
{
std::cout << i << endl;
});
});
});

Linear Search returning array with indices value is found at

I attempted a program to return an array with the indicies of the array where a specific inputed value is found, but every run results in an error, which seems to be an infinite run time. The error seems to be occuring right after printing out the last of the indicies found.
Can anyone help?
(Side note: I've seen multiple pages about deleting pointers when done with them; should I be doing that here?)
Forgot to mention - I want the first slot of the returned array to save the size of the array, so that it can be accessed easily later on in the program
#include <iostream>
#include <vector>
using namespace std;
int* linearSearch(int* n, int k, int f) {
// Input: Index 0 Address ; Size of Array; Element to Search
// Output: Array of Found Indicies
vector <int> a;
int* b;
for(int i = 0; i < k; i++)
if(n[i] == f)
a.push_back(i);
*b = a.size();
for(int i = 0; i < a.size(); i++)
b[i + 1] = a[i];
return b;
}
int main() {
int c[10] = {4, 4, 6, 3, 7, 7, 3, 6, 2, 0};
int* k = linearSearch(&c[0], sizeof(c)/sizeof(int), 4);
for(int i = 0; i < k[0]; i++) {
cout << "Found at index: " << k[i + 1] << endl;
}
return 0;
}
int* b;
....
*b = a.size();
b has to be allocated. Try following:
int* b = new int[a.size() + 1];
b[0] = a.size();
I see what you meant. b will have magically length in first element. This was in Pascal/Delphi but not the case in C/C++.
You are writing to heap memory that you never claimed.
int* b;
This pointer, having never been initialized, points to an undefined memory address. Then when you use the indexing operator to assign your matches, you are writing to the subsequent bytes following the undefined memory address.
You need to allocate space for storing the results using the 'new[]' operator. Additionally, if you had correctly claimed the memory, you would be assigning the number of match results to the first element in the result array - something that doesn't seem to be your intention.
Take a look at dynamic memory allocation in C++ using the new [] operator.
If you use std::vector anyway, why not to use it where it is needed the most? Also if you not suppose to modify array by that pointer express that by const pointer:
std::vector<int> linearSearch(const int* n, int k, int f)
{
std::vector<int> res;
for(int i = 0; i < k; i++)
if(n[i] == f) res.push_back(i);
return res;
}
int main() {
int c[10] = {4, 4, 6, 3, 7, 7, 3, 6, 2, 0};
std::vector<int> k = linearSearch(&c[0], sizeof(c)/sizeof(int), 4);
for(int i = 0; i < k.size(); i++) {
cout << "Found at index: " << k[i] << endl;
}
return 0;
}
This is not perfect but this is much closer to a correct implementation and you should be able to take it further with some work:
#include <iostream>
#include <vector>
using namespace std;
std::vector<int> linearSearch(int* n, int k, int f)
{
vector <int> a;
for(int i = 0; i < k; i++)
{
if(n[i] == f)
{
a.push_back(i);
}
}
return a ;
}
int main() {
int c[10] = {4, 4, 6, 3, 7, 7, 3, 6, 2, 0};
std::vector<int> result = linearSearch(&c[0], sizeof(c)/sizeof(int), 4);
for(unsigned int i = 0; i < result.size(); i++)
{
cout << "Found at index: " << result[i + 1] << endl;
}
return 0;
}