Starting Out with C++ from Control Structures to Objects (9th Edition)
9th Edition
ISBN: 9780134498379
Author: Tony Gaddis
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 16, Problem 16RQE
A template which is designed to work with a specific data type is referred as “specialized template”.
Program Plan Intro
Function template:
In C++, a function template is referred as a “generic” function, which can work with different data types.
- While writing a function template, a programmer can specify the “type parameter” instead of using the actual data type.
- The compiler generates the code, when it encounters a function call to a function template. This code will handle the particular data type which is used in the function call.
Example:
For example consider the below function template used to find the cube of given value for any types:
//template function
template <class T>
//function definition of "cube"
T cube(T x)
{
//return the cube of the value
return x*x*x ;
}
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
assembly language ctt
9. Counting Nearly Matching ElementsWrite a procedure named CountNearMatches that receives pointers to two arrays of signed doublewords, a parameter that indicates the length of the two arrays, and a parameter that indicates the maximum allowed difference (called diff) between any two matching elements. For each element x(i) in the first array, if the difference between it and the corresponding y(i) in the second array is less than or equal to diff, increment a count. At the end, return a count of the number of nearly matching array elements in EAX. Display this value.
Write a test program that calls CountNearMatches and passes pointersto two different pairs of arrays. Use the INVOKE statement to call your procedure and pass stackparameters. Create a PROTO declaration for CountMatches. Save and restore any registers (other than EAX) changed by your procedure.
Please complete the following guidelines and hints. Using C language.
Please use this template:
#include <stdio.h>#define MAX 100struct cg { // structure to hold x and y coordinates and massfloat x, y, mass;}masses[MAX];int readin(void){/* Write this function to read in the datainto the array massesnote that this function should return the number ofmasses read in from the file */}void computecg(int n_masses){/* Write this function to compute the C of Gand print the result */}int main(void){int number;if((number = readin()) > 0)computecg(number);return 0;}Testing your workTypical Input from keyboard:40 0 10 1 11 0 11 1 1Typical Output to screen:CoG coordinates are: x = 0.50 y = 0.50
One problem with dynamic arrays is that they don't carry around their size. Create a structure,
DynArray which uses a unique pointer for a dynamic array (data), and an int for the
size and capacity.
dynarray.h
#ifndef DYNARRAY_H
#define DYNARRAY_H
1
2
3
4
#include
#include
7
// Define the DynArray structure here
8
std::ostream& operator>(std::istream& in, DynArray& a);
DynArray makeDA (int capacity);
10
11
12
13
#endif
memory.cpp
1
#include
#include
#include "dynarray.h"
using namespace std;
2
3
4
int main()
{
cout « "Array capacity: ";
int capacity;
cin >> capacity;
7
8
9
10
11
auto a = makeDA (capacity);
cout « "Enter up to " > a;
12
13
14
15
16
cout « "a->" « a « endl;
17
}
Chapter 16 Solutions
Starting Out with C++ from Control Structures to Objects (9th Edition)
Ch. 16.1 - Prob. 16.1CPCh. 16.1 - Prob. 16.2CPCh. 16.1 - Prob. 16.3CPCh. 16.1 - Prob. 16.4CPCh. 16.1 - Prob. 16.5CPCh. 16.3 - Prob. 16.6CPCh. 16.3 - The following function accepts an i nt argument...Ch. 16.3 - Prob. 16.8CPCh. 16.3 - Prob. 16.9CPCh. 16.4 - Prob. 16.10CP
Ch. 16.4 - Prob. 16.11CPCh. 16 - Prob. 1RQECh. 16 - Prob. 2RQECh. 16 - Prob. 3RQECh. 16 - Prob. 4RQECh. 16 - What is unwinding the stack?Ch. 16 - What happens if an exception is thrown by a classs...Ch. 16 - How do you prevent a program from halting when the...Ch. 16 - Why is it more convenient to write a function...Ch. 16 - Why must you be careful when writing a function...Ch. 16 - The line containing a throw statement is known as...Ch. 16 - Prob. 11RQECh. 16 - Prob. 12RQECh. 16 - Prob. 13RQECh. 16 - The beginning of a template is marked by a(n)...Ch. 16 - Prob. 15RQECh. 16 - Prob. 16RQECh. 16 - Write a function that searches a numeric array for...Ch. 16 - Write a function that dynamically allocates a...Ch. 16 - Make the function you wrote in Question 17 a...Ch. 16 - Write a template for a function that displays the...Ch. 16 - Prob. 21RQECh. 16 - Prob. 22RQECh. 16 - Prob. 23RQECh. 16 - Prob. 24RQECh. 16 - T F All type parameters defined in a function...Ch. 16 - Prob. 26RQECh. 16 - T F A class object passed to a function template...Ch. 16 - Prob. 28RQECh. 16 - Prob. 29RQECh. 16 - Prob. 30RQECh. 16 - Prob. 31RQECh. 16 - T F A class template may not be derived from...Ch. 16 - T F A class template may not be used as a base...Ch. 16 - Prob. 34RQECh. 16 - Prob. 35RQECh. 16 - try { quotient = divide(num1, num2); } cout The...Ch. 16 - template class T T square(T number) { return T T;...Ch. 16 - template class T int square(int number) { return...Ch. 16 - Prob. 39RQECh. 16 - Assume the following definition appears in a...Ch. 16 - Assume the following statement appears in a...Ch. 16 - Prob. 1PCCh. 16 - Prob. 2PCCh. 16 - Prob. 3PCCh. 16 - Prob. 4PCCh. 16 - Prob. 5PCCh. 16 - IntArray Class Exception Chapter 14 presented an...Ch. 16 - TestScores Class Write a class named TestScores....Ch. 16 - Prob. 8PCCh. 16 - Prob. 9PCCh. 16 - SortableVector Class Template Write a class...Ch. 16 - Inheritance Modification Assuming you have...Ch. 16 - Prob. 12PCCh. 16 - Prob. 13PC
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- (Data processing) A bank’s customer records are to be stored in a file and read into a set of arrays so that a customer’s record can be accessed randomly by account number. Create the file by entering five customer records, with each record consisting of an integer account number (starting with account number 1000), a first name (maximum of 10 characters), a last name (maximum of 15 characters), and a double-precision number for the account balance. After the file is created, write a C++ program that requests a user-input account number and displays the corresponding name and account balance from the file.arrow_forwardIn C, in order to do operations to the data being referenced by a pointer you would need to use the (*) operator, which is called ______________ operator.arrow_forwardPHP Write a reduce function Write a function reduce($arr, $func) that takes an array and a function as a parameter. The reduce function should apply the parameter function to each element of the array in succession to produce a single result. Note: the current result should always be the first parameter to the function and the next element of the array should always be the second parameter. You may not use the PHP function array_reduce in your solution. For example, the result of the following should be 10. function myMax($current, $new) { return $current < $new ? $new : $current; } $arr = array(10, 5, 3, 5, 1, 2, 5, 7, 4); print("Max: " . reduce($arr, 'myMax') .arrow_forward
- C language. Function write the arraylist_sort function This generic function sorts an array list using the given compare function. l An array list compare Pointer to the function which compares two elements If you cannot write down a generic function which works for all types, write down a function which sorts resturants. If you cannot use function pointers, you can write down multiple functions which sort using different criteria.arrow_forwardC Program Functions with 1D Arrays Create a program that asks the user for the size of an integer array and the elements of the array. Then, create a function with the following details: Parameters: An integer array The size of the integer array Return type - int Return value - the maximum value of the integer array Call the function you created in the main and pass the integer array and its size to it. Make sure to store the return value in a variable so you could print it afterwards. Input 1. First line contains the size of the array 2. Succeeding lines are the elements of an array Output Enter size of array: 5 Enter element 1: 1 Enter element 2: 2 Enter element 3: 3 Enter element 4: 4 Enter element 5: 5 Maximum element: 5arrow_forwardc++ programming Write a function named maxCols which stores the maximum value in each row of a 3x3 2D array into a 1D array. This function should accept a 3x3 2D array, a 1D array and the size of each dimension as parameters. Example Calling maxCols with this 2D array:3 7 2 9 3 6 8 5 6 Results in the 1D array storing the values 7,9,8.arrow_forward
- Create the following code in C Create a program that has an array for which the user determines the maximum size. The array’s elements will be of some user defined type (struct) that you will make up. This array will be stored in disjoint dynamic memory. The program must have some type of menu. You will pick your own theme for the program. No previous themes can be used. Be sure to utilize error checking and be sure you free of all memory used on the heap.arrow_forwarduse c++ Programming language Write a program that creates a two dimensional array initialized with test data. Use any data type you wish . The program should have following functions: .getAverage: This function should accept a two dimensional array as its argument and return the average of each row (each student have their average) and each column (class test average) all the values in the array. .getRowTotal: This function should accept a two dimensional array as its first argument and an integer as its second argument. The second argument should be the subscript of a row in the array. The function should return the total of the values in the specified row. .getColumnTotal: This function should accept a two dimensional array as its first argument and an integer as its second argument. The second argument should be the subscript of a column in the array. The function should return the total of the values in the specified column. .getHighestInRow: This function should accept a two…arrow_forwardUse c++ Programming language Write a program that creates a two dimensional array initialized with test data. Use any data type you wish . The program should have following functions: .getAverage: This function should accept a two dimensional array as its argument and return the average of each row (each student have their average) and each column (class test average) all the values in the array. .getRowTotal: This function should accept a two dimensional array as its first argument and an integer as its second argument. The second argument should be the subscript of a row in the array. The function should return the total of the values in the specified row. .getColumnTotal: This function should accept a two dimensional array as its first argument and an integer as its second argument. The second argument should be the subscript of a column in the array. The function should return the total of the values in the specified column. .getHighestInRow: This function should accept a two…arrow_forward
- Huffman code // C program for Huffman Coding #include<stdio.h> #include<stdlib.h> #define MAX_TREE_HT 100 struct MinHeapNode { char data; unsigned freq; struct MinHeapNode *left, *right; }; struct MinHeap { unsigned size; unsigned capacity; struct MinHeapNode** array; }; struct MinHeapNode* newNode(char data, unsigned freq) { struct MinHeapNode* temp = (struct MinHeapNode*)malloc (sizeof(struct MinHeapNode)); temp->left = temp->right = NULL; temp->data = data; temp->freq = freq; return temp; } struct MinHeap* createMinHeap(unsigned capacity) { struct MinHeap* minHeap = (struct MinHeap*)malloc(sizeof(struct MinHeap)); minHeap->size = 0; minHeap->capacity = capacity; minHeap->array = (struct MinHeapNode**)malloc(minHeap-> capacity * sizeof(struct MinHeapNode*)); return minHeap; } void swapMinHeapNode(struct MinHeapNode** a, struct MinHeapNode** b) { struct MinHeapNode* t = *a; *a = *b;…arrow_forwardT/F The << operator is overloaded to input data items of fundamental types, strings and pointer valuesarrow_forwardC Program Functions and Pointers Create a function called terminate that asks: integer array to be referenced for modification integer value to serve as an indexing variable. This function sets the value of an element in the array to 0 based on the index value provided. This function does not return anything. In the main function, write a program that asks the user for an integer input and call the terminate function by passing the array and the inputted integer. Print the contents of the modified array where each elements are separated by a space. Input 1. One line containing an integer input Output Ten int outputs each separated by a space Enter·a·number(0-9):·1 1·0·1·1·1·1·1·1·1·1 Create a function called terminate that asks: integer array to be referenced for modification integer value to serve as an indexing variable. This function sets the value of an element in the array to 0 based on the index value provided. This function does not return anything.…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr