need help

this is the assignment:
That we are “blessed” with several absolute value functions is an accident of history. C libraries were already available when C++ arrived; they could be easily used, so they were not rewritten using function overloading. You are to find all the absolute value functions you can and rewrite all of them overloading the abs function name. At a minimum, you should have the int, long, float, and double types represented.( from problem solving with c++ 9th ed.)
i have this so far:





#include <iostream>
using namespace std;


int absolute(int n);
double absolute(double n);
float absolute(float n);
long absolute(long n);

int n;
int n2;


int main()
{

int n;
int n2;



cout << "Please enter any kind of number.\n" ;
cout << "I will give you it's absolute value.\n" ;
cin >> n;


cout << "The absolute value of " << n << " is "<< n2 << "\n";




// system ("pause");



return 0;
}

int absolute(int n)
{
if (n < 0)

{
// return (n * -1); // Makes positive.
return n2 = (n * -1); // Makes positive.
}
else
{
return (n); // Already positive.}
}

}

double absolute(double n)
{
if (n < 0.0)

{
// return (n * -1); // Makes positive.
return n2 = (n * -1); // Makes positive.
}
else
{
return (n); // Already positive.}
}

}


float absolute(float n)
{
if (n < 0)

{
return n2 = (n * -1); // Makes positive.
// return (n * -1); // Makes positive.
}
else
{
return (n); // Already positive.}
}

}

long absolute(long n)
{
if (n < 0.0)

{
return n2 = (n * -1); // Makes positive.
// return (n * -1); // Makes positive.
}
else
{
return (n); // Already positive.}
}

}

All i get when running it is an absolute of 0. i cant get it to pass the abs value to n2. I would appreciate any help.
thanks.
not looking for answer just where i might be going wrong


Last edited on
Consider function templates as an extreme form of “overloading” that saves you writing the function for each data-type:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# include <iostream>

template <typename T>
T absolute (T t)
{
    return (( t >= 0) ? t : -t);
}

int main()
{
   int absInt{1};
   double absDouble{-2.3};
   float absFloat{3.56};
   long absLong{-456789012};
   long long absLongLong{-34567891011121314};

   std::cout << "Abs Int: " << absolute(absInt) << "\n";
   std::cout << "Abs Double: " << absolute(absDouble) << "\n";
   std::cout << "Abs Float: " << absolute (absFloat) << "\n";
   std::cout << "Abs Long: " << absolute (absLong) << "\n";
   std::cout << "Abs Long Long " << absolute(absLongLong) << "\n";
}
for this assignment im not allowed to use templates.
you really only need 2. You need one for the largest double your system allows (64, 80 bits?) and one for the largest int (64? 128?). c++ is happy to promote.

That is unrelated to your problem though.
You has forgotten to call your function. you need

cout << stuff << absolute(n) <<<

OR, as your design begs (but isnt necessary to have extra variable)

n2 = absolute(n)

cout << blah << n2

Doubles and floats, a single bit can be flipped for abs, if you care to find it and flip it with logic operations. I did this long ago on a system that had a poor fabs implementation, using a union and a unsigned char to access and flip the offending bit.

Ints, you need a not and a +1, the multiply approach you have is probably faster.




Last edited on
Topic archived. No new replies allowed.