Hi. I'm trying to run a very simple program, using templates, which can do 2 things: sort an array, or reverse its elements. When I execute it, I get part of the answer, but also some weird numbers and/or letters. Any help much appreciated! (Oh, I'm using Microsoft Visual studio C++ 6)
This is my .h file:
Code:
#ifndef TEMPLATE_H
#define TEMPLATE_H
template <class ANYTYPE>
class TEMPLATE
{
public:
void Sort(ANYTYPE x[]);
void Reverse(ANYTYPE x[]);
};
#endif
This is my source.cpp file
Code:
template <class ANYTYPE>
void TEMPLATE<ANYTYPE>::Sort(ANYTYPE x[])
{
ANYTYPE i, j, imax;
ANYTYPE tempsortarray;
for(j = 4; j >= 2; j--)
{
imax = 0;
for(i = 0; i < j; i++)
{
if(x[i] > x[imax])
{imax = i;}
}
tempsortarray = x[imax];
x[imax] = x[j-1];
x[j-1] = tempsortarray;
cout << x[i] << " ";
}
}
template <class ANYTYPE>
void TEMPLATE<ANYTYPE>::Reverse(ANYTYPE x[])
{
ANYTYPE tempsortarray;
for(ANYTYPE i = 0; i < 4/2; i++)
{
tempsortarray = x[i];
x[i] = x[4 - i - 1];
x[4 - i - 1] = tempsortarray;
cout << x[i] << " ";
}
}
This is my main.cpp file
Code:
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include "Template.h"
#include "Templatesource.cpp"
int main()
{
TEMPLATE <int> x_int;
TEMPLATE <char> y_char;
TEMPLATE <float> z_float;
int a[4]={8, 6, 4, 2};
int b[4]={1, 3, 5, 7};
char c[4]={'a', 'b', 'c', 'd'};
char d[4]={'l', 'm', 'n', 'o'};
float e[4]={1.5, 2.5, 3.5, 4.5};
float f[4]={6.9, 7.9, 8.9, 9.9};
x_int.Sort(a);
y_char.Reverse(c);
return 0;
}