0
\$\begingroup\$

The question is:
In many mathematical problems, the parameters need to be scaled, either to convert between units or to standardise measurements to a certain range of values. Write a void function called scale which accepts a double passed by value and two doubles passed by reference. Your function should use the first parameter to scale the other two values. Write a driver program which tests scaling values up and down. Include a precondition which assumes the scale factor is not zero; use an assert case to terminate your program if this precondition is not satisfied. Your function should print out the values before and after scaling the values to show it works correctly.

Following is my solution:

#include<iostream>
#include<cassert>

void scale(double par1, double& par2, double& par3);

int main()
{
  double par1, par2, par3;
  std::cout<<"This function will convert cm to m.\n"
           <<"Enter any two values in centimeter = ";
  std::cin>>par2>>par3;
  std::cout<<"Before scaling = "<<par2<<", "<<par3<<std::endl;
  scale(par1, par2, par3);
  std::cout<<"After Scaling = "<<par2<<", "<<par3;
}

void scale(double par1, double& par2, double& par3)
{
  par1 = 0.01;
  assert(par1 != 0);
  par2 = par2 * par1;
  par3 = par3 * par1;
}

Have I done it according to the question ?

\$\endgroup\$
2
  • \$\begingroup\$ par1 initialization should not be done in scale, but in main. \$\endgroup\$
    – Jarod42
    Feb 1, 2022 at 10:29
  • \$\begingroup\$ ok I'll change that. Is assertion statement correct and at right place(I mean right after initializing par1) ? \$\endgroup\$ Feb 1, 2022 at 11:02

1 Answer 1

1
\$\begingroup\$

par1, par2 and par3 are very poor names for function arguments. It's not at all clear what they are for.

It seems that par1 is a scale factor, so let's call it scale_factor. The two others appear to be values to be modified - value1 and value2?

The scale() function seems to ignore the supplied scale factor, so it can't be used for converting metres to millimetres (for example), or stones to kilograms.

The par1 variable in main() is never initialised, and is passed to scale() without a determined value. That would be bad if scale() actually used it.

The test program misses the requirements - "Write a driver program which tests scaling values up and down" (my emphasis). You need to demonstrate that a scale factor greater than 1 works correctly.

It's not necessary to flush the output stream using std::endl - plain old \n is simpler and more efficient. But do end all your output with newline - it's rude for a program to exit having printed an incomplete line.

\$\endgroup\$
4
  • \$\begingroup\$ I didn't understand this "which tests scaling values up and down". \$\endgroup\$ Feb 1, 2022 at 10:57
  • \$\begingroup\$ Why scale() function can't be used for conversion ? \$\endgroup\$ Feb 1, 2022 at 10:59
  • \$\begingroup\$ If you don't understand the requirements you quoted, you need to get clarification from whoever gave you those requirements. That's possibly the most important step in software development, so it's important to practice that right now! \$\endgroup\$ Feb 1, 2022 at 11:15
  • \$\begingroup\$ My understanding is that scaling up means a scale factor of more than 1.0, and scaling down means a scale factor of less than 1.0. \$\endgroup\$ Feb 1, 2022 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.