I found this functions are maybe important for some people there for i wrote this code .
--------------------------------------------------------------------------
This Max() function is used to find the maximum value in a list
#include <iostream>
using namespace std;
int Max(const int *Numbers, const int Count)
{
int Maximum = Numbers[0];
for(int i = 0; i < Count; i++)
if( Maximum < Numbers[i] )
Maximum = Numbers[i];
return Maximum;
}
double Max(const double *Numbers, const int Count)
{
double Maximum = Numbers[0];
for(int i = 0; i < Count; i++)
if( Maximum < Numbers[i] )
Maximum = Numbers[i];
return Maximum;
}
int main()
{
int Nbrs[] = { 12, 483, 748, 35, 478 };
int Total = sizeof(Nbrs) / sizeof(int);
int Maximum = Max(Nbrs, Total);
cout << "Maximum: " << Maximum << endl;
return 0;
}
Here is an example of running the program:
Maximum: 748
Press any key to continue
----------------------------------------------------------------------------------------------------------------------------------------------------
This Min() function is used to find the maximum value in a list
#include <iostream>
using namespace std;
int Min(const int *Numbers, const int Count)
{
int Minimum = Numbers[0];
for(int i = 0; i < Count; i++)
if( Minimum > Numbers[i] )
Minimum = Numbers[i];
return Minimum;
}
double Min(const double *Numbers, const int Count)
{
double Minimum = Numbers[0];
for(int i = 0; i < Count; i++)
if( Minimum > Numbers[i] )
Minimum = Numbers[i];
return Minimum;
}
int main()
{
int Nbrs[] = { 12, 483, 748, 35, 478 };
int Total = sizeof(Nbrs) / sizeof(int);
int Minimum = Min(Nbrs, Total);
cout << "Minimum: " << Minimum << endl;
return 0;
}
Here is an example of running the program:
Minimum: 12
Press any key to continue
----------------------------------------------------------------------------------------------------------------------------------------------------This example calculates the sum of values in a range. The program requests two natural numbers from the users
#include <iostream>
using namespace std;
int main()
{
long Sum = 0;
long First, Last;
cout << "This program allows you to calculate the sum of ";
cout << "numbers\nfrom a lower to a higher values in a range\n";
cout << "Enter the first: ";
cin >> First;
cout << "Enter the last: ";
cin >> Last;
// Make sure that the last number is higher than the first
// If not, inverse them
if( First > Last )
{
int Temp;
Temp = First;
First = Last;
Last = Temp;
}
for( int Counter = First; Counter <= Last; Counter++ )
Sum += Counter;
cout << "\nSum of numbers from " << First << " to " << Last
<< " = " << Sum << endl;
return 0;
}
Here is an example of running the program:
This program allows you to calculate the sum of numbers
from a lower to a higher values in a range
Enter the first: 4
Enter the last: 52
Sum of numbers from 4 to 52 = 1372
Press any key to continue
----------------------------------------------------------------------------------------------------------------------------------------------------Greatest Common Divisor
#include <iostream>
using namespace std;
int GCD(int a, int b)
{
while( 1 )
{
a = a % b;
if( a == 0 )
return b;
b = b % a;
if( b == 0 )
return a;
}
int main()
{
int x, y;
cout << "This program allows calculating the GCD\n";
cout << "Value 1: ";
cin >> x;
cout << "Value 2: ";
cin >> y;
cout << "\nThe Greatest Common Divisor of "
<< x << " and " << y << " is " << GCD(x, y) << endl;
return 0;
}
--------------------------------------------------------------------------
This is an example function that calculates the square root of a number:
#include <iostream>
using namespace std;
double Abs(double Nbr)
{
if( Nbr >= 0 )
return Nbr;
else
return -Nbr;
}
double SquareRoot(double Nbr)
{
double Number = Nbr / 2;
const double Tolerance = 1.0e-7;
do Number = (Number + Nbr / Number) / 2;
while( Abs(Number * Number - Nbr) > Tolerance);
return Number;
}
int main()
{
double Number = 1448.64;
double Nbr = SquareRoot(Number);
cout << "The square root of " << Number << " is " << Nbr << "\n\n";
return 0;
}
Here is an example of running the program
The square root of 1448 is 38.0526
Press any key to continue
--------------------------------------------------------------------------
This is a simple function that calculates the absolute value of a number:
#include <iostream>
using namespace std;
double Abs(double Nbr)
{
// return (Nbr >= 0) ? Nbr : -Nbr;
if( Nbr >= 0 )
return Nbr;
else
return -Nbr;
}
int main()
{
double Number = -88;
double Nbr = Abs(Number);
cout << "The absolute value of " << Number << " is " << Nbr << endl;
return 0;
}
Here is an example of running the program:
The absolute value of -88 is 88
Press any key to continue