C++, using 5 template functions: 1. allow user to enter array data 2. print the
ID: 3870358 • Letter: C
Question
C++, using 5 template functions:
1. allow user to enter array data
2. print the average of the array with the smallest and largest values excluded form the calculation
3. sort array data in descending order
4. save array data to a text file (each array should be saved to a separate text file)
5. retrieve array data from the text file (print array data again after)
main template
int main()
{
const int n1 = 5, n2 = 7, n3 = 6;
int a[n1];
float b[n2];
char c[n3];
cout << "Enter integer data" << endl;
load(a, n1);
cout << "Enter float data" << endl;
load(b, n2);
cout << "Enter char data" << endl;
load(c, n3);
avg(a, n1);
avg(b, n2);
avg(c, n3);
sort(a, n1);
sort(b, n2);
sort(c, n3);
print(a, n1);
print(b, n2);
print(c, n3);
save(a,n1);
save(b,n2);
save(c,n3);
retrieve(a,n1);
retrieve(b,n2);
retrieve(c,n3);
print(a, n1);
print(b, n2);
print(c, n3);
return 0;
}
Explanation / Answer
#include<iostream>
#include <fstream>
#include <string>
using namespace std;
//Template type function to accept data
template <class T>
void load(T a[], int len)
{
cout<<" Enter "<<len<<" numbers: ";
//Loops till length
for(int co = 0; co < len; co++)
{
//Accept data
cout<<" Enter number "<<(co + 1)<<": ";
cin>>a[co];
}//End of for loop
}//End of function
//Template function to display the data
template <class T>
void print(T a[], int len)
{
//Loops till length
for(int co = 0; co < len; co++)
{
//Displays the data
cout<<" Data "<<(co + 1)<<": "<<a[co];
}//End of for loop
}//End of function
//Template function to sort the data in descending order
template <class T>
void sort(T a[], int len)
{
//Temporary variable of type template used for swapping
T temp;
//Loops till length
for(int r = 0; r < len; r++)
{
//Loops till length minus outer loop variable value minus one
for(int c = 0; c < len - r - 1; c++)
{
//Checks if array c index position value is less than array c plus one index position value then swap
if(a[c] < a[c + 1])
{
//Swapping
temp = a[c];
a[c] = a[c + 1];
a[c + 1] = temp;
}//End of if
}//End of inner for loop
}//End of outer for loop
}//End of function
//Template function to calculate average
template <class T>
void avg(T a[], int len)
{
//Template type variable to store total
T total = 0;
//To store average
float avg = 0;
//Calls the sort function
sort(a, len);
//Loops from one index position to length minus one
//Because after sorting first index position contains the biggest and last index position contains smallest value
for(int c = 1; c < len -1; c++)
//Calculates total
total += a[c];
//Calculates average
avg = total / len;
//Displays the average
cout<<" Average = "<<avg;
}//End of function
//Template function to write data to specified file
template <class T>
void save(T a[], int len)
{
//ofstream class object created
ofstream fileWrite;
//To store file name
string fileName;
//Cleans the standard input
fflush(stdin);
//Accepts file name
cout <<" Enter the file name to write: ";
getline(cin, fileName);
//Opens the specified file for writing
fileWrite.open(fileName.c_str() );
//Loops till end of array
for(int c = 0; c < len; c++)
//Writes data to file
fileWrite<<a[c]<<" ";
//Close the file
fileWrite.close();
}//End of function
//Template function to read data from specified file
template <class T>
void retrieve(T arr[], int len)
{
//ifstream class object created
ifstream fileRead;
//To store file name
string fileName;
//Clears the standard input
fflush(stdin);
//Accepts file name
cout <<" Enter the file name to read: ";
getline(cin, fileName);
//Open the specified file for reading
fileRead.open(fileName.c_str());
//Loops till length
for(int c = 0; c < len; c++)
//Reads data from file and stores it in array
fileRead>>arr[c];
//Close the file
fileRead.close();
}//End of function
//main function definition
int main()
{
//Declares and assigns length of the array constant
const int n1 = 5, n2 = 7, n3 = 6;
//Declares array with size
int a[n1];
float b[n2];
char c[n3];
cout << "Enter integer data" << endl;
load(a, n1);
cout << "Enter float data" << endl;
load(b, n2);
cout << "Enter char data" << endl;
load(c, n3);
//Calculates average of integer array
avg(a, n1);
//Calculates average of float array
avg(b, n2);
//Calculates average of character array
avg(c, n3);
//Sorts the integer array
sort(a, n1);
//Sorts the float array
sort(b, n2);
//Sorts the character array
sort(c, n3);
//Print the integer array
print(a, n1);
//Print the float array
print(b, n2);
//Print the character array
print(c, n3);
//Save the integer array in the specified file
save(a,n1);
//Save the float array in the specified file
save(b,n2);
//Save the character array in the specified file
save(c,n3);
//Retrieve data from file and stores in in integer array
retrieve(a,n1);
//Retrieve data from file and stores in in float array
retrieve(b,n2);
//Retrieve data from file and stores in in character array
retrieve(c,n3);
//Print the integer array
print(a, n1);
//Print the float array
print(b, n2);
//Print the character array
print(c, n3);
return 0;
}//End of main function
Sample Run:
Enter integer data
Enter 5 numbers:
Enter number 1: 10
Enter number 2: 2
Enter number 3: 40
Enter number 4: 3
Enter number 5: 11
Enter float data
Enter 7 numbers:
Enter number 1: 1.2
Enter number 2: 2.3
Enter number 3: 3.3
Enter number 4: 10.2
Enter number 5: 22.4
Enter number 6: 45.5
Enter number 7: 50.6
Enter char data
Enter 6 numbers:
Enter number 1: a
Enter number 2: s
Enter number 3: d
Enter number 4: f
Enter number 5: g
Enter number 6: h
Average = 4
Average = 11.9571
Average = -17
Data 1: 40
Data 2: 11
Data 3: 10
Data 4: 3
Data 5: 2
Data 1: 50.6
Data 2: 45.5
Data 3: 22.4
Data 4: 10.2
Data 5: 3.3
Data 6: 2.3
Data 7: 1.2
Data 1: s
Data 2: h
Data 3: g
Data 4: f
Data 5: d
Data 6: a
Enter the file name to write: Integer.txt
Enter the file name to write: Float.txt
Enter the file name to write: Character.txt
Enter the file name to read: Integer.txt
Enter the file name to read: Float.txt
Enter the file name to read: Character.txt
Data 1: 40
Data 2: 11
Data 3: 10
Data 4: 3
Data 5: 2
Data 1: 50.6
Data 2: 45.5
Data 3: 22.4
Data 4: 10.2
Data 5: 3.3
Data 6: 2.3
Data 7: 1.2
Data 1: s
Data 2: h
Data 3: g
Data 4: f
Data 5: d
Data 6: a
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.