C++ Modify arrtemp.cpp by removing the preassignment of the data for each array
ID: 3724402 • Letter: C
Question
C++
Modify arrtemp.cpp by removing the preassignment of the data for each array and by adding five new function templates.
The first new function template should allow the user to enter the array data from the keyboard.
The second new function template should print the second largest value for an array without sorting.
You may assume that each array contains distinct data, that is no duplicates.
The third new function template should sort the array data in ascending order.
The fourth new function template should save the array data to a text file.
The fifth new function template should retrieve the array data from the text file.
Output should include the second largest value of each array along with all three arrays being printed out in ascending order twice, once before the text file is saved and once after the array is retrieved from the text file. . (Make sure to include the line template<class T> before each function template)
Each array should be saved to a separate text file.
arrtemp.cpp
#include <iostream>
using namespace std;
//function template to print an array
//works for multiple data types
template <class T>
void printarray (T *a, const int n)
{
for (int i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
}
int main()
{
const int n1 = 5, n2 = 7, n3 = 6;
int a[n1] = {2, 4, 6, 8, 10};
float b[n2] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7};
char c[n3] = "HELLO";
cout <<"the integer array" << endl;
printarray(a, n1);
cout <<"the float array" << endl;
printarray(b,n2);
cout <<"the string is" << endl;
printarray(c,n3);
return 0;
}
/*
the integer array
2 4 6 8 10
the float array
1.1 2.2 3.3 4.4 5.5 6.6 7.7
the string is
H E L L O
*/
Explanation / Answer
#include<iostream>
#include<fstream>
#include<limits>
using namespace std;
template <class T> //Template of type T
void printarray (T *a, const int n) //To display integer, float and character array data
{
for (int i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
}
//To find out second largest and smallest data from integer, float and character array
template<class T> //Template of type T
void secondLargest(T *inputArray, const int elementCount)
{
//Initialize maxElement and secondMaxElement with INT_MIN.
//INT_MIN is the minimum value that can be represented by a signed int.
//INT_MIN macro is defined in limits.h header file.
T maxElement = INT_MIN;
T secondMaxElement = INT_MIN;
T min = inputArray[0]; //Consider array zero index position as the smallest one
int counter;
for(counter = 0; counter < elementCount; counter++)
{
//Checks the current position of the array is greater than max taken by us
if(inputArray[counter] > maxElement)
{
secondMaxElement = maxElement;
maxElement = inputArray[counter];
}
else if (inputArray[counter] > secondMaxElement && inputArray[counter] != maxElement)
{
secondMaxElement = inputArray[counter];
}
//To find out the minimum number from the array
if(inputArray[counter] < min)
{
//If the current position of the array is smaller than the min we have taken
//then store the current position of the array as minimum
min = inputArray[counter];
}
}
cout<<" Smallest Value = "<<min;
cout<<" Second Largest value = "<<secondMaxElement;
}
template<class T> //Template of type T
void sort(T *arr, const int len)
{
int x, y;
T Temp;
for(x = 0; x < len - 1; x++)
{
for(y = 0; y < len - x - 1; y++)
{
//Swapping process
if(arr[y] > arr[y + 1])
{
Temp = arr[y];
arr[y] = arr[y + 1];
arr[y + 1] = Temp;
}
}
}
cout<<" After Sorting: ";
printarray(arr, len);
}
//File write operation
template<class T> //Template of type T
void fileWrite (T *arr, const int len, char ch)
{
string fn;
ofstream myfile; //Creates an object of type ofstrem for writing
if(ch == 'i') //checks i for integer
myfile.open("intFile.txt");
else if(ch == 'f') //checks f for float
myfile.open("floatFile.txt");
else
myfile.open("charFile.txt"); //Otherwise character
for(int c = 0; c < len; c++)
myfile << *arr++<<" ";
myfile.close(); //Closes the file
}
//File read operation
void fileRead(char ch)
{
ifstream myfile;
string line;
cout<<" File contains : ";
if(ch == 'i') //checks i for integer
myfile.open("intFile.txt");
else if(ch == 'f') //checks f for float
myfile.open("floatFile.txt");
else //Otherwise character
myfile.open("charFile.txt");
if (myfile.is_open()) //To check file can be open or not
{
while ( getline (myfile,line)) //Extracts data from file
{
cout << line <<" "<< ' ';
}
myfile.close();
}
else cout << "Unable to open file";
}
int main()
{
const int n1 = 5, n2 = 7, n3 = 6;
int ia[n1], c, len;
float fa[n2];
char ca[n3];
string fn;
cout<<" Enter " <<n1<<" Integer data: ";
for(c = 0; c <n1; c++)
cin>>ia[c];
cout<<" Enter " <<n2<<" Float data: ";
for(c = 0; c <n2; c++)
cin>>fa[c];
fflush(stdin);
cout<<" Enter String data: ";
cin.getline(ca,sizeof(ca));
//Calculates number of characters available in array
for(len = 0; ca[len]!=''; len++);
cout <<" The Integer array Sorting: " << endl;
sort(ia, n1);
cout <<" The Float array Sorting: " << endl;
sort(fa, n1);
cout <<" The Character array Sorting: " << endl;
sort(ca, len);
cout <<" Save Integer Array Data: " << endl;
fileWrite(ia,n1,'i');
cout <<"Save Float Array Data: " << endl;
fileWrite(fa,n2,'f');
cout <<" Save Character Array Data: " << endl;
fileWrite(ca,len,'c');
cout<<" Smallest and Second Largest of Integer: ";
secondLargest(ia, n1);
cout<<" Smallest and Second Largest of Float: ";
secondLargest(fa, n2);
cout<<" Smallest and Second Largest of Character: ";
secondLargest(ca, len);
cout <<" Reading Integer array : " << endl;
fileRead('i');
cout <<" Reading Float array : " << endl;
fileRead('f');
cout <<" Reading Character array : " << endl;
fileRead('c');
return 0;
}
Output:
Enter 5 Integer data: 56
4
23
44
40
Enter 7 Float data: 12.1
23.4
45.7
89.2
1.2
5.3
56.1
Enter String data: tryit
The Integer array Sorting:
After Sorting: 4 23 40 44 56
The Float array Sorting:
After Sorting: 1.2 12.1 23.4 45.7 89.2
The Character array Sorting:
After Sorting: i r t t y
Save Integer Array Data:
Save Float Array Data:
Save Character Array Data:
Smallest and Second Largest of Integer:
Smallest Value = 4
Second Largest value = 44
Smallest and Second Largest of Float:
Smallest Value = 1.2
Second Largest value = 56.1
Smallest and Second Largest of Character:
Smallest Value = i
Second Largest value = t
Reading Integer array :
File contains : 4 23 40 44 56
Reading Float array :
File contains : 1.2 12.1 23.4 45.7 89.2 5.3 56.1
Reading Character array :
File contains : i r t t y
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.