Question

Create a template for a swap routine. You must create a program utilizing a template for...

Create a template for a swap routine. You must create a program utilizing a template for a swap routine showing that you can swap integers, floats, doubles, strings and characters.

The format for this template is as follows:

template <class T>
void swapValues (T& V1, T& V2) {

T temp; Temp = V1; V1 = V2; V2 = temp;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std; 

template <class T>
void swapValues (T& V1, T& V2) {
   T temp; temp = V1; V1 = V2; V2 = temp;
}


int main(){
   int i1 = 3, i2 = 5;
   swapValues(i1,i2);
   cout<<i1<<" "<<i2<<endl;
   
   float f1 = 5.2, f2 = 3.5;
   swapValues(f1,f2);
   cout<<f1<<" "<<f2<<endl;
   
   
   double d1 = 3.24543, d2 = 4234.44325;
   swapValues(d1,d2);
   cout<<d1<<" "<<d2<<endl;
   
   
   string s1 = "abc", s2 = "def";
   swapValues(s1,s2);
   cout<<s1<<" "<<s2<<endl;
   
   char c1 = 'b', c2 = 'a';
   swapValues(c1,c2);
   cout<<c1<<" "<<c2<<endl;
   return 0;
}
Add a comment
Know the answer?
Add Answer to:
Create a template for a swap routine. You must create a program utilizing a template for...
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
  • Create a template function printResult which prints a value of type T void printResult(T val); C++...

    Create a template function printResult which prints a value of type T void printResult(T val); C++ Write a program to create two Mathematic type (template below) objects a(10, 5) and b(3.4, 5.5) which prints using the template function printResult the result of a.addition(), a.subtraction(), a.multiplciation(), a.division() b.addition(), b.subtraction(), b.multiplication(), b.division() (Template) template <typename T> class Mathematics{ private: T val1, val2; public: Mathematics(T v1, T v2) { val1 = v1; val2 = v2; } T addition() { return val1 + val2;...

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

  • Any help in the compiler error //Driver Program //***************** /** * * CSCI 241 Assignment 8...

    Any help in the compiler error //Driver Program //***************** /** * * CSCI 241 Assignment 8, Part 3 * * Author: your name * z-ID: your z-ID * Date: due date of assignment * * This program builds, sorts and prints lists using the quicksort and * merge sort algorithms. */ #include <iostream> #include <iomanip> #include <vector> #include <string> #include "sorts.h" #include "quicksort.h" #include "mergesort.h" using std::cout; using std::fixed; using std::left; using std::setprecision; using std::string; using std::vector; // Data files...

  • Binary Tree Template Write your own version of a class template that will create a binary...

    Binary Tree Template Write your own version of a class template that will create a binary tree that can hold values of any data type. Demonstrate the class with a driver program. Place your binary tree template in it's own header file, Btree.h. Include methods for the following: inserting new values into the tree removing nodes from the tree searching the tree returning the number of nodes in the tree displaying the contents of the tree using preorder traversal Your...

  • help with C++ code The aim of this is to practice writing template classes. You must...

    help with C++ code The aim of this is to practice writing template classes. You must use SafeArray template class and implement a SafeMatrix template class that will allow you to work with two dimensional arrays of any type. The boundaries are checked. You must be able to create instances of SafeMatrix template class like, SafeMatrix<int> m(2,3); The above statement will create a 2 by 3 dimensional array of integers. You must be able to access and set values using...

  • in c++ please explain with answer 6. Given the following template function definition, which of the...

    in c++ please explain with answer 6. Given the following template function definition, which of the following is not a valid invocation of the function? template <class T> a. swap(s1,s2); void swap(T& left, T& right) b. swap(int1, int2); { c. swap(ch1, ch2); //implementation d. swap(int1, ch2); } int int1, int2; float flt1, flt2; char ch1, ch2; string s1, s2; 7. Why can you not use the swap template function to swap two complete arrays? template <class T> void swap(T& left,...

  • Study the c++ code below and answer the question on templates after // A polymorphic swap...

    Study the c++ code below and answer the question on templates after // A polymorphic swap function for any type of object (an example) template<typename T> void MySwap( T& x, T& y) { T temp; temp = x; x = y; y = temp; } // A polymorphic class holding an [x,y] position of any type template<class T> class Position { public: Position(T x=0, T y=0) : x_(x), y_(y) {} friend std::ostream& operator<<(std::ostream& os, const Position& p) { return os...

  • Your task in this programming assignment is to write a Python program that implements a simple...

    Your task in this programming assignment is to write a Python program that implements a simple vehicle class. The class models a vehicle that has a year, a make, and a model (e.g., 2016 Dodge Ram, 2007 Honda Civic, etc). For a 2016 Dodge Ram, for example, the year is 2016, the make is Dodge, and the model is Ram. You will be provided a template that includes code (that you cannot change) in the main part of the program...

  • Need to fill the comments with code that works. Create The Starting Template for this program...

    Need to fill the comments with code that works. Create The Starting Template for this program is shown below. Fill in code where you see highlighted in these highlighted sections that do what the comments request. Don't modify maint) or the printCounts0 routine. When you run your program, you will need to eater in a string for the 'readString" method. Use a package named string methods for this program ructions in the methods readString), countOccurrences0.. ete. Write code the string:...

  • **This program is to be created using Visual Studio C#**Create as a Windows Form application** 1....

    **This program is to be created using Visual Studio C#**Create as a Windows Form application** 1. Output a header in the console: "Program 1" 2. Next you will ask the user to enter three floats and read the values into variables. 3. Perform addition, multiplication, and division examples with the numbers entered. 4. Implicitly cast the floats into doubles. 5. Explicitly cast the floats into ints. 6. Use conversion methods to convert the floats into strings. 7. Ask the user...

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