Question

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?

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

`Hey,

Note: If you have any queries related to the answer please do comment. I would be very happy to resolve all your queries.

Basically find function has syntax

result=find(first,last,val)

where

first, last

Input iterators to the initial and final positions in a sequence. The range searched is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.

val

Value to search for in the range.
T shall be a type supporting comparisons with the elements pointed by InputIterator using operator== (with the elements as left-hand side operands, and val as right-hand side).

result is basically An iterator to the first element in the range that compares equal to val.
If no elements match, the function returns last.

SO, FOR LINE 3

assert(std::find(&table[0], &table[4], num) != &table[4]);

It searches in array table the value num and checks if address returned is not last then assert is true

LINE 4 IS BASICALLY THE RETURNED index from starting value where the value num is found

Kindly revert for any queries

Thanks.

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

  • #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" <<...

  • void card:: setNum(int n) { assert(n >= 1 && n <= 13); num = n; }...

    void card:: setNum(int n) { assert(n >= 1 && n <= 13); num = n; } void card::setSuit(char s) { assert(s == 'C' || s == 'D' || s == 'H' || s == 'S'); suit = s; } int card::getNum() { assert(num >= 1 && num <= 13); return num; } char card::getSuit() { assert(suit == 'C' || suit == 'D' || suit == 'H' || suit == 'S'); return suit; } string card:: read() { string output=""; if...

  • 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...

  • 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...

  • Find and fix errors #include <iostream> #include <cstdlib> #include <ctime> using namespace std; const int MIN...

    Find and fix errors #include <iostream> #include <cstdlib> #include <ctime> using namespace std; const int MIN = 1; const int MAX = 10; int getRandom(int low, int high); int main() {    int random_num = 0; int player_num; int tries; int seed = static_cast<int>(time(0)); bool guessed = false;    srand(seed); // call the getRandom function below       tries = 4; while ( tries > 0 && !guessed ) { cout << "Enter a number within the range 1 to...

  • 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...

  • 1 #include <string> 2 #include <iostream> 3 using std::string; 4 using std::cout; 5 using std::endl; 7...

    1 #include <string> 2 #include <iostream> 3 using std::string; 4 using std::cout; 5 using std::endl; 7 class Pet 8 { 9   public: 10       string name; 11       virtual void print( ) const; 12}; 13 14 class Dog:public Pet 15 { 16   public: 17       string breed; 18       virtual void print( ) const; 19}; 20 21 int main( ) 22 { 23   Dog vdog; 24   Pet vpet; 25   vdog.name = "Tiny"; 26   vdog.breed = "Great Dane"; 27   vpet =...

  • What is the value of  result after the following code executes?   int a = 60; int b...

    What is the value of  result after the following code executes?   int a = 60; int b = 15; int result = 20; if (a = b) result *= 3; 30 20 60 10 code will not execute The numeric data types in C++ can be broken into two general categories which are integers and floating-point numbers singles and doubles real and unreal numbers numbers and characters numbers and literals Which line in the following program will cause a compiler error?...

  • FILL IN THE BLANKS #include #include <> *BLANK* using namespace std; int main() {     const...

    FILL IN THE BLANKS #include #include <> *BLANK* using namespace std; int main() {     const double HOURLY_RATE = 15;     string input_str;     cout << "Enter days of attendance: " << endl;    *BLANK* (cin, input_str);     stringstream input_stream();     int total_hours = 0;     while(!input_stream.())*BLANK*     {         int hours;         string day;         input_stream >> day;         if(day ==  *BLANK*|| day == *BLANK*)         {             hours = 5;         }         *BLANK*(day == "Tuesday" || day == "Thursday")         {             hours = 4;         }         else if(day ==...

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