Probably easy for you guys but I'm not sure if I have this right!!

Original function

int testing (int c, float d)
{

  if (d > c) 
   return (int) d;
      else
        return c;

}

What I need to do:
The function returns the decimal value resulting from dividing d by c if d > c and returns the decimal resulting from dividing c by d if c is greater or equal to d

My attempt plus the reset of the program:

#include <iostream>
using namespace std;
 
int testing (int a, float b);
 
int main()
{
int x;
float y;
cout << "Enter an integer and a decimal number"<< endl;
cin >> x >> y;
cout << testing (x, y);

system("PAUSE");
return EXIT_SUCCESS;
 
  return 0;
}
 
int testing (int c, float d)
{

if (d > c) 
return (float) (d/c);
else
return (float) (c/d);

}

It doesn't seem to be doing anything different I don't get any decimals printed!!

Your function is declared to return an int. As a result, the decimal portion of the return value is being truncated.

Is this correct seems better to me.

#include <iostream>
using namespace std;
 
float testing (float a, float b);
 
int main()
{
float x;
float y;
cout << "Enter an integer and a decimal number"<< endl;
cin >> x >> y;
cout << testing (x, y);

system("PAUSE");
return EXIT_SUCCESS;
 
  return 0;
}
 
float testing (float c, float d)
{

if (d > c) 
return (float) (d/c);
else
return (float) (c/d);

}

There are some minor formatting issues that you may want to correct (indentation).

Also, this section:

system("PAUSE");
return EXIT_SUCCESS;
   return 0;
}

is redundant. The statement return EXIT_SUCCESS; is the same statement as return 0; .

Probably easy for you guys but I'm not sure if I have this right!!

Original function

int testing (int c, float d)
{

  if (d > c) 
   return (int) d;
      else
        return c;

}

What I need to do:
The function returns the decimal value resulting from dividing d by c if d > c and returns the decimal resulting from dividing c by d if c is greater or equal to d

My attempt plus the reset of the program:

#include <iostream>
using namespace std;
 
int testing (int a, float b);
 
int main()
{
int x;
float y;
cout << "Enter an integer and a decimal number"<< endl;
cin >> x >> y;
cout << testing (x, y);

system("PAUSE");
return EXIT_SUCCESS;
 
  return 0;
}
 
int testing (int c, float d)
{

if (d > c) 
return (float) (d/c);
else
return (float) (c/d);

}

It doesn't seem to be doing anything different I don't get any decimals printed!!

Here your function has integer as a return type. so it will not return any decimal value if you want decimal value than mention its return type in float or double.

This article has been dead for over six months. Start a new discussion instead.