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.
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.
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.
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.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
T FindSmallest(T * a, int n)
for (int i=0; i<n; i++)
if (a[i] < 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 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?