Question

question about c++ find function int num_to_id(int num) { static const int table[] = { 10,...

question about c++ find function

int num_to_id(int num) { static const int table[] = { 10, 15, 30, 50 }: assert(std::find(&table[0], &table[4], num) != &table[4]); return std::find(&table[0], &table[4], num) - &table[0]: } why do you need &table[0] and [4] and have to extract &table[0] from them ??

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

int num_to_id(int num) {
static const int table[] = { 10, 15, 30, 50 };
assert(std::find(&table[0], &table[4], num) != &table[4]);
return std::find(&table[0], &table[4], num);
}

Explanation:- We need &table[0] and &table[4] because here in the num_to_id function
we have to find the index of the element in the array table[] which
is equal to num. To find the index we use find() function that take
3 parameter first two are the range in which the search will done with
first parameter inclusive and second parameter exclusive that means if we
write &table[0] to &table[4] Then the search will start from element with
index 0 to element with index 3 (index 4 exclude).
  
We extract &table[0] from them because the function num_to_id must return
the index of the element in the array table[] which is equal to num.
when find() function is called it return the iterator pointing the first
element that matches with num. And for getting the index that is pointed by
iterator the it must be substrated from the reference point, Here the
reference point is &table[0] because we started the searching from this points.

Add a comment
Know the answer?
Add Answer to:
question about c++ find function int num_to_id(int num) { static const int table[] = { 10,...
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
  • int num_to_id(int num) { static const int table[] = { 10, 15, 30, 50 }: assert(std::find(&table[0],...

    int num_to_id(int num) { static const int table[] = { 10, 15, 30, 50 }: assert(std::find(&table[0], &table[4], num) != &table[4]); return std::find(&table[0], &table[4], num) - &table[0]: } Can you explain how the lines 3 and 4 work exactly?

  • C++ Create a static implementation of a set. Add the efficiency of each function to the...

    C++ Create a static implementation of a set. Add the efficiency of each function to the documentation in the header file. Use test_set.cpp as your test program. Header: /************************************ This class models a mathematical set. */ #ifndef _SET_H #define _SET_H #include <cstdlib> #include <iostream> class set { public: typedef int value_type; typedef std::size_t size_type; static const size_type CAPACITY = 30; set(); // postcondition: empty set has been created void insert (const value_type& entry); // precondition: if entry is not in...

  • 1) Fix the Function #include <iostream> void print Num() { std::cout << number; }; int main()...

    1) Fix the Function #include <iostream> void print Num() { std::cout << number; }; int main() { int number = 35; printNum (number); return 0; (Give two ways to fix this code. Indicate which is preferable and why.) #include <iostream> void double Number (int num) {num = num * 2;} int main() { int num = 35; double Number (num); std::cout << num; // Should print 70 return 0; (Changing the return type of doubleNumber is not a valid solution.)

  • Given the following function: int fun1(int count){ int Num ; for (i = 0; i <...

    Given the following function: int fun1(int count){ int Num ; for (i = 0; i < count; ++i) { cin >> Value; if (i == 0) Num = Value; else if (Value > Num) Num = Value; } return Num ; } What is the output of the following C++ code segment if the following list is entered? (Input list:  5 -15 -90 -2 -60 -30) int Num = 0 , Value, numValues; cin >> numValues; if (numValues > 0) cout...

  • public static boolean primeCheck (int num) int temp; boolean isPrime-true: for(int i-2;i<=num/ 2 ; i++) temp-numsi...

    public static boolean primeCheck (int num) int temp; boolean isPrime-true: for(int i-2;i<=num/ 2 ; i++) temp-numsi if (temp=-0) isPrime-false; break; return isPrime Given the above code segment, 1. (30 points) Draw a Control Flow Graph (CFG) for the code. 2. (10 points) Enumerate the Test Requirements for node coverage. Design a set of 3. 4. 5. test cases (input value num) that will satisfy node coverage. (10 points) Enumerate the Test Requirements for edge coverage. Design a set of test...

  • The goal is to reinforce the implementation of container class concepts in C++. Specifically, the goal...

    The goal is to reinforce the implementation of container class concepts in C++. Specifically, the goal is to create a static implementation of a set. Add the efficiency of each function to the documentation in the header file. Your program must compile. Use test_set.cpp as your test program. Set.h & Test_Set.cpp is code that is already given. All I need is for you add comments to Set.cpp describing each function. FILE: SET.H #ifndef _SET_H #define _SET_H #include <cstdlib> #include <iostream>...

  • QUESTION 26 Given the following function: int secret(int num, int m) inti, prod=1; if (m=0) return...

    QUESTION 26 Given the following function: int secret(int num, int m) inti, prod=1; if (m=0) return 1: - for (i=0; i<m; i++) { prod = prod * num; } return prod; What is the output for this function call? cout << secret(10,6);

  • #include <iostream> using namespace std; const int SIZE = 10; void displayGreaterThan(int[], int); void displaySmallerThan(int[],int); void...

    #include <iostream> using namespace std; const int SIZE = 10; void displayGreaterThan(int[], int); void displaySmallerThan(int[],int); void displayArrayContent(int[]); void displayLargestValue(int[]); void displaySmallestValue(int[]); int main(){    int number;    int numbers[SIZE] = {9,1,90,98,53,22,76,29,37,65}; cout <<"Enter a number: "; cin >> number; cout << endl;    displayGreaterThan(numbers,number); cout << endl; displaySmallerThan(numbers,number); cout << endl; displayArrayContent(numbers); cout << endl; displayLargestValue(numbers); cout << endl; displaySmallestValue(numbers); cout << endl;    return 0;       } void displayGreaterThan(int value[],int num){ cout << " All larger value(s)than" <<...

  • Python String Product Function Name: string Multiply Parameters: sentence (str), num (int) Returns: product (int) Description:...

    Python String Product Function Name: string Multiply Parameters: sentence (str), num (int) Returns: product (int) Description: You're texting your friend when you notice that they replace many letters with numbers. Out of curiosity, you want to find the product of the numbers. Write a function that takes in a string sentence and an int num, and find the product of only the first num numbers in the sentence. If num is 0, return 0. If num > O but there...

  • Write a python program write a function numDigits(num) which counts the digits in int num. Answer...

    Write a python program write a function numDigits(num) which counts the digits in int num. Answer code is given in the Answer tab (and starting code), which converts num to a str, then uses the len() function to count and return the number of digits. Note that this does NOT work correctly for num < 0. (Try it and see.) Fix this in two different ways, by creating new functions numDigits2(num) and numDigits3(num) that each return the correct number of...

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