change the numbers in arrays

there are two arrays, change the numbers of the second array which are already in the first to 0

i have the code but in only works if the numbers are in the same column, but i need it to work any way, what do i need to do for that?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

int main()
{
    int arr1[4];
    cout << "Enter members of the first array: " << endl;
    for (unsigned i = 0; i != 4; i++)
        cin >> arr1[i];

    int arr2[4];
    cout << "Enter members of the second array: " << endl;
    for (unsigned j = 0; j != 4; j++)
        cin >> arr2[j];

    for (unsigned k = 0; k != 4; k++)
    {
        if (arr2[k] == arr1[k])
            arr2[k] = 0;
    }

    cout << "Members of the second array now are: " << endl;
    for (unsigned l = 0; l != 4; l++)
        cout << arr2[l] << " ";
    cout << endl;
    return 0;
}
Have a nested pair of for loops (outer iterating on i, inner iterating on j), and compare each arr1[i] to arr2[j]. If there is ever a match, change arr2[j] to 0.

1
2
3
4
5
6
7
for (int i = 0; i < 4; i++)
{
    for (int j = 0; j < 4; j++)
    {
        // ... compare and set ...
    }
}


Also, I did the same mistake when I was new: You don't need to rename your iteration variable each time. Just keep it as i (or i and j, if nested). You only need the change the iteration variable if the loop is nested. This way you can avoid the temptation to use horrible variable names like lowercase L.
Last edited on
the simple way is to check every item against every other item, which needs a double for loop.
for i everything in first
for j everything in second
if first[i] == second[j]
...

which checks 0,0, 0,1, 0,2, 0,3, 1,0, 1,1,1,2,1,3, ... 3,3
instead of what you have now (0,0, 1,1, 2,2, 3,3)

if the arrays were huge instead of things like 4, you would need a better idea. But with small arrays, this is fine.
thank you!!
@Ganado is this what you meant? :D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using namespace std;

int main()
{
    setlocale(LC_ALL, "Russian");

        int arr1[4];
        cout << "Введите элементы первого массива(4): " << endl; // elements of the first array
        for (int i = 0; i != 4; i++)
            cin >> arr1[i];

        int arr2[4];
        cout << "Введите элементы вторoго массива(4): " << endl; // of the second
        for (int i = 0; i != 4; i++)
            cin >> arr2[i];

        for (int i = 0;i != 4;++i)
        {
            for (int j = 0;j != 4;j++)
            {
                if (arr1[i] == arr2[j])
                    arr2[j] = 0;
            }
        }
            cout << "Элементы второго массива теперь: " << endl; //elements of the second are now
            for (int i = 0; i != 4; i++)
                cout << arr2[i] << " ";
            cout << endl;
            return 0;
}
Last edited on
An alternative is to use range-for and the std::replace_if()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	const size_t arrSize {4};

	int arr1[arrSize] {};

	cout << "Enter members of the first array: ";

	for (int& a : arr1)
		cin >> a;

	int arr2[arrSize] {};

	cout << "Enter members of the second array: ";

	for (int& a : arr2)
		cin >> a;

	for (int a : arr1)
		std::replace_if(arr2, arr2 + arrSize, [a](int n) {return a == n; }, 0);

	cout << "Members of the second array now are: \n";

	for (int a : arr2)
		cout << a << " ";

	cout << '\n';
}



Enter members of the first array: 1 2 3 4
Enter members of the second array: 5 7 8 2
Members of the second array now are:
5 7 8 0


Last edited on
@seeplus thank you so much! :D
Topic archived. No new replies allowed.