function template program assignment

I have a homework assignment and the professor is very hard to understand. He hasn't taught us anything yet and expects us to know how to do this from learning C first. However, I have no idea even where to begin even after taking a class in C.

The assignment is:

1. To write a header file that defines a function template that returns the smallest value (minimum) in an array of numbers (ints, doubles, floats, unsigned ints, etc.)

2. Write a test program that uses the function template above on an array of ints and an array of doubles; you may just use arrays declared with initializers.

Is this supposed to mean something to someone coming from a C class to a C++ class? Any help with this would be so greatly appreciated!!!
You should know how to do most of this if you've taken a C course, but there are definitely some things he should have explained before this assignment. Depending on when it's due he may just be assigning before he actually tells you everything you need to know (My C professor did that sometimes, but he tried to be good about it).

All you need to do is create a .h file (a header file) and include in it the template for a function. This is the part you may have not seen before in C. Then #include that file with your .cpp file (your actual program) and make sure you include it correctly (depends on what compiler you're using).
Write your .cpp program using a call to that function, and it should work just fine.

Here are two links that can help you with the template.

All you need to know about the keyword "class" that you'll see in those links is that it basically (for this purpose) is being used in place of whatever type of variable you're putting into your function (in your case an array of doubles or an array of ints). You'll learn the real meaning (and a whole lot more functionality) later on in your course.

You can use C code for the function itself (btw, cout is just the C++ version of printf so don't get confused). Just be sure to note that you may need to use some new libraries that you haven't seen before.

Post here if you run into any problems.
Oh man, thank you so much! Yeah, I figured he would have explained it better last class because the assignment is due tomorrow, but he didn't! Those links are extremely helpful as well.

Greatly appreciated!

Are there any other conversions from C to C++ that I should know about? I know that CIN(?) is like scanf. I also know the main stuff that needs to be at the top. Other than that tho, C code should work?

p.s. I'm using the C++ compiler in Fedora Linux. That's what the school is forcing us to use this semester. I could also possibly do it in Visual Studio 2008 tho.
Last edited on
alright this is my template and then after it I have my main program:

template<typename T>
int FindSmallest(T * a, int N)
int count = 0;

for (int i=0; i<n; i++)
if (a[i] < n) count++;

return count;

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

using namespace std;

int main()

int a[] = {1, 5, 7, 2};
double b[] = {1.2, 9.3, 5.5, 2.1, 7.0};

cout << "The smallest number in a: " << FindSmallest<int>(a,4) << endl;
cout << "The smallest number in b: " << FindSmallest<double>(b,5) << endl;

return 0;

this will not compile, it says something about n not being declared in the function. I was never really strong in C btw...
Case sensitive... parameter is declared as capital-N but you access lower-n.

Also your FindSmallest function is quite wrong.
jsmith is right, you have to use the same case for each call as you declare the variable. Also, use code tags when posting here, it's the # symbol to the right of the text box.

This is a decent attempt, although I don't understand your FindSmallest function at all. Are you trying to find the position of the smallest number or are you trying to find the smallest number?

If you're trying to find the smallest number all you need to do is compare a variable called smallest to each number in the array and then print the smallest one. Your loop and setup is correct but your code for your .h should look like this:
template<typename T>
T FindSmallest(T * a, int n)
  T smallest=a[0];
  for (int i=0; i<n; i++)
      if (a[i] < smallest)
  return smallest;

Note that all variables that will be set to a variable you pass in have to be of type "T" (as well as the function call) because that is the generic type you are passing in. Also, you have to set smallest = to a[0] to start because you don't know what the initialized value of smallest will be so you have to start with the first element and compare all the rest.

What were you trying to do with the count++ and return count? Are you trying to relay the position of the number in the array and not just the number itself?
Last edited on
Thanks guys!! Sorry bout the code tags, didnt even notice I could use them. Will do next time.

Yeah, jpeg, I needed exactly what you said (the smallest number itself, not the position).

Alright it compiles, how the hell do I get it to run??? What's the ./a.exe or a.out equivalent in Fedora's c++
I know nothing about different compilers (only used gcc/g++ myself), sorry.
hmm...damn teacher lol. I'm trying to find some info on google but every site seems to give everything BUT what I need haha.

Thanks again for everything jpeg!
Hm? To run just use ./a.out
nope, didn't work :-/ I have no clue lol. Its okay tho, I'll figure it out eventually. Thanks anyway!

EDIT: I figured it out. Its ./(main.exe*) *main.exe being the name of the output file
Last edited on
Topic archived. No new replies allowed.