tags:

views:

171

answers:

4

Hello! I have a problem to do. I already did some part of it, however I stuck and don't know exactly what to do next.

The question: " You are given two arrays of ints, named A and B. One contains AMAXELEMENTS and the other contains BMAXELEMENTS. Write a Boolean-valued function that returns true if there is at least one point in A that is the same as a point in B, and false if there is no match between two arrays. "

The two arrays are made up by me, I think if I know how to compare two arrays I will be fine, and I will be able to finish my problem.

This is what I have so far (I changed AMAXELEMENTS to AMAX, and BMAXELEMENTS to BMAX):

#include <iostream>
using namespace std;

int main()
{
    const int AMAX = 5, BMAX = 6;
    int i;
    bool c1 = true, c2 = false;
    int A[AMAX] = { 2, 4, 1, 5, 9 };
    int B[BMAX] = { 9, 12, 32, 43, 23, 11 };

    for (i = 0; i < BMAX; i++)
        if (B[i] == A[i])       //  <---- I think this part has to look
                                //        different, but I can't figure it out.
            cout << c1 << endl;
        else
            cout << c2 << endl;
    return 0;
}
+4  A: 

This looks like homework so I'm not going to feed you the answer. But I will point out some basic issues.

First off, you be trying to define a function that returns true/false:

bool has_identical_value(int A[], size_t A_MAX, int B[], size_t B_MAX)
{
    bool answer = false;

    ... your homework goes here ...

    return answer;
}

Second is to make sure you never reference an A[i] where i >= A_MAX and you never reference B[j] where j >= B_MAX.

R Samuel Klatchko
It's actually a sample exam problem, I am trying to do it as my practice.
Martin
+2  A: 

You should loop over the 2 arrays, not over 1 array to compare. Else you will compare 2 with 9, 4 with 12 etc

Here some pointing in the right direction:

for(i=0;i<BMAX;i++)
{
    for (j=0;j<AMAX;j++)
    {
        if (B[i]==A[j])
        {
        }
        else
        {
        }
    }
}

You should wrap it in a function as your question states.

PoweRoy
A: 

First of all you have to write a function. Then learn about functions because it looks like currently you have no clue what function is.

And here's the hint, what your loop is doing wrong?

for(i=0;i<BMAX;i++)
   if (B[i]==A[i]) //A[BMAX - 1]???
doc
No need for sarcasm (I did not ding you)
Romain Hippeau
@Romain Hippeau ????????
doc
@doc - I just saw the phrase "you have no clue" as being a little sarcastic.
Romain Hippeau
@Romain Hippeau: it wasn't my intention to be sarcastic, rather a bit harsh (I guess you know why).
doc
So I did some studying, and this is how I end up:#include <iostream> using namespace std; int main (){ const int SIZE=10; int A[]={4,32,9,7,14,12,13,17,19,18}; double sum, avg, max; int i; max=A[0]; for(i=0;i<SIZE;i++){ sum+=A[i]; if(A[i]>max){ max=A[i];} } avg=sum/10; if(2*avg<=max) cout<<"OUTLIER= "<<max<<endl; else cout<<"OK"<<endl; return 0; }
Martin
A: 

MY answer again... after couple of hours of working with this :) Thanks for help!

include

using namespace std;

bool myBool(int, int, int[], int[]);

int main(){

  const int SIZEA=5;
  const int SIZEB=4;
 int A[]={54,65,76,67,4};
  int B[]={23,435,67,5};

  if (myBool(SIZEA, SIZEB,A,B))
     cout<<"TRUE"<<endl;

  else
     cout<<"FALSE"<<endl;
  return 0;

}

bool myBool(int AMAXELEMENTS, int BMAXELEMENTS, int A[], int B[]){
  int i,j;

  for(i=0;i<AMAXELEMENTS;i++){
     for(j=0;j<BMAXELEMENTS;j++){
        if(A[i]==B[j])
           return true;       
     }
  } 
  return false;  

}

Martin