understanding template use

Currently I have three functions that individually add int+int, double+double/double+double/int+int, and a fucntion that concatenates strings.
Is there a way instead of having three functions, to simply use a template amnd make it a simple one function.

The goal of the code:
write a template function to add two arrays and return the resultant array
Arrays are added element by element.
The template function should work for integers , doubles, and strings
For strings the addition is actually concatenation.
Embed the function in a simple interactive test program and test it


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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

#include <iostream>
#include <string>

using namespace std;

template <class Item>
Item add(Item a, Item b){ return a+b; }

void addingIntegers()  // first function
{
    int first[20], second[20]; int c, n;
   cout << "Enter the number of elements in the array ";
   cin >> n;
   cout << "Enter elements of first array " << endl;
   for ( c = 0 ; c < n ; c++ )
      cin >> first[c];
   cout << "Enter elements of second array " << endl;
   for ( c = 0 ; c < n ; c++ )
      cin >> second[c];
   cout << "Sum of elements of two arrays " << endl;
  for ( c = 0 ; c < n ; c++ )
      cout << " " <<  add(first[c], second[c]) << " ";

}
void addingDoubles() // second
{
    float first[20], second[20]; int c, n;
   cout << "Enter the number of elements in the array ";
   cin >> n;
   cout << "Enter elements of first array " << endl;
   for ( c = 0 ; c < n ; c++ )
      cin >> first[c];
   cout << "Enter elements of second array " << endl;
   for ( c = 0 ; c < n ; c++ )
      cin >> second[c];
   cout << "Sum of elements of two arrays " << endl;
  for ( c = 0 ; c < n ; c++ )
      cout << " " <<  add(first[c], second[c]) << " ";

}

void AddingStrings() // third function
{
    string first[20], second[20]; int c, n;
   cout << "Enter the number of elements in the array ";
   cin >> n;
   cout << "Enter elements of first array " << endl;
   for ( c = 0 ; c < n ; c++ )
      cin >> first[c];
   cout << "Enter elements of second array " << endl;
   for ( c = 0 ; c < n ; c++ )
      cin >> second[c];
   cout << "Sum of elements of two arrays " << endl;
  for ( c = 0 ; c < n ; c++ )
      cout << " " <<  add(first[c], second[c]) << " ";

}

int main()
{
    char choice;
    cout << "Press 'A' or 'a' to show the addition of integer-types. " << endl;
    cout << "Press 'B' or 'b' to show the addition of double-types ."  << endl;
    cout << "Press 'C' or 'c' to show the concatenation of string literals."  << endl;
    cout << "Press any other character to end this program."  << endl;
    cout << "Your choice: ";
    cin >> choice;
    switch (choice)
    {
        case 'A':
        case 'a':
            addingIntegers();
                break;
        case 'B':
        case 'b':
            addingDoubles();
                break;
        case 'C':
        case 'c':
            AddingStrings();
                break;
        default:
            cout << "\n\n\n\t\t\t";
                break;
    }
}


I know there is way to condense, I just need more enlightened eyes to guide me to a clearer solution.

I thank you in advance,

Giovanni
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
30
31
32
33
34
35
36
#include <iostream>
#include <string>

template < typename T > void adding()
{
    const int MAX_SIZE = 20 ;

    T first[MAX_SIZE] ;
    T second[MAX_SIZE] ;

    int actual_size ;
    std::cout << "Enter the number of elements in the array ";
    std::cin >> actual_size ;

    if( actual_size > 0 )
    {
        actual_size = std::min( actual_size, MAX_SIZE ) ;

        std::cout << "Enter elements of first array\n" ;
        for( int i = 0 ; i < actual_size ; ++i ) std::cin >> first[i] ;

        std::cout << "Enter elements of second array\n" ;
        for( int i = 0 ; i < actual_size ; ++i ) std::cin >> second[i] ;

        std::cout << "Sum of elements of two arrays\n" ;
        for( int i = 0 ; i < actual_size ; ++i ) std::cout << first[i] + second[i] << ' ' ;
        std::cout << '\n' ;
    }
}

int main()
{
    adding<int>() ;
    adding<double>() ;
    adding<std::string>() ;
}
write a template function to add two arrays and return the resultant array
1
2
3
4
template <class Item>
Item add(Item a, Item b) {
  return a+b;
}

Hmmm, if that were correct, then you would use it:
1
2
3
4
5
6
const size_t N = 42;
string[N] foo;
string[N] bar;
string[N] gaz;

gaz = add( foo, bar ); // error 

Arrays are added element by element.

Your function does not do this yet.

... return the resultant array

This requires a design decision.
I thank you guys for helping me!! :)
1
2
3
4
5
6
7
8
9
10
11
12
#include <vector>

// http://www.mochima.com/tutorials/vectors.html
template < typename T > std::vector<T> add( std::vector<T> first, const std::vector<T>& second )
{
    const auto n = std::min( first.size(), second.size() );

    for( std::size_t i = 0 ; i < n ; ++i ) first[i] = first[i] + second[i] ;

    first.resize(n) ;
    return first ;
}
Our instructor has decided against using C++11 (I find it odd) ... thank you for helping me out.
@JLBorges, you are a genius!! :)
Last edited on
Topic archived. No new replies allowed.