How to compare array values and put them in another array

Hey guys,

I'm having some problems with this exercise: I have two arrays A and B. Using for loop, I need to check what elements are greater than variable x=3 and put those values into third array C. I managed to write this code:

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  #include <iostream>

using namespace std;

int main()
{
    double A[100];
    double B[100];
    double C[200];
    double n, m;
    const double x = 3;

    cout << "Type how many elements will be in array A\n";

    cin >> n;

    cout << "Type the elements of array A:\n";

    for(int i=0; i<n; i++)
    {
        cin >> A[i];
    }

    cout << "Type how many elements will be in array B\n";

    cin >> m;

    cout << "Type the elements of array B:\n";

    for(int i=0; i<m; i++)
    {
        cin >> B[i];
    }

    for(int i=0; i<n; i++)
    {
        if(A[i] > c)
        {
            C[i] = A[i];
            cout << C[i];
        }
    }

    for(int i=0; i<m; i++)
    {
        if(B[i] > c)
        {
            C[i] = B[i];
            cout << C[i];
        }
    }

    return 0;
}


You see, the problem is that I don't know how to make program print "The array C is empty" if there are no greater values in arrays.
(1) What if n and m are entered as greater than 100?

(2) In your if-statements, I think you meant to use 'x' and NOT 'c'

(3) You do realize that you are not actually filling up your C array normally? Depending on what numbers the user enters, some elements may have valid values and others in between may just be garbage numbers. Values may also be replaced once the B array is being checked. Make another for-loop and cout your C array and you'll see.

(4)
You see, the problem is that I don't know how to make program print "The array C is empty" if there are no greater values in arrays.


You can make else-statements to go along with each if-statement which will contain counters that increment. Then make a separate if-statement at the end asking if 'counter1' and 'counter2' are equal to n and m respectively. If yes, cout 'The Array is empty".

Although technically, arrays are never empty, but I get what you mean.
Last edited on
(1) The exercise tells there can't be more than 100 elements in each A and B arrays.

(2) Yeah, at first I declared that variable as c and just forgot to correct it.

(3) I understand what you mean. However I don't know how to fill array C 'correctly'.

(4) If I do the else statement like this, it prints 'The array C is empty' everytime there is at least 1 element which is not greater than x. And it's wrong, because it should only print 'The array C is empty' when all the elements are lower than x = 3.

1
2
3
4
5
6
7
8
9
10
11
12
for(int i=0; i<n; i++)
    {
        if(A[i] > x)
        {
            C[i] = A[i];
            cout << C[i];
        }
        else
        {
            cout << "The array C is empty";
        }
    }


Any Ideas of how I can correct this program?
You don't know whether C is going to be empty until after the loop is completed, so checking for that condition inside the loop is kind of... silly.

1
2
3
4
5
6
7
8
9
10
11
12
    unsigned c_idx = 0;
    for(unsigned a_idx=0; a_idx<n; ++a_idx)
    {
        if (A[a_idx] > x)
        {
            C[c_idx] = A[a_idx];
            ++c_idx;
        }
    }

    if (c_idx == 0)
        cout << "C is empty.\n";


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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  #include <iostream>

using namespace std;

int main()
{
    double A[100];
    double B[100];
    double C[200];
    double n, m;
    const double x = 3;

    cout << "Type how many elements will be in array A\n";

    cin >> n;

    cout << "Type the elements of array A:\n";

    for(int i=0; i<n; i++)
    {
        cin >> A[i];
    }

    cout << "Type how many elements will be in array B\n";

    cin >> m;

    cout << "Type the elements of array B:\n";

    for(int i=0; i<m; i++)
    {
        cin >> B[i];
    }

    for(int i=0; i<n; i++)
    {
        if(A[i] > c)
        {
            C[i] = A[i];
            cout << C[i];
        }
    }

    for(int i=0; i<m; i++)
    {
        if(B[i] > c)
        {
            C[i] = B[i];
            cout << C[i];
        }
    }

    return 0;
}


Ok, this program runs well with any kind of data, except if I choose A[i] and B[i] to be less than x=3. In this case, my program should print 'There are no elements from arrays A and B that are greater than x=3. Array C is empty". The problem that I don't know where and how should I write it in my program, because I have two for loops. And I cant create if statement outside the loop, because there will be an error for not understanding what is i. Hope you understand what I mean
And I cant create if statement outside the loop, because there will be an error for not understanding what is i. Hope you understand what I mean

I hope you understand that I used more than one index variable, because one is not sufficient for the task you must accomplish.
closed account (48T7M4Gy)
1. declare/initialise array C
2. declare bool Empty = true
3. on the first/any 'transfer' of a number from A or B to C set Empty = false
4. finally test Empty == true or false
Last edited on
Cire,

Now I understand. However if array A has 1 element with a value of 2 and array B has 1 element with a value of 4, it doesn't print answer well. It shows a value of 4 and it prints C is empty, which is not right, because it should only print 4.
closed account (48T7M4Gy)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  #include <iostream>

using namespace std;

int main()
{
    double A[100];
    double B[100];
    double C[200]={0};// <---
    
    bool Empty = true;
    
    double n, m;
    const double c = 3;

    cout << "Type how many elements will be in array A\n";

    cin >> n;

    cout << "Type the elements of array A:\n";

    for(int i=0; i<n; i++)
    {
        cin >> A[i];
    }

    cout << "Type how many elements will be in array B\n";

    cin >> m;

    cout << "Type the elements of array B:\n";

    for(int i=0; i<m; i++)
    {
        cin >> B[i];
    }

    for(int i=0; i<n; i++)
    {
        if(A[i] > c)
        {
            Empty = false;
            C[i] = A[i];
            cout << C[i];
        }
    }

    for(int i=0; i<m; i++)
    {
        if(B[i] > c)
        {
            Empty = false;
            C[i] = B[i];
            cout << C[i];
        }
    }
    
    if (Empty == true)
        cout << "There are no elements from arrays A and B that are greater than x=3. Array C is empty";

    return 0;
}


Type how many elements will be in array A
3
Type the elements of array A:
1
1
2
Type how many elements will be in array B
4
Type the elements of array B:
1
2
2
1
There are no elements from arrays A and B that are greater than x=3. Array C is empty
Exit code: 0 (normal program termination)
Last edited on
Wow, this kind of solution is interesting. Maybe you can explain how this works?

bool Empty = true;

Empty = false
Torex wrote:
Now I understand. However if array A has 1 element with a value of 2 and array B has 1 element with a value of 4, it doesn't print answer well. It shows a value of 4 and it prints C is empty, which is not right, because it should only print 4.

I can't comment on your code that I cannot see. ;)

Torex wrote:
Wow, this kind of solution is interesting.

Note the solution doesn't leave you with any way to determine what values in C were placed there by your code and what values are just uninitialized garbage. An explicit flag isn't required if you keep a count of the elements added to C (which doubles as an index while you're adding to it.)


closed account (48T7M4Gy)
Well I don't want to steal cires thunder but what I did was implement the steps 1 to 4 I listed above. And initialising array C overcomes the count issue - if a value is zero it can't possibly be a 'transferee'.

I assume you know bool is a/the Boolean data type that takes one of only two values that equate to true or false.

So C starts off empty, i.e. the variable I named Empty is set at being true. The later two lines trap any transfer to C and so Empty is then set to false.

Since there is no statement to set Empty back to true, it remains false from then on. If it's true at the end then no transfer could have occurred so emptiness follows.
Last edited on
Topic archived. No new replies allowed.