Create a new file in c++ and save it as lab11_XYZ.cpp (replace XYZ with your initials).Create ANOTHER new file (in C++) and save it as swap_XYZ.h (replace XYZ with your initials). Use the swap_XYZ.h header file to define a set of functions for swapping two items of the same type.Given the following prototypes, define (overloaded) swap functionsvoid swap(int&, int&);  //swap two integersvoid swap(double&, double&);  //swap two doublesvoid swap(float&, float&);  //swap two floatsvoid swap(char&, char&);  //swap two charactersvoid swap(string&, string&);  //swap two stringsThe header file should contain the following:header comments (similar to normal program header comments)function prototypes (provided above)function definitions (for you to do...)In general, a swap works like this:void swap(type first&, type second&){    type temp = first; first = second; second = temp;}Once your header file (H file) is complete, make sure to include it in your main CPP program. Use double quotes instead of angle brackets. Make sure the H file is located in the same folder as the CPP file.#include <iostream>#include "swap_XYZ.h"using namespace std; Write a driver program to test your header file. The program should test each version of the swap function and display the results.The program shoulddisplay a hello message consider using displayMessage()!for each version of swap()provide or prompt for two itemsdisplay which version is being calleddisplay values before swapcall swap functiondisplay values after swapdisplay a goodbye message use displayMessage() again!

Question
Asked Jul 31, 2019
103 views

Create a new file in c++ and save it as lab11_XYZ.cpp (replace XYZ with your initials).

Create ANOTHER new file (in C++) and save it as swap_XYZ.h (replace XYZ with your initials).

 

Use the swap_XYZ.h header file to define a set of functions for swapping two items of the same type.

Given the following prototypes, define (overloaded) swap functions

  • void swap(int&, int&);  //swap two integers
  • void swap(double&, double&);  //swap two doubles
  • void swap(float&, float&);  //swap two floats
  • void swap(char&, char&);  //swap two characters
  • void swap(string&, string&);  //swap two strings


The header file should contain the following:

  • header comments (similar to normal program header comments)
  • function prototypes (provided above)
  • function definitions (for you to do...)

In general, a swap works like this:

void swap(type first&, type second&){    type temp = first; first = second; second = temp;

}

Once your header file (H file) is complete, make sure to include it in your main CPP program. Use double quotes instead of angle brackets. Make sure the H file is located in the same folder as the CPP file.

#include <iostream>#include "swap_XYZ.h"

using namespace std;

 

Write a driver program to test your header file. The program should test each version of the swap function and display the results.

The program should

  • display a hello message consider using displayMessage()!
  • for each version of swap()
    • provide or prompt for two items
    • display which version is being called
    • display values before swap
    • call swap function
    • display values after swap
  • display a goodbye message use displayMessage() again!

 

 

check_circle

Expert Answer

Step 1

Program Instructions:

  • In the header file, create a temporary variable temp of the data type of each version of swap() to hold the values while swapping.
  • In the main file, declare string constants and use them to display hello and goodbye message.
  • Display before and after swapping the values for each swap() version.
Step 2

Program:

File (swap_XYZ.h):

// this header file implements the various versions of swap function

// header file

#include<iostream>

#include<string> // for string class

using namespace std;

//prototypes

void swap(int&, int&);  //swap two integers

void swap(double&, double&);  //swap two doubles

void swap(float&, float&);  //swap two floats

void swap(char&, char&);  //swap two characters

void swap(string&, string&);  //swap two strings

 

void swap(int& a, int& b){

     int temp;

    temp = a;

    a = b;

    b = temp;

}

void swap(double& a, double& b){

     double temp;

    temp = a;

    a = b;

    b = temp;

}

void swap(float& a, float& b){

     float temp;

    temp = a;

    a = b;

    b = temp;

}

void swap(char& a, char& b){

     char temp;

    temp = a;

    a = b;

    b = temp;

}

void swap(string& a, string& b){

     string temp;

    temp = a;

    a = b;

    b = temp;

}

 

File (lab11_XYZ.cpp ):

// header files

#include <iostream>

#include "swap_XYZ.h"

using namespace std;

const string HELLO = "Hello!Welcome to the program...\n";

const string GOODBYE = "\nGoodbye!exiting the program...";

// prototypes

void displayMessage(string);

int main(){

     displayMessage(HELLO);

    

     int a = 5, b = 10;

     cout<<"Swap version: swap(int& a, int& b)"<<endl;

     cout << "Before swapping." << endl;

    cout << "a = " << a << ", b = " << b << endl;

    swap(a,b);

    cout << "After swapping." << endl;

    cout << "a = " << a << ", b = " << b << endl;

   

    double c = 5.2, d = 10;

     cout<<"\nSwap version: swap(double&, double&)"<<endl;

     cout << "Before swapping." << endl;

    cout << "c = " << c &l...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: create a file in c++. Download the attached CreateRandomNumbersFile.cpp file, open it in Dev C++, an...

A: Program Instructions:Include necessary header files, declare local variables, and display a hello me...

question_answer

Q: Let L1 = L (a∗baa∗) and L2 = L (aba∗). Find L1/L2.

A: Solution:Given that,Input: L1= L(a*baa*) and L2 = L(aba*) Output: Determine the L1/L2.We take all st...

question_answer

Q: For problem 10RQ for Business Informaiton Systems book by Paige Baltzan, the question asked what is ...

A: The terms Collective intelligence, Knowledge management and Crowdsourcing are related to each other ...

question_answer

Q: Find dfa’s that accept the following languages: (a) L (aa∗ + aba∗b∗). (b) L (ab (a + ab)∗ (a + aa)).

A: We will solve part (a) L (aa∗ + aba∗b∗) first. To get the DFA, we need to draw the NFA of the given ...

question_answer

Q: Develop pseudocode for the problem of reading in the radius of a circle (r), calculate the area of t...

A: The program will have the following steps:Receive the input as a parameter passed to the function wh...

question_answer

Q: Write a class AnalyzeNumbers which asks the user to enter the number of inputs andstores them in an ...

A: Program plan:Define the class “AnalyzeNumbers”.Define the “main()” method.Create object for “Scanner...

question_answer

Q: Find regular grammars for the following languages on {a, b}: L = {w : na (w) and nb (w) are both eve...

A: Regular grammar for the language L = {W: na (w) and nb (w) are both even} is as follows:

question_answer

Q: what are some of the technical advanteges that a firm can benenfit from selling their products on Am...

A: Technical advantages of selling the product in amazon rather than in own e-commerce website:The mark...

question_answer

Q: How do you convert 172.16.20.55 to binary format?

A: 172.16.20.55 represents IP address in decimal format. In order to convert it into binary format we n...