C++ program Help! 2015 Fall CIS200 Lab 7 Program date: November 10, 2015 1: Rele
ID: 3773060 • Letter: C
Question
C++ program
Help! 2015 Fall CIS200 Lab 7 Program date: November 10, 2015 1: Release November 17, 2015 an unsorted an Due date: takes the array function, max func(...), hich with array of of n elements write a template element In this value. lass an input parameter and returns the class T. need to Assume for the and also write may the operators and functions you calls a hardcode must write all (data.dat): test data stored in a data file the following a 4 113 32 1.1 4.1 8.1 5.2 2.3 the student is in class is judged on the basis of alphabetical order ll In addition, the results should be printed out in maino routine to the screen and to an output file (out.txt). template typename Te void functionMethodCT inData) cout in Data n'; int maino function Method
Explanation / Answer
Program 1:
/**C++ program that reads an input text file called data.dat
and call maxfunc template funciton . Then finds the maximum value
and prints the maximum value and write to output file out.txt*/
#include<iostream>
#include<fstream>
using namespace std;
//constant set to 5
#define n 5
//function prototype
template<typename T>
int maxfunc(T arr[])
{
//assume first element is max
T max=arr[0];
for(int index=1;index<n;index++)
//find max vlaue
if(arr[index]>max)
max=arr[index];
//return max value
return max;
}
int main()
{
//array of size 5
int arr[n];
ifstream fin;
//Open input text file
fin.open("data.dat");
//Check if file exists
if(!fin)
{
cout<<"File does not exist"<<endl;
system("pause");
return 0;
}
//read elements from text file data.dat
for(int index=0;index<5;index++)
{
fin>>arr[index];
cout<<"element arr["<<index<<"]"<<":"<<arr[index]<<endl;
}
//call template fuction maxfunc with arr
int max=maxfunc(arr);
//open out.txt file
ofstream fout;
fout.open("out.txt");
if(!fout)
{
cout<<"File does not exist"<<endl;
system("pause");
return 0;
}
//print max to console
cout<<"Max element is "<<max<<endl;
//write max to out.txt file
fout<<"Max element is "<<max<<endl;
system("pause");
return 0;
}
------------------------------------------------------------
data.dat
4 1 13 3 2
Console output:
element arr[0]:4
element arr[1]:1
element arr[2]:13
element arr[3]:3
element arr[4]:2
Max element is 13
---------------------------------------------------------------
out.txt
Max element is 13
------------------------------------------------------------------------------------------
Program 2:
#include<iostream>
using namespace std;
struct Date
{
int day;
int month;
int year;
};
template<class T>
class A
{
T valuea;
public:
A( )
{
}
T getValuea() const
{
return valuea;
}
void setValuea( T x)
{
valuea=x;
}
//Copy constructor
A( A &a)
{
valuea=a.getValuea();
}
};
template<class T>
class B : public A
{
T valueb;
public:
B( ):A()
{
}
T getValueb() const
{
return valueb;
}
void setValueb( T x)
{
valueb=x;
}
//Copy constructor
B( B &b)
{
valueb=b.getValueb();
}
};
int main()
{
B<float> b1;
b1.setValueb(1.34);
cout<<b1.getValueb()<<endl;
B<float> b2;
b2.setValueb(3.14);
cout<<b2.getValueb()<<endl;
struct Date date={27,10,2015};
B<Date> b;
b.setValueb(date);
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.