Question

The aim of this exercise is to check the presence of a number in an array....

The aim of this exercise is to check the presence of a number in an array.

Specifications:

• The items are integers arranged in ascending order.

• The array can contain up to 1 million items.

• The array is never NULL.

Implement the method bool Answer::exists(int ints[], int size, int k) so that it returns true if k belongs to ints, otherwise the method should return false. size contains the size of ints.

Important note: Try to save CPU cycles if possible.

Example:

int ints[] = {-9, 14, 37, 102);

Answer::exists(ints, 4, 102) returns true

Answer::exists(ints, 4, 36) returns false

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code in C++,

since array's element in ascending order then I can easily use binary search

bool exists(int ints[],int size,int k){

return binary_search(ints+0,ints+size,k)

}

binary_search() return true if k exists in array else false

if you want in any other language please let me know

Add a comment
Know the answer?
Add Answer to:
The aim of this exercise is to check the presence of a number in an array....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • c++ please. Write a recursive method that takes the following parameters: * a sorted array of...

    c++ please. Write a recursive method that takes the following parameters: * a sorted array of ints * an int representing the size of the array * an int representing a value to be searched for Your function should return a bool. If the element is in the array, return true. Otherwise, return false. Your function should never produce memory access errors, regardless of the size of the array.

  • using c++ Write a function that returns true if a given integer array is sorted(in ascending...

    using c++ Write a function that returns true if a given integer array is sorted(in ascending order) and false if it is not. The function should perform no more than "size" comparisons. bool isSorted(int A[], int size);

  • TestScores . SIZE: int //size of the array scores: int [ 1 estScores findMax ( )...

    TestScores . SIZE: int //size of the array scores: int [ 1 estScores findMax ( ) : int findMin ) int findScore (value: int): bool res(): int Write the constructor method. It fills the array with random number from 0-100. Find below the code to get your started 1. public TestScores () Random rand -new Random); for (int i-0 i<SIZE i+) socresi] -rand.nextInt (101); Finding the maximum value in an array. Write the method findMax. It returns the maximum value...

  • QUESTION: ADT stack: resizable array-based implementation    for Ch4 programming problem 4 "maintain the stacks's top...

    QUESTION: ADT stack: resizable array-based implementation    for Ch4 programming problem 4 "maintain the stacks's top entry at the end of the array" at array index N-1 where the array is currently allocated to hold up to N entries. MAKE SURE YOU IMPLEMENT the functions:  bool isEmpty() const; bool push(const ItemType& newEntry); bool pop(); in ArrayStackP4.cpp //FILE StackInterface.h #ifndef STACK_INTERFACE_ #define STACK_INTERFACE_ template<class ItemType> class StackInterface { public:    /** Sees whether this stack is empty.    @return True if the...

  • The exists function returns whether a given value exists in the given array or not. It...

    The exists function returns whether a given value exists in the given array or not. It returns 1 (true) if the value exists in the given array, and returns 0 (false) otherwise. The first function parameter is the value searched; the second parameter is the array, the third one is the array size. Fill in the blanks by dragging and dropping the items below into the code in their suitable places. Notice that some of the items are distractors and...

  • Problem 1 1. Consider the following function (K is the size of array A and L...

    Problem 1 1. Consider the following function (K is the size of array A and L is the size of array B) bool func (int A[], int K, int B[], int L, int start) { if (L > K-start) return false; for (int i =0; i < L; i++) { if(A[start+i] != B[i]) return false } return true; } What are the input and output variables to this function. Trace it for the following call: int F[] = {1, 3,...

  • A method stub, or signature, is the first line of a method. Write a class that...

    A method stub, or signature, is the first line of a method. Write a class that contains the following “stubs” for methods used in measurement conversion. You do not have to implement the methods. Correct response: public void showMeters(int numMillimeters){} Write a method that takes two integers and displays their sum. Write a method that takes five doubles and returns their average. Write a method that returns the sum of two randomly generated integers. Write a method that takes three...

  • Please help JAVA Create the following in Stack.java: An array of ints set to size 10...

    Please help JAVA Create the following in Stack.java: An array of ints set to size 10 to represent the Stack, an int variable for the index A constructor that sets the index to -1 A method called isEmpty (returns a boolean) that has no parameters. This method returns true if the Stack is empty and false if the Stack is not empty. true A method called push(returns a Boolean) that takes an int as a parameter. This method pushes, or...

  • I need to implement raw array Stack for create empty stack, isEmpty, isFull, push, pop, and...

    I need to implement raw array Stack for create empty stack, isEmpty, isFull, push, pop, and size using below pseudo code. I need two classes with stack pseudo code implementation and the main method to demonstrate the correct working of each operation. pseudo code StackADT (using raw array) class StackADT { int top int items[] int max StackADT(int n) Initialize array to n capacity top = 0 max = n boolean isEmpty() if array has no elements return true else...

  • Can someone help me with the main class of my code. Here is the assignment notes....

    Can someone help me with the main class of my code. Here is the assignment notes. Implement project assignment �1� at the end of chapter 18 on page 545 in the textbook. Use the definition shown below for the "jumpSearch" method of the SkipSearch class. Notice that the method is delcared static. Therefore, you do not need to create a new instance of the object before the method is called. Simply use "SkipSearch.jumpSearch(...)" with the appropriate 4 parameter values. When...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT