Introduction to C++ absolute value
The C++ absolute is one of the default function the abs() will return the integer number values because it needs the round off value. so the absolute value is the integer data type this function is defined by using the <cstdlib> header files and also it overloaded the <cmath> directives also. It does not accepts floating point values. The function is used for finding the absolute values as well as it will return the absolute value of the input numbers. It returns the absolute values the data types are casted and it compatible for the other data types values.
Syntax:
In C++ each object, variables, keywords, and functions have their own syntax and attributes for declaring in the programming codes. Based on the requirements we will utilize the special keywords, variables, data types, and functions from the programming libraries. The abs() is one of the function from the math library and it is used to calculate the absolute values of the input numbers.
Code:
#include<iostream>
#include<cmath>
using namespace std;
data type main()
{
data type variable name;
abs(variable name);
----some C++ code logics---
}
The above codes are the basic syntax for calculating the absolute values of the user-inputs. These function does not throw the Exceptions.
How absolute value works in C++?
The C++ having n number of reserved keywords, functions that will provided the some level of abstractions from the actual namespaces as well as the new libraries which is being already used for the programmers to allow it with the more focus on the coding concepts. It also makes it easier for to write the programming codes and clean it up using the some methods like destroy() or any other default methods belonging to the garbage collections and it’s the main area for destroying the unwanted codes and clean up the memory space areas. It depends upon the data types and the object creation sizes must be calculated and it allocates the memory space for both the big storage data type variables as well small amount storage variables. Normally the C++ declarations and the directives are used with some kind of memory spaces allocated for the functions to store it in the RAM. The function declaration and the definition will be the standard type of protocols and it brings all the type of members and the functions are calculated into the current and future scopes.
According to the abs() function it will calculates the user inputs to the absolute values it may be any data type ranges. Sometimes the double, float are the some datatypes values are not acceptable and the values being round off it in the outputs it denotes as an absolute values. The abs() function will take and passed as the single level of parameters and it will return the int, long int or long long int. The valid integers will return the valid integers numbers else the sequence is not occurred on that time either the data type has the empty or null values it must contain only for the whitespace characters. There is no conversions occurred in the function and also there is no return value is found if the variable is not initialized in the function. Also, the calculated integer value is out of range means the represented value is in int data type and it causes some undefined behavior and it throws the exceptions. The scientific and mathematical calculations are needed and required for the user-inputs it will be the more accurate and valid type of values are performed and returned with the absolute valid integer values.
Examples
Lets us discuss examples of C++ absolute value.
Example #1
Code:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
bool demo(char a)
{
return (a >= '0' && a <= '9')
? true
: false;
}
int demo1(char* s)
{
if (*s == '\0')
return 0;
int b = 0;
int s1 = 1;
int p = 0;
if (s[0] == '-') {
s1 = -1;
p++;
}
for (; s[p] != '\0'; ++p) {
if (demo(s[p]) == false)
return 0;
b = b * 10 + s[p] - '0';
}
return s1 * b;
}
int main()
{
char c[] = "-987654";
int output = demo1(c);
printf("%d ", output);
int m = -3;
long n = -87645;
int l = abs(m);
long w = abs(n);
cout << "abs(" << m << ") = |" << m << "| = " << l << endl;
cout << "abs(" << n << ") = |" << n << "| = " << w << endl;
return 0;
}
4.5 (4,545 ratings)
View Course
Output:
Example #2
Code:
#include <iostream>
using namespace std;
struct demo {
virtual void one(int) { std::cout << "demo::one\n"; }
void two(char) { std::cout << "demo::two\n"; }
void three(int) { std::cout << "demo::three\n"; }
protected:
int a;
typedef int val;
};
struct demo1 : demo {
using demo::a;
using demo::val;
using demo::one;
void one(int) { std::cout << "demo1::one\n"; }
using demo::two;
void two(int) { std::cout << "demo1::two\n"; }
using demo::three;
void three(int) { std::cout << "demo1::three\n"; }
};
int main()
{
demo1 i;
demo& d = i;
int m;
i.a = 3;
i.one(3);
i.one(3);
i.two(2);
i.two('k');
i.three(3);
i.three(3);
cout<<"Please enter your number:";
cin>>m;
if(m>=0)
cout<<"Welcome User the absolute value of your input is:"<<m;
else
cout<<"Thank you User your absolute value of the input is"<<-(m);
return 0;
}
Output:
Example #3
Code:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float m;
m = -67;
cout<<"abs("<<m<<"): "<<abs(m)<<endl;
m = -676.5645;
cout<<"abs("<<m<<"): "<<abs(m)<<endl;
m = 7665.2456;
cout<<"abs("<<m<<"): "<<abs(m)<<endl;
m = -.67832;
cout<<"abs("<<m<<"): "<<abs(m)<<endl;
m = -.87892;
cout<<"abs("<<m<<"): "<<abs(m)<<endl;
m = -6767.25245;
cout<<"abs("<<m<<"): "<<abs(m)<<endl;
m = -.6527;
cout<<"abs("<<m<<"): "<<abs(m)<<endl;
return 0;
}
Output:
Conclusion
The absolute() function is one of the most important functions to calculate the absolute set of user input values and it will return the same as the outputs. It has supports all types of integer data type values if the input is in some other data types it also performed and convert it as the integer values.
Recommended Articles
This is a guide to C++ absolute value. Here we discuss the introduction and How C++ absolute value works in C++? along with few examples respectively. You may also have a look at the following articles to learn more –