Need help with template project for C++ please and thank you. Please send correc
ID: 665891 • Letter: N
Question
Need help with template project for C++ please and thank you. Please send correct code and output so I do not get confused on the concepts. The dropbox link to the text documents are below:
https://www.dropbox.com/sh/36sxpfkktf7rawv/AABiPMaEmnPVSsVlR65Jr7S6a?dl=0
For this lab, you will write three functions, then convert them to template functions so they may work with any data type.
Template functions work by allowing any data type by just adding a line to the beginning of the function:
template <class T>
T someFunction(T parameter1) {
…
}
T becomes the template type, and so anywhere in this function there is a T, it is replaced by the type that is passed in. So you can call this function in several ways:
someFunction(0) returns an integer
someFunction(1.23) returns a double
someFunction(“String”) returns a string
Since it can identify the parameter’s type of T (which is an int, double, or string), it knows that the function will return the same type T (an int, double, or string).
Input
You are provided three files. The first file, integers.txt, contains 100 integers. The other two files are doubles.txt and strings.txt also contain a list of 100 of doubles and strings (single words).
Part 1
Start a Visual Studio Project, and create a file called Lab8Main.cpp. In your main() function, load in the 100 integers into an array. Then, write these functions:
* A void function called mySwap() that takes in an array parameter, and two integer positions, and will swap the items at those positions in that array
* A function called myMin() that takes in an array parameter, and returns an integer that is the lowest number out of that array
* A function called myMax() that takes in an array parameter, and returns an integer that is the highest number out of that array
* A function called mySearch() that takes in an array and an integer to find, and will return which array position that integer is located at
Make sure these functions work before continuing to part 2. You may wish to debug them by printing some information to a file or on screen to verify that they do.
Part 2
Load in the doubles and strings files, and store those in arrays. Then, modify those functions to work with any data type by making them template functions. Pay attention to what parameters and return types need to be changed to templates and what do not. (Ex: you can’t have a double or string as a
position for an array, for example: myArray[“string”] is not valid. Test the same functions to see if they work with the new types. (For example, mySearch() should work on ints, doubles, and strings.)
Output
The output will be a text file named output.txt. In it, you will print out some data that proves the functions work. First, print out the type you are working with. Second, print out elements 10 and 20 of the array, swap them, then print them out again. Third, print out the minimum and maximum of the set, then print out if these items are in the list:
Integers: 1 and 5
Doubles: 4.62557 and 1.23456
Strings: Shoes and Pumpkin
Integers:
Swapped items at positions 10 and 20
Before: [10] 78 [20] 81
After: [10] 81 [20] 78
Minimum: 2
Maximum: 99
The number 1 is at position -1
The number 5 is at position 38
Doubles:
Swapped items at positions 10 and 20
Before: [10] 7.69861 [20] 7.21923
After: [10] 7.21923 [20] 7.69861
Minimum: 0.00569
Maximum: 9.9935
The number 4.62557 is at position 19
The number 1.23456 is at position -1
Strings:
Swapped items at positions 10 and 20
Before: [10] Album [20] Vacuum
After: [10] Vacuum [20] Album
Minimum: Airport
Maximum: X-ray
The word Shoes is at position 69
The number Pumpkin is at position -1
Explanation / Answer
Templete
Integers
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.