0

I want to create the function, that takes an input of two arrays of the same size and delete all elements from the second array, that are multiples of the element(s) of the first array.

Here is my attempt:

#include <iostream>
#include <vector>
#include <string>
#include <stdexcept>
#include <algorithm>
#include <cmath>
#include <list>
#include <algorithm>
using namespace std;


template<typename T, size_t N>
int multipl(T(&one)[N],T(&two)[N]){
    for(int i = 0;i<N;++i){
        for(int j = 0;j<N;++j){
            if(two[j]%one[i] == 0 && one[i] !=1 && one[i] != two[j]){
                two[j] = -1;   
            }
        }
    }

}





int main(){
    int one[] = {2,5,2,5,5,11};
    int two[] = {4,8,1,3,2,10};
    multipl(one,two);
}

Basically I substituted all elements, that are multiples of the elements from first array by -1. Right noew I want to delete all elements from the array two, which are equal to -1. How can I do it?

With vectors it would be easier, but I want to try it with arrays in order to better understand them.

Rob_Fir
  • 155
  • 1
  • 7
  • 1
    Arrays have fixed sizes. You cannot add or remove elements from them. The best you can do with arrays is to define an empty or neutral element like `0`, `-1` or `std::numeric_limits::max()` and make all users understand that. – nwp Jan 22 '18 at 13:19
  • you cant delete elements in an array like a node in linked list, your best bet is to copy the elements that are not `-1` into a new array – Albin Paul Jan 22 '18 at 13:24
  • @Rob_Fir Use the standard algorithm std::remove and keep the position of the first "free" element. – Vlad from Moscow Jan 22 '18 at 13:25
  • But what to do with the last element? – Rob_Fir Jan 22 '18 at 13:42

0 Answers0