开发者

C++ Boolean problem (comparison between two arrays)

开发者 https://www.devze.com 2022-12-30 00:39 出处:网络
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.

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 =开发者_StackOverflow中文版 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;
}


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.


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.


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]???


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;  

}

0

精彩评论

暂无评论...
验证码 换一张
取 消