#include <iostream>
#include <fstream>
using namespace std;
// template 1
template <typename T>
void get_input( T* arr, int size )
{
cout << "\nEnter the data: \n";
for ( int i = 0; i < size; i++ )
{
cout << "\tData value " << i+1 << ": ";
cin >> arr[i];
}
cout << "\nThe values have been saved" << endl;
}
// template 2
template <typename T>
void print_smallest_and_second_smallest_values( T* arr, int size )
{
T max = arr[0], second_max = arr[0];
for (int i = 1; i < size; i++)
{
if (arr[i] > max)
{
second_max = max;
max = arr[i];
}
else if (arr[i] > second_max && arr[i] != max)
{
second_max = arr[i];
}
}
cout << "\nHighest value: " << max << ", second highest value: " << second_max
<< endl;
}
// template 3
template <typename T>
void sort( T* arr, int size )
{
T temp;
for ( int i = 0; i < size; i++ )
{
for ( int j = i+1; j < size; j++ )
{
if ( arr[i] < arr[j] )
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
cout << "\nArray has been sorted in descending order\nValues: \n\t";
for ( int i = 0; i < size; i++ )
{
cout << arr[i] << " ";
}
cout << endl;
}
// tempplate 4
template <typename T>
void save_data_to_file( T* arr, int size, const string& file_name )
{
ofstream out(file_name);
for ( int i = 0; i < size; i++ )
{
out << arr[i] << " ";
}
out.close();
}
// template no.5
template <typename T>
void retrieve_data_from_file( T* arr, int size, const string& file_name )
{
T* temp = new T[size];
ifstream in(file_name);
for ( int i = 0; i < size; i++ )
{
in >> temp[i];
}
cout << "\nSuccessfully retrieved data from file '" << file_name << "'\
nValues:\n\t";
for ( int i = 0; i < size; i++ )
{
cout << temp[i] << " ";
}
delete[] temp;
}
int main ()
{
int int_arr_size, double_arr_size, string_arr_size;
cout << "enter size for integer array: ";
cin >> int_arr_size;
cout << "enter size for double array: ";
cin >> double_arr_size;
cout << "enter size for string array: ";
cin >> string_arr_size;
int* int_arr = new int[int_arr_size];
double* double_arr = new double[double_arr_size];
string* string_arr = new string[string_arr_size];
string file_names[3] = { "integer_array_file.txt", "double_array_file.txt",
"string_array_file.txt" };
get_input( int_arr, int_arr_size );
get_input( double_arr, double_arr_size );
get_input( string_arr, string_arr_size );
print_smallest_and_second_smallest_values( int_arr, int_arr_size );
print_smallest_and_second_smallest_values( double_arr, double_arr_size );
print_smallest_and_second_smallest_values( string_arr, string_arr_size );
sort( int_arr, int_arr_size );
sort( double_arr, double_arr_size );
sort( string_arr, string_arr_size );
save_data_to_file( int_arr, int_arr_size, file_names[0] );
save_data_to_file( double_arr, double_arr_size, file_names[1] );
save_data_to_file( string_arr, string_arr_size, file_names[2] );
retrieve_data_from_file( int_arr, int_arr_size, file_names[0] );
retrieve_data_from_file( double_arr, double_arr_size, file_names[1] );
retrieve_data_from_file( string_arr, string_arr_size, file_names[2] );
delete[] int_arr;
delete[] double_arr;
delete[] string_arr;