Question

C++ Chapter 16 Problem: Implement #25 as a template function (Demonstrate using int, double, string, and...

C++

Chapter 16 Problem: Implement #25 as a template function (Demonstrate using int, double,

string, and x,y pair object)

24. Write a function that searches a numeric array for a specified value. The function

should return the subscript of the element containing the value if it is found in the

array. If the value is not found, the function should throw an exception.

25. Write a function that dynamically allocates a block of memory and returns a char

pointer to the block. The function should take an integer argument that is the amount

of memory to be allocated. If the new operator cannot allocate the memory, the function

should return a null pointer.

26. Make the function you wrote in Question 24 a template.

27. Write a template for a function that displays the contents of an array of any type.

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

Thanks for the question.


Here is the completed code for this problem. Let me know if you have any doubts or if you need anything to change.


Thank You !!

################################################################################################

#include<iostream>
using namespace std;

//24. Write a function that searches a numeric array for a specified value. The function
template< typename T>
int indexOf(T array[], T val, int size){
  
   for(int i=0; i<size; i++){
       if( array[i]==val){
           return i;
       }
   }
  
   throw "Value not found inside array.";
  
}

//25. Write a function that dynamically allocates a block of memory and returns a char

char* allocateCharMemory(int size){

   return new char[size];
}


//26. Make the function you wrote in Question 24 a template.
tempate <typename T>
T* allocateMemory(int size){
  
   T* pointer = new T[size];
   return pointer;
}

//27. Write a template for a function that displays the contents of an array of any type
tempate <typename T>
void print(T array[], int size){
   for(int i=0; i<size; i++){
       cout<<T[i]<<" ";
   }
   cout<<endl;
  
}

int main(){
  
   int a[] ={1,2,3,4,5,6,7,8,9,10};
   try{
       int i= indexOf(a,8,10);
       cout<<"Index of "<<6<<" is : "<<i<<endl;
       i= indexOf(a,11,10);
       cout<<"Index of "<<6<<" is : "<<i<<endl;
   }catch(const char *message){
       cout<<message<<endl;
   }
}

##############################################################################################

Note:
As per Chegg policy, I am allowed to only the first question with the first 4 sub parts, please post the remaining questions as separate post, and i will be happy to answer them.


Sorry for the inconvenience caused.

Add a comment
Know the answer?
Add Answer to:
C++ Chapter 16 Problem: Implement #25 as a template function (Demonstrate using int, double, string, and...
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 this project you will create a console C++ program that will have the user to...

    In this project you will create a console C++ program that will have the user to enter Celsius temperature readings for anywhere between 1 and 365 days and store them in a dynamically allocated array, then display a report showing both the Celsius and Fahrenheit temperatures for each day entered. This program will require the use of pointers and dynamic memory allocation. Getting and Storing User Input: For this you will ask the user how many days’ worth of temperature...

  • ​Write a C++ function int calculate(char, int , int ) which takes a char of either ‘+’, ‘ ‘--’, ‘*’, or ‘/’ as operator and two integers as operands.

    Write a C++ function int calculate(char, int , int ) which takes a char of either ‘+’, ‘ ‘--’, ‘*’, or ‘/’ as operator and two integers as operands. The function should return the result of applying the corresponding operator on the operands if all of them are of proper types and values. It should throw a char exception if the operator is unknown and a char* exception if the operator is ‘/’ and the second operand is zero. Write...

  • In C Programming Language In this lab you will implement 4 string functions, two using array...

    In C Programming Language In this lab you will implement 4 string functions, two using array notation and two using pointers. The functions must have the signatures given below. You may not use any C library string functions. The functions are 1. int my strlen (char s ) - This function returns the number of characters in a string. You should use array notation for this function. 2. int my strcpy (char s [], char t I)- This function overwrites...

  • C++,please help. I have no idea how to do that begining with zero parameter constructor. and...

    C++,please help. I have no idea how to do that begining with zero parameter constructor. and help me to test the method, Write a class template Square Matrix that implements a square matrix of elements of a specified type. The class template should be fully in the Square Matrix.h file. The class uses "raw" pointers to handle the dynamically allocated 2D-array. "Raw" pointers are the classic pointers that you've used before. In addition to it, you will need a variable...

  • 17.9 Worksheet 7 (C++) Follow the instructions commented into the given template. int main() { int...

    17.9 Worksheet 7 (C++) Follow the instructions commented into the given template. int main() { int array1[20] = {3, 18, 1, 25, 4, 7, 30, 9, 80, 16, 17}; int numElements = 11; cout << "Part 1" << endl; // Part 1 // Enter the statement to print the numbers in index 4 and index 9 // put a space in between the two numbers    cout << endl; // Enter the statement to print the numbers 3 and 80...

  • (C++) Write a function that accepts an int array and the array’s size as arguments. The function...

    (C++)Write a function that accepts an int array and the array’s size as arguments.The function should create a new array that is twice the size of the argument array.The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0.The function should return a pointer to the new array.Demonstrate the function by using it in a main program that reads an integer N (that is not more...

  • Let’s build a dynamic string tokenizer! Start with the existing template and work on the areas...

    Let’s build a dynamic string tokenizer! Start with the existing template and work on the areas marked with TODO in the comments: Homework 8 Template.c Note: If you turn the template back into me without adding any original work you will receive a 0. By itself the template does nothing. You need to fill in the code to dynamically allocate an array of strings that are returned to the user. Remember: A string is an array. A tokenizer goes through...

  • In C++ (HexFormatException) Implement the hex2Dec function in Programming Exercise 16.1 to throw a HexFormatException if...

    In C++ (HexFormatException) Implement the hex2Dec function in Programming Exercise 16.1 to throw a HexFormatException if the string is not a hex string . Define a custom exception class named HexFormatException. Write a test program that prompts the user to enter a hex number as a string and displays the number in decimal. If the function throws an exception, display "Not a hex number". Included is the hex2Dec Function #include <iostream> #include <string> #include <cctype> using namespace std; // Converts...

  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

  • Write a program in C++ that uses a class template to create a set of items....

    Write a program in C++ that uses a class template to create a set of items. . . The Problem Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 Hint: Use vectors and vector functions to store the set 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