Question

The following function compares integers of type int and gives as a result the smaller Number:...

The following function compares integers of type int and gives as a result the smaller Number: int min (int a, int b) { if (a < b) return a; else return b; } Create a template and Convert this function to a template. Before converting this function to a template, display how you would handle var a & b if they held the same value. In other words if a and b are equal, what would you do (how would this be coded)?

Homework Answers

Answer #1

The following function could be coded as :

int min (int a, int b)

{

IF (a<b):

THEN return a

ELSE IF (b<a):

THEN return b

ELSE:

return a

}

This function could be called with two variables (a & b) from main function. This could either be integers of float numbers. We have shown for integers. Hence, the return type is also given as 'int'. We can consider this as a pseudocode (not specific to any programming language, rather generic for understanding purpose).

Here, there are three conditions as given in IF-THEN-ELSE format - 1) a is less than b, in this case we return a, 2) b is less than a, in this case, we return b, 3) a and b are equal, in this case, we can retrun either of a or b, doesn't matter since both are equal.

Know the answer?
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
Similar Questions
Data Structures using C++ Consider the definition the following function template: template <class Type> Type func(Type...
Data Structures using C++ Consider the definition the following function template: template <class Type> Type func(Type list[], int size) {        Type x = list[0];        Type y = list[size - 1];        for (int j = 1; j < size / 2; j++)        {               if (x < list[j])                      x = list[j];               if (y > list[size - 1 - j])                      y = list[size - 1 - j];        }        return x + y; }...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
Please answer the following C question: There is a documented prototype for a function called get_a_line...
Please answer the following C question: There is a documented prototype for a function called get_a_line in the code below. Write a definition for get_a_line—the only function called from that definition should be fgetc. #include <stdio.h> #include <string.h> #define BUFFER_ARRAY_SIZE 10 int get_a_line(char *s, int size, FILE *stream); // Does what fgets does, using repeated calls to fgetc, but // provides a more useful return value than fgets does. // // REQUIRES // size > 1. // s points to...
QUESTION 1 What does the following code segment output? int red, blue; red = 7; blue...
QUESTION 1 What does the following code segment output? int red, blue; red = 7; blue = red + 2 * 5 red++; blue = blue + red; cout << blue; 4 points    QUESTION 2 Is the following statement true or false? The Boolean expression in the following if statement will be true for all values of x in the range from 10 to 20 (including the endpoints) and false for all other values: int x; if (x >=...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any type dynamic arrays (replace string by the template in all instances below). • The class should have: – A private member variable called dynamicArray that references a dynamic array of type string. – A private member variable called size that holds the number of entries in the array. – A default constructor that sets the dynamic array to NULL and sets size to 0....
question : Take the recursive Java program Levenshtein.java and convert it to the equivalent C program....
question : Take the recursive Java program Levenshtein.java and convert it to the equivalent C program. Tip: You have explicit permission to copy/paste the main method for this question, replacing instances of System.out.println with printf, where appropriate. You can get the assert function from assert.h. Try running the Java program on the CS macOS machines. You can compile and run the program following the instructions discussed in class. Assertions are disabled by default. You can enable assertions by running java...
1. Given the following multi-way if statement, provide a switch statement, using proper java syntax, that...
1. Given the following multi-way if statement, provide a switch statement, using proper java syntax, that will provide the same function. Char grade; String tstmsg; if (grade == ‘A’) {   tstmsg = “Excellent”; } else if (grade == ‘B’) {   tstmsg = “Good”; } else if (grade == ‘C’) {   tstmsg = “OK”; } else {   tstmsg = “Study More”; } 2.Write the following for statement as a while statement. for (k = 0; k < 3; k++) {   System.out.println...
In a small-scale regression study, we collected data on the number of children in a family...
In a small-scale regression study, we collected data on the number of children in a family Xi and the number of hours per week spent shopping Yi. The following data were obtained: i 1 2 3 4 5 6 Xi 2 6 3 1 1 9 Yi 13 17 12 12 9 22 Assume we performed a simple linear regression of Yi on Xi, i.e. E(Yi) = ?0 + ?1Xi (a) By hand compute X?X, X?Y, (X?X)-1, b, Y^(means Y-hat),...
Your C program will do the following : Must use at least 2 function prototypes &...
Your C program will do the following : Must use at least 2 function prototypes & definitions . You can also use repetitions , control structures . You re not allowed any type of global arrays, or global variables. You are only allowed to use 2 dimensional arrays. 1. In your main program, create a array of size 7 X 7. 2. Create a function that accepts the empty array. The function will initiate the to zero. Then, the function...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix it.. Can you explain to me what I am doing wrong? Warning: dlist.cc: In function 'std::ostream& operator<<(std::ostream&, dlist&)': dlist.cc:66:10: error: invalid initialization of reference of type 'std::ostream& {aka std::basic_ostream&}' from expression of type 'dlist::node*' dlist.cc: In function 'dlist operator+(dlist&, dlist&)': dlist.cc:93:8: error: invalid operands of types 'dlist::node*' and 'dlist::node*' to binary 'operator+' dlist.cc:97:8: error: could not convert 'result' from 'int' to 'dlist' My code:...