Template functions populate_array, display_array, etc?

Hello.

I have to write a program that uses the following template functions: populate_array(), display_array(), invert_array(), sum_array(), max_element(), and min_element().
One time using integers and then using decimal numbers.

So the user would input the number of items in the array and then input the value of each item in the array.

Im trying to research and google how to use populate_array() and the others but I cant find anything that uses or talks about these template functions that I have to use.

What is what i should be looking for in order to find relevant results?
Im googling things like "C++ template function populate_array"

Thanks
Those functions are not standard C++ functions. Are you sure you are not supposed to create them, or that your professor has provided you with code that defines them?
Thanks for your answer. I was not provided with any code. So i guess I would need to create them.
This, and an example of what the program would look like when running was all i was given:
Write a program that uses the following template functions:

populate_array() takes as arguments the name of an array of type T values and an
array size in int. It prompts the user to enter type T values to be entered in the array. It
ceases taking input when the array is full.

display_array() takes as arguments the name of an array of type T values and an
array size and displays the contents of the array.

invert_array() takes as arguments the name of an array of type T values and an
array size and reverses the order of the values stored in the array.

max_element() takes as arguments the name of an array of type T values and an
array size and finds max element of the array.

min_element() takes as arguments the name of an array of type T values and an
array size and finds max element of the array.

sum_array() takes as arguments the name of an array of type T values and an
array size and return the sum of all the elements of the array.

The program should use these functions for data type int and then for data type double,

Fill an array,
Show the array,
Sum the elements of the array,
Reverse the array,
Show the array,
Sum the elements of the inverted array,
Max element of array
Minimum element of array
> populate_array() takes as arguments the name of an array of type T values and an array size in int.

Badly worded. 'Name of an array'? As a string?

What your teacher attempted to say is probably this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

template < typename T > void populate_array( T array[], int sz )
{
    std::cout << "enter " << sz << " values\n" ;
    for( int i = 0 ; i < sz ; ++i ) std::cin >> array[i] ;
}

template < typename T > void display_array( const T array[], int sz )
{
    for( int i = 0 ; i < sz ; ++i ) std::cout <<  array[i] << ' ' ;
    std::cout << '\n' ;
}

int main()
{
      const int N = 10 ;
      int array[N] = {0} ;

      populate_array( array, N ) ;
      display_array( array, N ) ;

      // TODO: other array operations
}
Thanks a lot.
I started writing some code but yours is much simpler and better than what what i was writing.
I have been writing the other functions that I need and they 'work', but Im having some problems with them.
They will work if I add the function code inside the main, but if I call the function they will not work.

From the debugging output it seems like the problem is that array_size is not defined when compiling because it must be defined by user input.
But why does it work if i add the code to main rather than calling the function?

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
#include <iostream>
#include <valarray>
#include <algorithm>
#include <iterator>
using namespace std;

template < typename T > void populate_array(T array[], int sz)
{
	std::cout << "Please enter " << sz << " values:\n";
	for (int i = 0; i < sz; ++i) std::cin >> array[i];
}

template < typename T > void display_array(const T array[], int sz)
{
	for (int i = 0; i < sz; ++i) std::cout << array[i] << ' ';
	std::cout << '\n';
}

template < typename T > void sum_array(T array[], int sz)
{
	std::valarray<int> valarray(array, array_size);
	std::cout << "The sum of the array is " << valarray.sum() << "\n";

}

template < typename T > void min_array(T array[], int sz)
{
	std::valarray<int> valarray(array, array_size);
	std::cout << "Min element of the array is " << valarray.min() << "\n";

}

template < typename T > void max_array(T array[], int sz)
{
	std::valarray<int> valarray(array, array_size);
	std::cout << "Max element of the array is " << valarray.max() << "\n";

}

template < typename T > void reverse_array(T array[], int sz)
{
	
      //does not work
      std::reverse(std::begin(arr), std::end(arr));

}



// MAIN STARTS HERE
int main()
{
	const int N = 100;
	int array_size;

	std::cout << "How many integers do you want?";
    std:cin >> array_size;
	std::cout << "\n";
	

	
	int array[N] = { 0 };

	populate_array(array, array_size);
	display_array(array, array_size);
	sum_array(array, array_size);
	min_array(array, array_size);
	max_array(array, array_size);
	reverse_array(array, array_size);

	// TODO: other array operations
}
Hi,

JLBorges code uses a static array, you are attempting to use a dynamic array incorrectly. The assignment didn't specify dynamic memory.

In function 'void sum_array(T*, int)': 21:37: error: 'array_size' was not declared in this scope


I like to name the parameter the same as the argument variable:

const int array_size = 100; // could use std::size_t

sum_array(array, array_size);

21
22
23
24
25
26
template < typename T > void sum_array(T array[], int array_size)
{
	std::valarray<int> valarray(array, array_size);
	std::cout << "The sum of the array is " << valarray.sum() << "\n";

}


That helps avoid errors like you have here. Same story for the other functions.

Remove the using namespace std; it will bite you one day. You already have array which exists in std as std::array , it's not a problem at the moment because there is no #include <array> There are other header files - especially <algorithm> which has a lot of things with common names that will likely cause a conflict.
Last edited on
//does not work
std::reverse(std::begin(arr), std::end(arr));


This is because an ordinary array does not have begin or end iterators associated with it.

Are you allowed to use the STL algorithms ?

My guess is you are most probably violating the requirements of the assignment by using STL algorithms, your teacher wants you to write the code for those functions - it's a bit trivial otherwise.
Last edited on
Topic archived. No new replies allowed.