user7359095
user7359095

Reputation:

Comparing one element of an array with whole another array elements

First, I created 2 arrays. Call them A[] and B[].

Array A has elements A = { 11, 16, 21 }

Array B has elements B = { 12, 7, 18, 24, 101 }

I made a loop. Which compares both.

for (int i = 0; i<Acount; ++i) {
    for (int j = 0; j < Bcount; ++j) {
        if (A[i] <= B[j]) {
            total = B[j] + total;
            break;
        }
    }
}

This one compares them and if A[] element is smaller than any B[] element, loop stops and adds up B's value to the total at that point and stops comparing.

Now, if A's every element are bigger than B, for example: A = { 11 } and B = { 3, 5 } it should say ERROR and shouldn't show any value (as variable total), but whatever I tried it's always printing either lots of "ERROR" text or the value or even both at the same time.

What do I do?

Edit: To clarify;

Example input:

3 5 //these sets the sizes of A and B, respectively.
11
16
21  //these are for A.
12
7
18
24
101 //these are for B.

The program should do: Hey, it's cool there's 11. Let me compare 11 from A with 12,7,18,24,101 from B and see if there's anything bigger than me. There's 12! That's nice. Let's add 12 to the total. Now, I have 16, let's do the same comparison with 12,7,18,24,101. 12 is smaller than me... so I should skip it. 7 as well... 18 is bigger than me! Let's add 18 to the total as well! Now for 21... same comparison same stuff. There's 24. Add up all and print the result.

Now, there's another example input:

1 2 //these sets the size of A and B, respectively.
10 // this is for A
3
5 // these are for B

Code should do the very same, but this time... I have 10, let's see if anything is bigger than me. Is 10 bigger than 3? No? Is 5 bigger than 10? Obviously not. So... there's nothing bigger than me... I should print an ERROR.

I hope I made it simpler. Cheers.

Edit 2: Here is my full code. Try it yourself with the example inputs I provided above and tell me what's wrong. First input should've printed an actual number result but prints ERROR as an example.

#include <iostream>
#include <conio.h>
using namespace std;

int main() {
    int amountA, amountB, i = 0 ,j = 0, total = 0;
    int A[20000];
    int B[20000];
    cin >> amountA >> amountB;
    for (i = 0; i < amountA; i++) {
        cin >> A[i];
    }
    for (j = 0; j < amountB; j++) {
        cin >> B[j];
    }

    for (int i = 0; i < amountA; ++i) {
        for (int j = 0; j < amountB; ++j) {
            if (A[i] <= B[j]) {
                total = B[j] + total;
                break;
            } else {
                cout << "ERROR!" << endl;
                return 0;
            }
        }
    }
    cout << total << endl;
    _getch();
}

Upvotes: 2

Views: 1548

Answers (2)

GhostCat
GhostCat

Reputation: 140427

As this is possible some homework; just some thoughts to get you going:

You need some boolean marker variable that indicates: "A values are all bigger than B values".

Initially, that thing is true.

And within your if-then case, you simply change it to false.

And when you then check it after the loop: if the marker is still true; you know that all As are actually >= than the Bs. If the marker is false, there was at least one A smaller ...

Your problem with your current code is simply: you have a return statement in your else block. This means: you end your whole program when you get into that condition!

As said; you need a marker:

bool hitError = false;

for (int i = 0; i< amountA; ++i) 
{
  if (hitError) {
  break;
  }

  for (int j = 0; j < amountB; ++j)
  {
    if (A[i] <= B[j])
    {
        total = B[j] + total;
        break;
    }
    else
    {
        cout << "ERROR!" << endl;
        hitError = true;
        break;
    }
  }
}

Upvotes: 0

Razvan Alex
Razvan Alex

Reputation: 1802

This could get fixed with an if-else statement. If it's smaller execute the sum and the break, but if it's bigger you can set an else condition. There's an example below ;)

for (int i = 0; i<Acount; ++i)
{
    for (int j = 0; j < Bcount; ++j)
    {
        if (A[i] <= B[j])
        {
            total = B[j] + total;
            break;
        } else {
           std::cout<<"Error"<<std::endl;
        }
    }
}

If you still can't figure it out, below is the solution ;)

Spoiler solution

Upvotes: 1

Related Questions