Check if element found in array c

0 votes

How can I see if the element I'm looking for is present in my array?

I would carry out the following in Java:

Foo someObject = new Foo(someParameter);
Foo foo;
//search through Foo[] arr
for(int i = 0; i < arr.length; i++){
  if arr[i].equals(someObject)
    foo = arr[i];
}
if (foo == null)
  System.out.println("Not found!");
else
  System.out.println("Found!");

What would be the C++ solution, though, since I don't believe I'm allowed to search if an Object is null in C++?

Nov 17, 2022 in C++ by Ashwini
• 5,430 points
849 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes
float arr[] = ...;

declares an array(because of []) of floats(because of the float keyword) that is called arr. Similarly in a function declaration:

int valueinarray(float val, float *arr[]);

means that the second argument is a pointer(because of *) to an array(because of[]) which isn't what you need at all. You need to accept just an array:

int valueinarray(float val, float arr[]);

Following this logic your code would look like this:

int valueinarray(float val, float arr[])
{
    int i;
    for(i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
    {
        if(arr[i] == val)
            return 1;
    }
    return 0;
}

Notice a number of changes:

  1. The function parameter arr is now an array.

  2. The expression

    (arr) / (sizeof (arr[0]) initially determines the array's size in bytes. then divides it by the first element's size, expressed in bytes. The number of elements is all that is left at this point. Since each character takes up one byte in a char array, this would not be necessary. Any other type will require it. In addition, you might have divided by sizeof(float), but doing so makes it more difficult for you to switch types and is a bad idea (arr[0]).

    You could have an array of 10 floats if you didn't split, but you could try to access 40. As a result, you would be tampering with 120 bytes of memory that belong to someone else right after your array.

int a[5];
int x = 5;
int *pointerToInt = &x;

then x is a label for some memory (variable) that stores the value 5. &x is a pointer to the memory that stores the label. The opposite of & is *. Whlie pointerToInt equals &x, *pointerToInt equals x. Also *a equals a[0], but isn't literally the same, *(a+1) equals a[1].

answered Nov 24, 2022 by Tejashwini
• 3,820 points

edited Mar 5

Related Questions In C++

0 votes
1 answer

Check if element is in the list (contains)

The simplest and quickest method. You could also ...READ MORE

answered Jun 2, 2022 in C++ by Damon
• 4,960 points
2,316 views
0 votes
0 answers

Check if a string contains a string in C++

I've got a std::string variable. I'd want ...READ MORE

Jul 28, 2022 in C++ by Nicholas
• 7,760 points
709 views
0 votes
1 answer

How to use std::sort to sort an array in C++

We receive std::begin and std::end in C++0x/11, which are overloaded for arrays: #include <algorithm> int main(){ int v[2000]; ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
1,315 views
0 votes
1 answer

How to convert string to char array in C++?

Simplest way I can think of doing ...READ MORE

answered Jun 20, 2022 in C++ by Damon
• 4,960 points
5,904 views
0 votes
1 answer

Is 'If Else' statement indentation important or not in C++? [duplicate]

White space has no effect on the understanding of code in C and C++.  That is not to say that the programmer should be unconcerned about its misuse. The easiest method to demonstrate what the above code truly represents is to specify all of the inferred braces directly, as seen below.  The 'if then' or 'otherwise' clause only affects one line of code in the if statement with no brackets. This is one of the reasons why people strive to insist on 'proper coding standards' to guarantee that other people can clearly grasp the programmer's flow and meaning. while(c != cols) { ...READ MORE

answered Jun 27, 2022 in C++ by Damon
• 4,960 points
655 views
0 votes
0 answers

How to find if a given key exists in a C++ std::map

I'm trying to check if a given ...READ MORE

Jul 14, 2022 in C++ by Nicholas
• 7,760 points
856 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,020 points
792 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 58,020 points
2,541 views
0 votes
1 answer

How to pass large records to map/reduce tasks?

Hadoop is not designed for records about ...READ MORE

answered Sep 25, 2018 in Big Data Hadoop by Frankie
• 9,830 points
1,560 views
0 votes
1 answer

Invalid method parameters for eth_sendTransaction

params needs to be an array, try {"jsonrpc":"2.0","method":"eth_se ...READ MORE

answered Sep 28, 2018 in Blockchain by digger
• 26,740 points
2,017 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP