Template Functions

Hello, I'm having trouble getting my arrays to show using the show function. I get an output but its like 1920820.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;

template <class T>

void show(T x[], int n)
{
    for (int i = 0; i < n; i++)
    {
        cout << x[n] << '\t' << endl;
    }
}

int a[5] = {9, 1, 7, 5, 0};
string s[4] = {"WINNERS" , "NEVER", "QUIT"};
char c[5] = {'a', 'e', 'i', 'o', 'u'};

int main()
{
    show(a, 5);
    show(s, 4);
    show(c, 5);

    system("pause");
    return 0;

}

On line 12:
10
11
12
13
    for (int i = 0; i < n; i++)
    {
        cout << x[n] << '\t' << endl;
    }


Surely you mean x[i] rather than x[n].
Thank you. Also I am trying to use the sort function to sort my arrays. I don't understand what should go into my parameters.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
template<class T1>
void sortArray(T1 x[], int n)
{
	
}


int a[5] = {9, 1, 7, 5, 0};
string s[4] = {"WINNERS" , "NEVER", "QUIT"};
char c[5] = {'e', 'o', 'u', 'a', 'i'};

int main()
{
    show(a, 5);
    show(s, 4);
    show(c, 5);

	sortArray(a, 5);
	sortArray(s, 4);
	sortArray(c, 5);

    system("pause");
    return 0;

}
The values you are passing into your parameters look OK to me. Do you mean to ask a different question?
Topic archived. No new replies allowed.