Question

Define an ordinary function called DisplayMin that has three parameters: the first two parameters are parallel...

Define an ordinary function called DisplayMin that has three parameters: the first two parameters are parallel arrays of different types and the third argument is of type size_t representing the size of both arrays. The function DisplayMin must work when called with various types of actual arguments, for example
string DisplayMin(string names[], int calories[], int size) or
int DisplayMin(int calories[], float prices[], int size).
(Parallel Arrays are two arrays whose data is related by a common index. For example: with the string array and int array, index 3 of the string array would have some name, and index 3 of the int array would have an associated value to that name (say, number of calories).)

Write one function that will take either set of arguments in, displays and return the value from the first array corresponding to the min value from the second array, in the form “The min value of <int/float value> belongs to <string/int value>.”

Test your function by calling it with different inputs from a main() function and then printing the result of the function in the main program.

Examples:

Given:

names => string[] = {“French Fries”, “Cheeseburger”, “Hamburger”, “Soda”, “Coffee”}

calories => int[] = {300, 650, 550, 450, 50}

size => int = 5

Output:

“The min value of 50 belongs to Coffee.”

Given:

calories => int[] = {300, 35, 650, 550, 450, 50}

prices => float[] = {2.99, .99, 2.79, 3.19, 1.79, 2.15}

size => int = 6

Output:

“The min value of 35 belongs to 0.99.”

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

Answer:

Code

#include<iostream>

#include<string>

using namespace std;

template <typename T,typename U>

int DisplayMin(T x[], U y[],int size)

{

U min=y[0];

int index=0;

for(int i=1;i<size;i++)

{

if(min>y[i])

{

min=y[i];

index=i;

}

}

return index;

}

int main()

{

//string names[]={"French Fries","ChesseBurger","Hamburger","Soda","Coffee"};

//int calories[]={300,650,550,450,50};

//int size=5;

int size=6;

int calories[]={300,35,650,550,450,50};

float price[]={2.99,.99,2.79,3.19,1.79,2.15};

int index=DisplayMin(price,calories,size);

cout<<"The min value of "<<calories[index]<<" belongs to "<<price[index]<<"."<<endl;

return 0;

}

output:

#include<iostream>

#include<string>

using namespace std;

template <typename T,typename U>

int DisplayMin(T x[], U y[],int size)

{

U min=y[0];

int index=0;

for(int i=1;i<size;i++)

{

if(min>y[i])

{

min=y[i];

index=i;

}

}

return index;

}

int main()

{

string names[]={"French Fries","ChesseBurger","Hamburger","Soda","Coffee"};

int calories[]={300,650,550,450,50};

int size=5;

//int size=6;

//int calories[]={300,35,650,550,450,50};

//float price[]={2.99,.99,2.79,3.19,1.79,2.15};

int index=DisplayMin(names,calories,size);

cout<<"The min value of "<<calories[index]<<" belongs to "<<names[index]<<"."<<endl;

return 0;

}
output:le two function - Microsoft Visual Studio Quick Launch (Ctrl+Q) The min value of 50 belongs to Coffee Press any key to contin

Add a comment
Know the answer?
Add Answer to:
Define an ordinary function called DisplayMin that has three parameters: the first two parameters are parallel...
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
  • In C++ format: Define a function called DisplayMax. It will have 3 parameters of two different...

    In C++ format: Define a function called DisplayMax. It will have 3 parameters of two different types of known parameter lists which are either void DisplayMax(string idI, double value[], int size) or void DisplayMax(int idn, short value几int size). The first and second parameters are parallel arrays. (Parallel Arrays are two arrays whose data is related by a common index. For example: with the string array and double array, index 5 of the string array would have some name, and index...

  • 1. Write code for a function that receives two parameters (a,and b) by value and two...

    1. Write code for a function that receives two parameters (a,and b) by value and two more parameters (c and d) by reference. All parameters are double. The function works by assigning c to (a/b) and assigning d to (a*b). The function has no return value. From main, use scanf to get two numbers, then call the function, and then display both outcome values to the output in a printf statement. 2. After part 1 is completed, write code to...

  • A function that takes three parameters: an int array, an int n that is the size...

    A function that takes three parameters: an int array, an int n that is the size of the array and an int pointer max that is to assign the maximum value in the array. The function will find and return the index of the first occurrence of the maximum value in the array and assign the maximum value to the pointer. For example, in the array {1, 2, 6, 5, 6, 4, 3, 6, 4}, the maximum value is 6...

  • In this question you have to write a complete function. MyMedia Publishers uses two parallel arrays...

    In this question you have to write a complete function. MyMedia Publishers uses two parallel arrays to keep track of the number of subscriptions for each of their 50 publications. Array publications holds the names of the magazines and newspapers published and array subscriptions holds the number of subscriptions for each corresponding magazine or newspaper. You have to write a void function, called findMost Subs to determine which publication has the most subscribers. Function findMost Subs has to return the...

  • Code written in NASM Function called findLargest Write a function called findLargest that receives two parameters:...

    Code written in NASM Function called findLargest Write a function called findLargest that receives two parameters: an unsigned doubleword array and the length of the array. The function must return the value of the largest array member in eax. Preserve all registers (except eax) that are modified by the function. Write a test program in main that calls findLargest three times, each call using a different array with different lengths. Function called countHits Write a function called named countHits that...

  • C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) L...

    C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) Last Name (char[]) Age (int) Height in Inches (double) Weight in Pounds (double) You will use pass-by-reference to modify the values of the arguments passed in from the main(). Remember that arrays require no special notation, as they are passed by reference automatically, but the other...

  • C Programming Language 2(a) Define a struct with 1 int array named i, 1 float array...

    C Programming Language 2(a) Define a struct with 1 int array named i, 1 float array named f, and one double array named d, each of size M. (b)Declare array x with N of those structs. (c)Write a void function to traverse array x (using a pointer) assigning to each element in each array d (in each struct in array x) the sum of the corresponding elements in arrays i and f (in the same struct). Use 3 pointers (of...

  • Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...

    Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. To test your function, write a main that prompts a user for a list of 15 integers and outputs the index and value of the first occurrence of the smallest value. The program should print out Enter 15 integers: The position of the first occurrence of the smallest element in...

  • In this question you have to write a complete function. in C++ MyMedia Publishers uses two...

    In this question you have to write a complete function. in C++ MyMedia Publishers uses two parallel arrays to keep track of the number of subscriptions for each of their 50 publications.    Array  publications holds 1)  the names of the  magazines and newspapers  published and  array  subscriptions 2)holds  the  number  of  subscriptions  for  each  corresponding  magazine  or newspaper. You  have  to write  a  void function, called  findMostSubs to  determine  which  publication has  the  most  subscribers. Function findMostSubs has to return the name of the publication as well as the number of subscribers to that publication. Assume the following: • a declaration of a global constant: const...

  • Write a program that works with two arrays of the same size that are related to...

    Write a program that works with two arrays of the same size that are related to each other in some way (or parallel arrays). Your two arrays must be of different data types. For example, one array can hold values that are used in a formula that produces the contents of the second array. Some examples might be:  from a previous program, populations and the associated flowrates for those populations (an int array of populations and a double array...

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