In: Computer Science
C++ , please answer both
1) Code a function object class which can scale data toward a desired maximum given the actual maximum and the desired maximum. (Reminder: to scale a value v use v*desired/actual.)
then after this
2) Show how to use your function object class from above to fix the following test scores. (What kind of loop would be most appropriate?) (Hint: What is the desired maximum of a typical test?)
double scores[MAX] = { 42, 55.5, 82, 74.5, 62 };
In the given question the desired maximum value and the actual maximum value are not given therefore I am taking these value as inputs and assume their values as desired maximum = 100 and actual maximum = 90 respectively.
Both the part of the question are combined together and solve in a single program.
In the scores array the variable MAX is not defined therefore I am assuming it as the numbers of test scores which will be provided by the user, in such case all the loops will be appropriate to execute it but I for simplicity I am using for loop.
The C++ code of the given question is as follows:-
#include <iostream>
using namespace std;
class Scale
{
public:
double desired=100;
double actual=90;
Scale(double d,double a)
{
desired=d;
actual=a;
}
double ScaleUp(double i)
{
i=i*desired/actual;
return i;
}
};
int main()
{
int MAX,i;
double desired,actual;
cout<<"Enter desired maximum value"<<endl;
cin>>desired;
cout<<"Enter actual maximum value"<<endl;
cin>>actual;
cout<<"Enter maximum number of test scores to be
scaled"<<endl;
cin>>MAX;
double scores[MAX]={42,55.5,82,74.5,62};
float result;
Scale obj(desired,actual);
for(i=0;i<MAX;i++)
{
result=obj.ScaleUp(scores[i]);
cout<<"After scaling "<<scores[i]<<" the new
value is "<<result<<endl;
}
return 0;
}
The output of the above code is as follows:-
Enter desired maximum value
100
Enter actual maximum value
90
Enter maximum number of test scores to be scaled
5
After scaling 42 the new value is 46.6667
After scaling 55.5 the new value is 61.6667
After scaling 82 the new value is 91.1111
After scaling 74.5 the new value is 82.7778
After scaling 62 the new value is 68.8889
The screenshot of the above code is as follows:-

If you have further any doubts then feel free to ask me in the comment section.